Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3677952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:17:00+00:00 2026-05-19T03:17:00+00:00

I would like to alternate between gray and white as background from one line

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:17:00+00:00Added an answer on May 19, 2026 at 3:17 am

    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);
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have alternate behavior during a print stylesheet on a web
i would to have alternate 2 colors of rows, like the first black, the
Is there any alternative to this: Organizations.Include(Assets).Where(o => o.Id == id).Single() I would like
My Case: I have original Page Control app from Apple. I would like to
I would like to use a technology that is used for communication between services
I would like to share a Subversion repository between my main computer running OS
I would like to generate uniform random numbers in C++ between 0 and 1,
Does anyone know of a good alternative to cron? I would like something that
Would anyone like to recommend free alternative to SharePoint Portal ? We want to
Would like to get a list of advantages and disadvantages of using Stored Procedures.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.