Solved

setting the background of a particular cell in JTable

Posted on 2000-04-17
3
479 Views
Last Modified: 2012-05-04
Hi all,
Actually I am using JTable. I want to show some alarm information through a JTable object. I want to set the background of individual cells corresponding to the alarm severity. Some information will also be stored on the foreground. That's why I want to set the background color of the individual cells .
Thanks in adv.
anand
0
Comment
Question by:anandshekhar
3 Comments
 
LVL 1

Expert Comment

by:yogeshr
ID: 2722475
Pls search in the following url as it contains some examples for setting the background and foreground of individual cells:
http://www2.gol.com/users/tame/swing/examples/JTableExamples4.html

0
 
LVL 3

Accepted Solution

by:
ovidiucraciun earned 50 total points
ID: 2722624
// Look hear an example that can set
// foreground color of a cell in red
// depending of YOUR_CONDITION
// in the same way you can change the background color

public class MyTableCellRenderer extends DefaultTableCellRenderer
{
      public Component getTableCellRendererComponent( JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column)      
      {
            JLabel normal = new JLabel( );
            JLabel extra = new JLabel( );
            extra.setForeground( Color.red );
            extra.setText( (String)value );
            normal.setText( (String)value );
            if ( YOUR_CONDITION )
            {
                  return extra;
            }
            else
            {
                  return normal;
            }
      }
}
0
 

Expert Comment

by:anloleo
ID: 5456427
The code set the foreground color into the cell!!! How it's possible to set the background? Using JLabel as rendering component it's not possible!!!!
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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