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 933031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:45:57+00:00 2026-05-15T20:45:57+00:00

Using a different background color for odd and even rows is a commonly used

  • 0

Using a different background color for odd and even rows is a commonly used trick to improve readability of large tables.

I want to use this effect in Swing’s JTable. I started out by creating a custom table renderer, but this can only be used to paint actual cells, and I also want to add stripes to the “white” part of the table where there might be no cells. I can subclass JTable and override paintComponent(), but I would prefer an option where I can just change the table’s rendering.

Is there a better way of doing this?

Edit: According to the answers so far this seems to be impossible without extending JTable. However, when I override JTable.paintComponent() it also only paints the area where there are rows. How can I paint the rest?

  • 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-15T20:45:58+00:00Added an answer on May 15, 2026 at 8:45 pm

    Use getCellRect( getRowCount() - 1, 0, true ).y to get the top y-coordinate of the empty space, and then paint some Rectangles and (Grid-)Lines with paintComponent( Graphics g ).

    jtable with empty space grid

    To make it much easier for you, here’s a long (but complete) solution 😉

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    public class StripedEvenInWhitePartsTable extends JTable
    {
    
      public StripedEvenInWhitePartsTable( String[][] data, String[] fields )
      {
        super( data, fields );
        setFillsViewportHeight( true ); //to show the empty space of the table 
      }
    
    
      @Override
      public void paintComponent( Graphics g )
      {
        super.paintComponent( g );
    
        paintEmptyRows( g );
      }
    
    
      public void paintEmptyRows( Graphics g )
      {
        Graphics newGraphics = g.create();
        newGraphics.setColor( UIManager.getColor( "Table.gridColor" ) );
    
        Rectangle rectOfLastRow = getCellRect( getRowCount() - 1, 0, true );
        int firstNonExistentRowY = rectOfLastRow.y; //the top Y-coordinate of the first empty tablerow
    
        if ( getVisibleRect().height > firstNonExistentRowY ) //only paint the grid if empty space is visible
        {
          //fill the rows alternating and paint the row-lines:
          int rowYToDraw = (firstNonExistentRowY - 1) + getRowHeight(); //minus 1 otherwise the first empty row is one pixel to high
          int actualRow = getRowCount() - 1; //to continue the stripes from the area with table-data
    
          while ( rowYToDraw < getHeight() )
          {
            if ( actualRow % 2 == 0 )
            {
              newGraphics.setColor( Color.ORANGE ); //change this to another color (Color.YELLOW, anyone?) to show that only the free space is painted
              newGraphics.fillRect( 0, rowYToDraw, getWidth(), getRowHeight() );
              newGraphics.setColor( UIManager.getColor( "Table.gridColor" ) );
            }
    
            newGraphics.drawLine( 0, rowYToDraw, getWidth(), rowYToDraw );
    
            rowYToDraw += getRowHeight();
            actualRow++;
          }
    
    
          //paint the column-lines:
          int x = 0;
          for ( int i = 0; i < getColumnCount(); i++ )
          {
            TableColumn column = getColumnModel().getColumn( i );
            x += column.getWidth(); //add the column width to the x-coordinate
    
            newGraphics.drawLine( x - 1, firstNonExistentRowY, x - 1, getHeight() );
          }
    
          newGraphics.dispose();
    
        } //if empty space is visible
    
      } //paintEmptyRows
    
    
    
      public Component prepareRenderer( TableCellRenderer renderer, int row, int column )
      {
        Component c = super.prepareRenderer( renderer, row, column );
    
        if ( !isRowSelected( row ) )
        {
          c.setBackground( row % 2 == 0 ? getBackground() : Color.ORANGE );
        }
    
        return c;
      }
    
    
      public static void main( String[] argv )
      {
        String data[][] = { { "A0", "B0", "C0" }, { "A1", "B1", "C1" }, { "A2", "B2", "C2" }, { "A3", "B3", "C3" }, { "A4", "B4", "C4" } };
        String fields[] = { "A", "B", "C" };
    
        JFrame frame = new JFrame( "a JTable with striped empty space" );
        StripedEvenInWhitePartsTable table = new StripedEvenInWhitePartsTable( data, fields );
        JScrollPane pane = new JScrollPane( table );
    
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( pane );
        frame.setSize( 400, 300 );
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
      }
    
    }
    

    This example could be extended to:

    • fix the painted pseudogrid for variable RowHeights (I’m using the lowest height used in any row)
    • explain to the user why nothing happens if he clicks in the empty space to edit the cells (via tooltip)
    • add an extra row to the table model if the user clicks in the empty space (nooo! no Excel please!)
    • use the empty space to draw a reflection of the table (including all rendered data ( what for? 😉 )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to apply different background color to odd and even
I want to make different background color for ul children depending on their orders,
I am using jQuery to animate the background colors of 3 different divs, repeated
How can I draw two different rectangles with black background without using subclass of
I need that when i check a checkbox i apply different background color to
In Emacs, I'm using a color scheme with a dark background and light text.
What I know: Rails has the cycle() method that enables odd/even rows in a
I want to toggle my content using different links. If the contents of div2
I can set the background color of the current page using: document.body.style.background='red'; Is there
I have a table that has rows with different classes depending on what color

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.