Solved

changing a color in one row of JTable

Posted on 2002-07-24
14
3,348 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
  • 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
 
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
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.

 
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 150 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

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

Title # Comments Views Activity
JAVA part two 5 57
jar file executable 12 38
Spring Framework HTTPSession management 1 22
couple of eclipse 5 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

914 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

17 Experts available now in Live!

Get 1:1 Help Now