Skip to Main Content

Java SE (Java Platform, Standard Edition)


For appeals, questions and feedback, please email

Stretching background colour across whole JTextPane for one line of text

843806Mar 14 2008 — edited Mar 17 2008
Hey guys,

There's something I'd like to do with my JTextPane, but I don't know if it's possible without too much fooling around.

When a line of text is entered into the JTextPane the foreground and the background of the entered message are coloured. All of that works fine, but if possible I would like to also fill the remaining white space of the line the same colour as the existing background, just to make everything look nicer. I would also want the background colour to stretch if the window is stretched, as the JTextPane stretches

This is how I'm going about it.
SimpleAttributeSet attrs = new SimpleAttributeSet();

SimpleAttributeSet attrs2 = new SimpleAttributeSet();
StyleConstants.setForeground(attrs2, Color.gray);

Document doc = conversion.getDocument();

FileCredential credential = ((ClientService)pixecurFile).getCredential();

conversion.getDocument().insertString(doc.getLength(), credential.userData(FileCredential.USER_NAME) + " Says:  ", attrs);
conversion.getDocument().insertString(doc.getLength(), now("yyyy-MM-dd HH:mm:ss aa")+"\n", attrs2);
conversion.getDocument().insertString(doc.getLength(), "   " + chatMessage.getText()+"\n", attrs);
I'm willing to bet that there is no clean way to go about what I'm looking to do, but I hope I'm wrong.

Also, I don't think this matters, but the JTextPane in question is inside of a JScrollPane.

Thanks in advance,
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2008
Added on Mar 14 2008