I have a JComboBox in a JTable and am looking at the getTableCellRendererComponent documentation which explains the parameters.
table - the JTable that is asking the renderer to draw; can be null
value - the value of the cell to be rendered. It is up to the specific renderer to interpret and draw the value. For example, if value is the string "true", it could be rendered as a string or it could be rendered as a check box that is checked. null is a valid value
isSelected - true if the cell is to be rendered with the selection highlighted; otherwise false
hasFocus - if true, render cell appropriately. For example, put a special border on the cell, if the cell can be edited, render in the color used to indicate editing
row - the row index of the cell being drawn. When drawing the header, the value of row is -1
column - the column index of the cell being drawn
My confusion is "value" and "isSelected". If the "value" is to be rendered how could "isSelected" ever be false? If false why is "value" being rendered since it is not selected? What would be rendered? TIA.
Update after camickr's clarification and some experimentation
Apparently I only partially understand what is happening and it has presented me with a problem. When a JComboBox selection is made, the content of "value" is the selected item rather than the JComboBox instance. Thus I no longer have the instance of the JComboBox to render. I also do not see a method for "table" that lets me get the component in the current cell. How do I get the JComboBox instance so the box is rendered properly in that cell? As it is when a selection is made the JComboBox disappears and I get a runtime error for cases 2,5,6,7 which makes sense since value is now a string rather than a JComboBox instance. TIA.
public class TimelineCellRenderer implements TableCellRenderer {
@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(JTable table_, Object value_, boolean isSelected_, boolean hasFocus_, int row_,int column_) {
Component field=null;
String str="";
if (value_!=null) {
str=value_.toString();
}
switch (column_) {
case 0:
case 3:
case 4:
case 8:
field=new JTextField();
((JTextField) field).setText(str);
break;
case 1:
field=new JTextField();
((JTextField) field).setText(Double.toString((Double) value_));
break;
case 2:
case 5:
case 6:
case 7:
field=(JComboBox<String>) value_;
break;
case 9:
field=new JTextField();
((JTextField) field).setText("Add button");
break;
case 10:
field=new JTextField();
((JTextField) field).setText("del button");
break;
}
if (field instanceof JTextField) {
Font f=field.getFont().deriveFont(Font.PLAIN, (float) 14);
field.setFont(f);
}
return(field);
}
}
Whenever you click on a cell the selected row changes.
So every cell in the row needs to be rendered since the row highlighting needs to change.
Within the row, only a single cell can be selected an any one time.
Also, all the cells of the previously selected row need to be repainted without highlighting.
So the basic answer is that the method is called multiple times, once for each cell and the parameters will be different.