Solved

changing a color in one row of JTable

Posted on 2002-07-24
14
3,342 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 64
IT Company 5 69
strDist challenge 35 84
DO we need Java installed on a Windows PC and WHY ? 13 78
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 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