Solved

color row in JTable

Posted on 2002-05-12
3
1,456 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 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