This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Set the JTable Rows Color??

 
daniel xiao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to Set the JTable Rows Color??
row1.color is green,row2 color is red ......
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Raghuveer Rawat
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,I have solve!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic