?
Solved

color row in JTable

Posted on 2002-05-12
3
Medium Priority
?
1,459 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 600 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

Industry Leaders: 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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