I'm working on a chat client and I have this problem: What I want to do is divide the JTextPane in 3 colums so that in the first one it puts out the name of the user, in the second the message and the date in the third. I tried to do this by creating 3 separate JTextPane, then synchronized their scrollbars and everything seemed to be working perfectly until I saw how it reacted to the insertion of emotiocons. When the message had multiple lines of code, the JTextPanes for name and date printed new lines but the emoticons have different height and the name and date JTextPanes can't keep up with them. For a single line I solved it by inserting a blank image of dimensions (1px, height of the emoticon), but that works only for one of the lines. Let's get back to my question. I'm not willing to continue brainstorming on how to fix the problem in the case where I have multiple JTextPanes because I'm sure even more problems are going to go out like that. Is there another way to go around it? Can I do it with only one JTextPane and how? Here is a screenshot of how it works with 3 JTextPanes: http://postimage.org/image/611z2807b/ Here is a picture of how I have structured the JTextPanes in Eclipse: http://postimage.org/image/n2ut4bf2f/ I hope that's enough info and thanks in advance! I didn't provide a SSCCE because my code is a few hundred lines and it would really take me a while to just take out the important lines for the question, but if it's trully necessary, I will.
-
I'd preffer to first see if there is a way to do it with only one JTextPane because I really want to just wipe out the old code and replace it with a new one. If no one can figure out another way(other than the 3 JTextPane method) then I'll post the SSCCE as it would concern the 3 JTextPane method entirely. For now I hope someone will think of another way. – martin Dec 25 '12 at 17:42
-
Perhaps instead of a JTextPane, you can use a JEditorPane containing HTML, and put all of the content into a borderless `
` element. But since the Swing HTML renderer is so old, I'm not sure what performance will be like when the component displays dozens or hundreds of messages.
– VGR Dec 25 '12 at 22:54
3 Answers
You can add your custom tables as described here http://java-sl.com/JEditorPaneTables.html
Add 3 columns table placing in the row's cells your content. You can also set invisible borders if you need.

- 56,971
- 9
- 68
- 98
Some Java Swing components are HTML-capable so there may be a JComponent
which allows you to format text output (and maybe input?) the way you're describing.
Many chat clients print the user name and the message in the same field so you can select and copy both the user name and the message.

- 855
- 10
- 21
I think you'll be hard pressed to make this work really well with only a single JTextPane
, or even a few. You'll end up putting in more work to make it solid than you would taking a simpler approach.
Looking the the pictures you provided, I see a perfect use of a 3 column GridLayout
with each column vertically aligned to the top.
If you find that you encounter performance issues with really long chats, then you can optimize for performance. But don't worry about it until you get there.

- 5,692
- 7
- 37
- 74