Solved

color row in JTable

Posted on 2002-05-12
3
1,453 Views
Last Modified: 2011-09-20
How to color programatically groups of rows to group visually and differentiate between them in a JTable.

I mean, color gray two of them, then color white the next 3, or 4 or just one, then ...etc, etc.
0
Comment
Question by:eov
3 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 7005080
This is an example of a table cell renderer. It extends the DefaultTableCellRenderer. What it does is to set a cell's background to gray if it is on an odd row, and to white if it is on an even row. If a cell is selected, then its background color will stay as the default. In all cases, the foreground of the cell is the default color. If a cell has the focus, then its border is set to the default border.

class MyTableCellRenderer extends DefaultTableCellRenderer {
 public Component getTableCellRendererComponent(
   JTable table, Object value, boolean isSelected,
   boolean hasFocus, int row, int column) {
    setOpaque(true);
    setText(value.toString());

    // set cell's foreground to default cell foreground color
    setForeground(table.getForeground());

    // if cell is selected, set background color to default cell selection background color
    if (isSelected) {
      setBackground(table.getSelectionBackground());
    }

    // otherwise, set cell background color to our custom color
    else {
       // set odd rows background to gray
       if (row % 2 == 0) {
         setBackground(Color.GRAY);
       }

       // set even rows background to white
       else {
         setBackground(Color.WHITE);
       }
    }

    // draw border on cell if it has focus
    if (hasFocus) {
      setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
    }

    // position cell text at center
    setHorizontalAlignment(SwingConstants.CENTER);

    return this;
 }
}


As an example of its use, first we create an instance of our table cell renderer:

MyTableCellRenderer tableCellRenderer = new MyTableCellRenderer();

Then we set the renderer to all the columns in the table (assume total of 5 columns):

for (int i = 0; i < 5; i++) {
 // get each table column
 TableColumn tableColumn = myTable.getColumnModel().getColumn(i);

 // set our own table cell renderer for each table column.
 tableColumn.setCellRenderer(tableCellRenderer);
}
0
 

Expert Comment

by:cini_gm
ID: 7005264
0
 

Author Comment

by:eov
ID: 7019856
Thanks with minor changes I adapted it to my problem
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now