Solved

setting the background of a particular cell in JTable

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

724 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