Solved

color row in JTable

Posted on 2002-05-12
3
1,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

689 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