9

I would like to alternate between gray and white as background from one line of my JTextPane to the next. I tried to do this by overloading the paintComponent() method and drawing the background manually according to component height an Font size but I did not succeed.

Any hints?

2
  • What do you mean by "one line"? Will setBackground() not work if you alternate the color you use?
    – jzd
    Jan 12, 2011 at 15:48
  • is your textpane editable? i.e. do you want the backgrounds to change if lines are added/removed?
    – dogbane
    Jan 12, 2011 at 17:37

5 Answers 5

2

You can use the line highlighter (here or here), written by @VonC, in response to this question.

In order to highlight alternate lines you can use it as follows:

public static void main(String[] args)  {

    JTextPane t = new JTextPane();
    t.setSelectionColor(new Color(1.0f, 1.0f, 1.0f, 0.0f));
    Highlighter hilite = new MyHighlighter();
    t.setHighlighter(hilite);
    t.setText("Line1\nLine2\nLine3\nLine4\nLine5\n");


    DefaultHighlightPainter whitePainter = new DefaultHighlighter.DefaultHighlightPainter(Color.WHITE);
    DefaultHighlightPainter grayPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GRAY);

    try {
        Document doc = t.getDocument();
        String text = doc.getText(0, doc.getLength());
        int start = 0;
        int end = 0 ;

        boolean even = true;

        //look for newline char, and then toggle between white and gray painters.
        while ((end = text.indexOf('\n', start)) >= 0) {
            even = !even;
            DefaultHighlightPainter painter = even ? grayPainter : whitePainter;
            hilite.addHighlight(start, end+1, painter);
            start = end+1;
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    JPanel p = new JPanel(new BorderLayout());      
    p.add(t, BorderLayout.CENTER);
    JFrame f = new JFrame();
    f.add(p);
    f.setSize(100, 100);
    f.setVisible(true);
}

Here is VonC's MyHighlighter:

class MyHighlighter extends DefaultHighlighter
{

    private JTextComponent component;

    /**
     * @see javax.swing.text.DefaultHighlighter#install(javax.swing.text.JTextComponent)
     */
    @Override
    public final void install(final JTextComponent c)
    {
        super.install(c);
        this.component = c;
    }

    /**
     * @see javax.swing.text.DefaultHighlighter#deinstall(javax.swing.text.JTextComponent)
     */
    @Override
    public final void deinstall(final JTextComponent c)
    {
        super.deinstall(c);
        this.component = null;
    }

    /**
     * Same algo, except width is not modified with the insets.
     * 
     * @see javax.swing.text.DefaultHighlighter#paint(java.awt.Graphics)
     */
    @Override
    public final void paint(final Graphics g)
    {
        final Highlighter.Highlight[] highlights = getHighlights();
        final int len = highlights.length;
        for (int i = 0; i < len; i++)
        {
            Highlighter.Highlight info = highlights[i];
            if (info.getClass().getName().indexOf("LayeredHighlightInfo") > -1)
            {
                // Avoid allocing unless we need it.
                final Rectangle a = this.component.getBounds();
                final Insets insets = this.component.getInsets();
                a.x = insets.left;
                a.y = insets.top;
                // a.width -= insets.left + insets.right + 100;
                a.height -= insets.top + insets.bottom;
                for (; i < len; i++)
                {
                    info = highlights[i];
                    if (info.getClass().getName().indexOf(
                            "LayeredHighlightInfo") > -1)
                    {
                        final Highlighter.HighlightPainter p = info
                                .getPainter();
                        p.paint(g, info.getStartOffset(), info
                                .getEndOffset(), a, this.component);
                    }
                }
            }
        }
    }
}
1

Here is an example of a possible solution.

To explain it a little, JTextPane is a styled document editor, and as such, allows one to change StyledDocument, that's to say a document which includes style information, like the background color for a given line.

0
JTextField someField = new JTextField();
someField.setBackground(Color.GREY );
3
  • Sory I meant JTextPane not JTextField... I have corrected the question.
    – jumar
    Jan 12, 2011 at 15:52
  • Unfortunatly, this one will change the whole JTextPane color, and not just one of its lines.
    – Riduidel
    Jan 12, 2011 at 15:56
  • @Riduidel, yes, but the way the question was originally worded this made sense.
    – jzd
    Jan 12, 2011 at 23:36
0

I tried to do this by overloading the paintComponent() method and drawing the background manually according to component height an Font size but I did not succeed.

Sounds reasonable. Make sure you make the text pane non-opaque.

Then the basic code would be:

  1. paint your alternating background based on the clip region
  2. invoke super.paintComponent() so the text is painted over top of the background.

If you need more help then post the SSCCE demonstrating the problem.

-1

JTextPane will take an HTML document as text. Have you considered adding your text to the JTextPane via an HTML document?

JTextPane pane = new JTextPane();
pane.setContentType("text/html");

String text = /*some html table or set of divs for formatting */

pane.setText(text);

myJFrame.add(pane);

When you build the text string, you can set the alternating colors with css, or hard code it directly into you created document. This will help you avoid having to override the repaint methods.

This will work good in instances where you will not allow the user to edit the box (ie setEditable to false) If you expect this to repaint the line each time the user hits the enter key, you will have to adapt the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.