Solved

setting the background of a particular cell in JTable

Posted on 2000-04-17
3
476 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

810 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