This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JTable Cell Renderer

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTable to which i intend to set the background color for every alternative row (irrespective of the values in each cell). I know the mehod for setting the background (of a cell alone) based on some condition present in each cell. But what i want to do is to set the colour of every cell (ie a row) . Could somebody please help me with this.

Thanks.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Write your own renderer for the table in which you can write following code (code that set color red ,black alternately)


class MyTableRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(row%2 == 0)
{
cell.setBackground(Color.red);
}
else
{
cell.setBackground(Color.black);
}
return cell;
}
}
Regards,
Anand
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is something wrong with this. Its not working.



Any idea how to instantiate the class MyTableRenderer ?
 
Anand Loni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just try this...

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
//import layouts.GridPanel;

public class TableTest extends GridPanel
{
final String[] colHeads =
{ "SNo", "col1", "col2", "col3", "col4" };

final Object[][] data =
{
{ "1", "a", "d", "b", "c" },
{ "2", "a", "d", "b", "c" },
{ "3", "a", "d", "b", "c" },
{ "4", "a", "b", "c", "d" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" } };


TableTest()
{
super();
JTable jtbl = new JTable(data, colHeads);
JScrollPane jsp = new JScrollPane(jtbl);
jtbl.setDefaultRenderer(Object.class, new MyTableRenderer());

this.setSize(640, 600);
this.setBackground(new Color(225, 232, 219));
jsp.setViewportView(jtbl);// table on scroll pane
this.addFilledComponent(jsp, 1, 1, 5, 2, GridBagConstraints.BOTH);
}// constructor

class MyTableRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row % 2 == 0)
{
cell.setBackground(Color.red);
}
else
{
cell.setBackground(Color.black);
}
return cell;
}
}
}
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning Anand.

Thanks a lot for pointing out that bug.
jtbl.setDefaultRenderer(Object.class, new MyTableRenderer());
I messed up this line completely.

Have a great day !!!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!