• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3372
  • Last Modified:

changing a color in one row of JTable

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
cicurel
Asked:
cicurel
  • 7
  • 4
  • 2
  • +1
1 Solution
 
objectsCommented:
You need to define a custom cell renderer.
0
 
objectsCommented:
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
 
rjackmanCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ksivananthCommented:
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
 
cicurelAuthor Commented:
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
 
ksivananthCommented:
>>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
 
objectsCommented:
> 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
 
objectsCommented:
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
 
cicurelAuthor Commented:
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
 
objectsCommented:
The correct line is:

yourTable.setDefaultRenderer(
   yourTable.getColumnModel().getColumn( i ),
   new MyTableCellRenderer() ) ;
0
 
cicurelAuthor Commented:
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
 
objectsCommented:
Sorry you need to specify the column class to use the renderer for, not the column:

yourTable.setDefaultRenderer(
  Object.class,
  new MyTableCellRenderer() ) ;
0
 
cicurelAuthor Commented:
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
 
objectsCommented:
Happy to help :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now