Solved

setting the background of a particular cell in JTable

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
sumHeights2  challenge 7 75
for i loop in grovy 1 32
Java asynchronous logging 4 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 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

14 Experts available now in Live!

Get 1:1 Help Now