views:

298

answers:

1

im trying to implement a Chat feature in my application. i have used 2 JEditorPane. one for holding chat history and the other for sending chat to the previous JEditorPane.

the JEditorPane is text/html type.

the problem i am having is when i put more than one space between characters it is automatically removed by the parser because it is HTML!

how can i make it so, that the spaces are not stripped?

example: hello               world

becomes: hello world

also i am having to parse the html tags so the new messages can be added to the history window.

is there a better option than using JEditorPane? if i used JTextPane would it be easier to implement?

i would like the chat boxes/panes to be able to handle bold, URL embedding for now.

thank you and look forward to your guidance.

EDIT: im trying to replace " " with a relavent char.

newHome[1] = newHome[1].replace(" ", newChar) 

what should be the newChar value?

EDIT: im trying:

newHome[1] = newHome[1].replaceAll(" ", " ");

but it is not producing the results. any ideas?

EDIT: @Thomas - thanks! for some reason i can post a note to your answer.

+1  A: 

Using HTML markup is a quick way to get simple text formatting done in a Swing text component. However, it's not the only way.

A more sophisticated method is to use a javax.swing.text.StyledDocument to which you can attach different "styles" (hence the name). A style is basically a set of attributes, for instance, whether the text should be in bold or italics or what Color it should have.

JTextPane provides a number of convenience methods to deal with styles, and it is a subclass of JEditorPane which means it should integrate rather seamlessly into your existing code. As an example, to mark a portion of the text within a JTextPane as bold, you could use something like this:

JTextPane textPane = new JTextPane();
Style bold = textPane.addStyle("bold", null);
StyleConstants.setBold(bold, true);

textPane.setText("I'll be bold.");

textPane.getStyledDocument().setCharacterAttributes(8, 4, bold, true);

Similarly, you could define a second style that e.g. uses a blue, underlined font and which you could use to display hyperlinks.

Unfortunately, the downside is that you will have to take care of the mechanics of the links yourself. Although you can use the existing infrastructure of javax.swing.event.HyperlinkListener et al., you will be responsible for detecting mouse clicks. The same goes for hovering and changing the Cursor into a hand-symbol etc.

Thomas