permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes How to Set the JTable Rows Color?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Set the JTable Rows Color??" Watch "How to Set the JTable Rows Color??" New topic
Author

How to Set the JTable Rows Color??

daniel xiao
Greenhorn

Joined: Jul 04, 2003
Posts: 12
How to Set the JTable Rows Color??
row1.color is green,row2 color is red ......
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Here's an example of how to do something similiar in a JList... you should be able to make minor changes to have it work in a JTable...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi Nathan
I have same problem and I am trying this way. This is setting table background with Color.cyan color. how can I have alternate rows with diffrent colors. I

class MyTableCellRenderer extends DefaultTableCellRenderer//implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int col)
{
Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
//setBackground((Color)value);
if(isSelected)
{
c.setBackground(Color.GRAY);
}
else
{
c.setBackground(Color.cyan);
}
return c;
}
}


Raghuveer Rawat<br />SCJP2
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Did you follow the link... :roll:

If you did, you would have seen the following renderer in the code :



Replace Color.yellow with Color.cyan and add a check for being selected to get what you want...
[ July 07, 2003: Message edited by: Nathan Pruett ]
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi Nathan
I could not get what you are saying. This is a code for JList and I require JTable where I do not have index than how can I get alternate colored rows.
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi Nathan
I have one another problem.
I have one Combobox and a JTable. JCombobox contains table names. when user select a table from the combox box respecting table is shown in the JTable.
I have put this JTable in the JScrollPane using
JTable tabel = new JTable();
JScrollPane pane = new JScrollPane(table)
When User select a table from the combox this method is called.
void jComboBox1_actionPerformed(ActionEvent e)
{
Object combo= e.getSource();
String selectedTable="";
// This will redraw the table when user changes selection in the combo box
if(combo instanceof JComboBox)
{
JComboBox jcb = (JComboBox) combo;
//this will return selected table in the combo box
selectedTable = (String)jcb.getSelectedItem();
System.out.println("selected table:"+selectedTable);
// this will return array list containg table column array and values array
ArrayList tablaData = new Customer().getTableData(selectedTable);
String tableColumn[] = (String[]) tablaData.get(0);
System.out.print("column list:"+tableColumn.length);
Object data[][] = (Object[][]) tablaData.get(1);
System.out.print("row list:"+data.length);
jTable1 = new JTable(new MyTableModel(data, tableColumn));
JTableHeader header = jTable1.getTableHeader();
header.setDefaultRenderer(new MyHeaderRenderer());
for(int i=0;i<tableColumn.length;i++)
{
//System.out.println("column: "+tableColumn.length);
TableColumn column = (TableColumn) jTable1.getColumn(tableColumn[i]);
header.setResizingColumn(column);
column.setResizable(true);
int width= column.getWidth();
column.setMinWidth(width);
column.setCellRenderer(new MyTableCellRenderer());
}
pane.setViewportView(jTable1);
}
My problem is that table is refreshed when I make a new selection from the combo but these table are too wide to come in the scrollpane. I mean only few column are displaying others are missing.
I donot know what is the problem when I am creating ScorllPane like this
JScrollPane pane = new JScrollPane(table);
both Horizontal and Vertical Scroll should be present.
Thanx
daniel xiao
Greenhorn

Joined: Jul 04, 2003
Posts: 12
Thanks,I have solve!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Originally posted by raghuveer rawat:
hi Nathan
I could not get what you are saying. This is a code for JList and I require JTable where I do not have index than how can I get alternate colored rows.

Change it to extend DefaultTableCellRenderer instead of DefaultListCellRenderer, and change the method to match the table cell renderer method. Instead of using "index", you will use the "row" parameter.

Not sure about your ComboBox/Table problem, but it might be this part...


The default value of getWidth() on the TableColumn is 75... maybe this value is too big?

Also, try using setPreferredScrollableViewportSize() on the table... this will tell the scrollpane it's preferred size when it is holding the table.
 
jQuery in Action, 2nd edition
 
subject: How to Set the JTable Rows Color??