Solved

setting the background of a particular cell in JTable

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

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

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

20 Experts available now in Live!

Get 1:1 Help Now