Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

color row in JTable

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 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