Solved

JTable: How do I paint a cell red? or green?

Posted on 2004-10-13
6
1,279 Views
Last Modified: 2010-05-18
How do I set the background color of a cell in a JTable?
0
Comment
Question by:ycomp
6 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 500 total points
ID: 12301725
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301768
2 solutions:

1. In your subclass of JTable include the code looking something like this:

 public Component prepareRenderer(TableCellRenderer renderer,
                                         int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
                c.setBackground(Color.red);
            } else {
                // If not shaded, match the table's background
                c.setBackground(getBackground());
            }
            return c;
        }

This will color every second column red.
Advantage: Works for every kind of cell renderer (Text, number, date,
custom renderes etc.).
Disadvantage: You have to subclass JTable

2. Make your own version of a TableCellRenderer and put the logic in
there.

Advantage: You do not need to subclass JTable
Disadvantage: It will only change the color for those cells which have
your cellrenderer. e.g. you need to manually install them.
0
 

Author Comment

by:ycomp
ID: 12301796
Thanks sudhakar, unfortunately I just accept manuel's answer (his was good too). Should have split the points but I didn't see yours until after I accepted.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301803
Here is thre model to writeCellREnderrer class

 JTable table = new JTable();
    // Add data...
   
    // Install the custom renderer on the first visible column
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());
   
    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // This method is called each time a cell in a column
        // using this renderer needs to be rendered.
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            // 'value' is value contained in the cell located at
            // (rowIndex, vColIndex)
   
            if (isSelected) {
                // cell (and perhaps other cells) are selected
            }
   
            if (hasFocus) {
                // this cell is the anchor and the table has the focus
            }
   
            // Configure the component with the specified value
            setText(value.toString());
   
            // Set tool tip if desired
            setToolTipText((String)value);
   
            // Since the renderer is a component, return itself
            return this;
        }
   
        // The following methods override the defaults for performance reasons
        public void validate() {}
        public void revalidate() {}
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12301819
Oh, I am late. No problems ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12303683
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 71
Java DateChooser? 3 29
servlet doXXX methods 3 38
expectj telnet failing 5 26
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

867 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

13 Experts available now in Live!

Get 1:1 Help Now