Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

changing a color in one row of JTable

Posted on 2002-07-24
14
Medium Priority
?
3,364 Views
Last Modified: 2012-06-21
Hi.

I am using a JTable, and every couple minutes i add another line to it by using:
DefaultTableModel model = new DefaultTableModel(){
        public boolean isCellEditable(int rowIndex, int mColIndex) {
            return false;
        }
    };
  JTable table = new JTable(model);

but i need some lines of it to be in a different color.
is there a way to select a single line and change it's color?
or just add it in a different color..

Thanks.
0
Comment
Question by:cicurel
[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
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7176043
You need to define a custom cell renderer.
0
 
LVL 92

Expert Comment

by:objects
ID: 7176051
Something like:

class MyTableCellRenderer extends DefaultTableCellRenderer {
 public Component getTableCellRendererComponent(
  JTable table, Object value, boolean isSelected,
  boolean hasFocus, int row, int column) {
   Component result = super.getTableCellRendererComponent(
     table, value, isSelected, hasFocus, row, coloumn);
   Color bg = ....; // determine appropriate bg colour
   setBackground(bg);
   return result;
 }
}
0
 
LVL 3

Expert Comment

by:rjackman
ID: 7176280
hi
cicurel
pls have a look at following site got a example which might help u
http://www2.gol.com/users/tame/swing/examples/JTableExamples4.html

cheers
RJ
0
Independent Software Vendors: 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!

 
LVL 26

Expert Comment

by:ksivananth
ID: 7176368
Hi,

  A small change in Object's code fragment,

class MyTableCellRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(
  JTable table, Object value, boolean isSelected,
  boolean hasFocus, int row, int column)
  {
    Color bg = ....; // determine appropriate bg colour
    if( row == "your row" )
      setBackground(bg);
    return this;
  }
}
// set the following for each column in the table
yourTable.setDefaultCellRenderer( column,
            new MyTableCellRenderer() ) ;

thank u
siva
0
 

Author Comment

by:cicurel
ID: 7176691
Thanks for your replies, but , sorry, i am a bit newbie at java:
few questions..

yourTable.setDefaultCellRenderer( column,
           new MyTableCellRenderer() ) ;

the column is supposed to be "Class column" ?! how exactly do i define it?

and i added to ksivananth's code a method.
public void setColor(Color color)

in order to determine each line in which color to paint.

((MyTableCellRenderer) table.getCellRenderer(table.getRowCount() - 1 ,0)).getTableCellRendererComponent ( table, args[1], false, false, table.getRowCount() -1, 0);

which the args[1] is what i want to put in this cell, and it's the last cell of the table (since adding a cell to the model each time)

Thanks alot.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 7176888
>>the column is supposed to be "Class column" ?! how >>exactly do i define it?

  Set the renderer for all the columns. You can get the columns by, "table.getColumnModel().getColumn(2)"

  Then the other things that you asked I could not understand. But I have added some comments for the code, so you can understand.

class MyTableCellRenderer extends DefaultTableCellRenderer {
 public Component getTableCellRendererComponent(
 JTable table, Object value, boolean isSelected,
 boolean hasFocus, int row, int column)
 {
// assume you want to set blue color for the 5th row
   Color bg = Color.blue ; // determine appropriate  
                           //bg colour
   if( row == 5 )
     setBackground(bg); // set the blue color for 5th row
   return this;
 }
}

for( int i = 0; i < yourTable.getColumnCount(); i++ )
{
  yourTable.setDefaultCellRenderer(
     table.getColumnModel().getColumn( i ),
     new MyTableCellRenderer() ) ;
}

  If you just done the above things, the 5th row in your table will be displayed in blue color.

Hope this helps!
Still if you have q's please let me know!

thank u
siva
0
 
LVL 92

Expert Comment

by:objects
ID: 7178667
> if( row == "your row" )

My intention was that this woulod be taken into account when setting the value of bg. So this is not necessary, and also a bit limiting.
0
 
LVL 92

Expert Comment

by:objects
ID: 7178685
Following adds a method allowing you to set the colour for a particular row:

class MyTableCellRenderer extends DefaultTableCellRenderer {
 private Hashtable Colors = new Hashtable();

 public Component getTableCellRendererComponent(
  JTable table, Object value, boolean isSelected,
  boolean hasFocus, int row, int column)
  {
   Component result = super.getTableCellRendererComponent(
     table, value, isSelected, hasFocus, row, coloumn);
   Color bg = getRowColor(row); // determine appropriate bg colour
   if (bg!=null)
   {
     setBackground(bg);
   }
   return result;
 }

 public void setRowColor(int row, Color c)
 {
   Colors.put(new Integer(row), c);
 }

 private getRowColor(int row)
 {
   return (Color) Colors.get(new Integer(row));
 }
}
0
 

Author Comment

by:cicurel
ID: 7182761
Hi.

Sorry for the late respond.
i understood everything, but the one line which is not working for me is...

 yourTable.setDefaultCellRenderer(
    table.getColumnModel().getColumn( i ),
    new MyTableCellRenderer() ) ;

please help

Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 7182766
The correct line is:

yourTable.setDefaultRenderer(
   yourTable.getColumnModel().getColumn( i ),
   new MyTableCellRenderer() ) ;
0
 

Author Comment

by:cicurel
ID: 7182808
i know, i keep getting the error

TempTable.java:32: setDefaultRenderer(java.lang.Class,javax.swing.table.TableCellRenderer) in javax.swing.JTable cannot be applied to (javax.swing.table.TableColumn,MyTableCellRenderer)
0
 
LVL 92

Accepted Solution

by:
objects earned 600 total points
ID: 7182932
Sorry you need to specify the column class to use the renderer for, not the column:

yourTable.setDefaultRenderer(
  Object.class,
  new MyTableCellRenderer() ) ;
0
 

Author Comment

by:cicurel
ID: 7183128
Thanks alot for your comments, it works like a charm :)

The Object.class bothered me, i tried String.class before and it didnt work, forgot about the Object itself ;)

Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 7184163
Happy to help :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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