Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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();
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,
aqzman

Comments

darrylburke
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 ();
         }
      });
   }
   
   @Override
   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) {
         ex.printStackTrace();
      }
   }
}
Is that what you wanted or have I misread the requirement?

db
camickr
Or some code that should work on any JTextComponent:

http://www.discoverteenergy.com/files/LinePainter.java
843806
Thanks a lot for the help guys! I was able to do exactly what I needed.
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 14 2008
Added on Mar 14 2008
3 comments
700 views