dcsimg
JTable Cell Renderer
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Trex_Lim
Posted On:   Sunday, April 6, 2003 11:42 PM

Anyone who may advice: I am currently developing a JApplet with JComboBox and JTable within it. When the table first loaded, it will just display the employee data from database. However, when user select a department no from the JComboBox, comparison will be made between the department no of each employee in the table and the one selected from the combo box. I would like to know how to highlight the row of employee data having different department no from the selected one. I tried to use a custom cell renderer. However, it color the whole column. Is there a way to just color a particular cell? The code for the custom cell render is as follow: class Color   More>>

Anyone who may advice:

I am currently developing a JApplet with JComboBox and JTable within it. When the table first loaded, it will just display the employee data from database. However, when user select a department no from the JComboBox, comparison will be made between the department no of each employee in the table and the one selected from the combo box.



I would like to know how to highlight the row of employee data having different department no from the selected one.



I tried to use a custom cell renderer. However, it color the whole column. Is there a way to just color a particular cell?



The code for the custom cell render is as follow:

			
class ColorColumnRenderer extends DefaultTableCellRenderer {
Color bgColor, fgColor;
String deptNo;

public ColorColumnRenderer(Color bg, Color fg,
String dno) {
super();

bgColor = bg;
fgColor = fg;
deptNo = dno;
}

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);

for(int i = 0; i < table.getRowCount(); i++) {
if (!deptNo.equals(table.getValueAt(i,3))) {
System.out.println("Different");
cell.setBackground(bgColor);
cell.setForeground(fgColor);
}
}

return cell;
}
}



At the event listener of the combo box, I added the following statements:

			
TableColumn tm = table.getColumnModel().getColumn(3); // Column having department short name
tm.setCellRenderer(new ColorColumnRenderer(Color.lightGray, Color.blue, stvo.getDepartmentSName()));




Please advice. Thanks    <<Less

Re: JTable Cell Renderer

Posted By:   Bahman_Barzideh  
Posted On:   Monday, April 7, 2003 07:08 AM

You are using CellRenderer incorrectly. A cell renderer's getTableCellRendererComponent method is
called by the system when it needs to paint a given cell. The input
parameters tell you what cell is about to be painted (table, row and column arguments). and what the
vaue should be (the value argument). My guess is you need
to remove the for loop you have and replace it with some
logic that decides whether this cell is to have an alternate color set.
How you do this depends on your application. May be you wold take
the row and look it up in the data model. or may be you can tell from
the value. But either way you should only be setting the colors for a
single cell (the one identified by row and column argument. The loop you have herer will simply print different a bunch of times and set the background and
foreground of the same cell (not necessarily the right one) a bunch of times.
About | Sitemap | Contact