3 Replies Latest reply on Mar 17, 2008 1:13 PM by 843806

    Stretching background colour across whole JTextPane for one line of text

      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();
      StyleConstants.setForeground(attrs, Color.red);
      StyleConstants.setBackground(attrs, Color.blue);
      SimpleAttributeSet attrs2 = new SimpleAttributeSet();
      StyleConstants.setForeground(attrs2, Color.gray);
      StyleConstants.setBackground(attrs2, Color.blue);
      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,
        • 1. Re: Stretching background colour across whole JTextPane for one line of text
          Probably not very efficient, but seems to work ... fairly clean imho :-)
          import java.awt.BorderLayout;
          import java.awt.Color;
          import java.awt.Dimension;
          import java.awt.Graphics;
          import java.awt.Rectangle;
          import javax.swing.JFrame;
          import javax.swing.JScrollPane;
          import javax.swing.JTextPane;
          import javax.swing.SwingUtilities;
          import javax.swing.event.CaretEvent;
          import javax.swing.event.CaretListener;
          import javax.swing.plaf.basic.BasicTextPaneUI;
          import javax.swing.text.BadLocationException;
          public class LineHighlightTextPane {
             public LineHighlightTextPane () {
             void makeUI () {
                JTextPane textPane = new JTextPane ();
                textPane.setUI (new LineHighlightTextPaneUI (textPane));
                JScrollPane scrollPane = new JScrollPane (textPane);
                JFrame frame = new JFrame ("Line Highlight Text Pane");
                frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                frame.setSize (300, 300);
                frame.add (scrollPane, BorderLayout.CENTER);
                frame.setVisible (true);
             public static void main (String[] args) {
                SwingUtilities.invokeLater (new Runnable () {
                   public void run () {
                      new LineHighlightTextPane ().makeUI ();
          class LineHighlightTextPaneUI extends BasicTextPaneUI {
             JTextPane tc;
             LineHighlightTextPaneUI (JTextPane t) {
                tc = t;
                tc.addCaretListener (new CaretListener () {
                   public void caretUpdate (CaretEvent e) {
                      tc.repaint ();
             public void paintBackground (Graphics g) {
                super.paintBackground (g);
                try {
                   Rectangle rect = modelToView(tc, tc.getCaretPosition ());
                   int y = rect.y;
                   int h = rect.height;
                   g.setColor (Color.YELLOW);
                   g.fillRect (0, y, tc.getWidth (), h);
                } catch (BadLocationException ex) {
          Is that what you wanted or have I misread the requirement?

          • 2. Re: Stretching background colour across whole JTextPane for one line of text
            Or some code that should work on any JTextComponent:

            • 3. Re: Stretching background colour across whole JTextPane for one line of text
              Thanks a lot for the help guys! I was able to do exactly what I needed.