Needs double click to edit cell in JTable
I know this is bad design but there's no other way that I could possibly meet the requirement which is to have dynamic components. Anyhow, the requirement is to have any kind of components(e.g. TextFields, Combobox, Regular expression fields, a panel with a number of checkboxes) in a table cell in the same column. It would have been easier to do this by using the the DefaultCellEditor, with the 'panel containing a number of checkboxes' I cannot use it.
I already have an implementation for this requirement. There was no problem with it when we used Java 1.5. But when we shifted to Java 1.6, I noticed that I need to double-click on a cell so that I can edit it. I did not notice this behavior at all with 1.5. What could have changed in 1.6?
Below, are my (trimmed-down) codes:
//TestComponent.java
public class TestComponent extends JPanel{
private int componentHeight=16;
public TestComponent(int i){
this.setPreferredSize(new Dimension(90, this.componentHeight));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
switch (i){
case 0: createTextField(); break;
case 1: createCheckBox(); break;
case 2: createCombo(); break;
}
}
private void createTextField(){
String text = null;
int textFieldMaxLength = 5;
JTextField oText = new JTextField(textFieldMaxLength);
// add it to this Panel:
this.add(oText);
// set the data:
text = "test";
oText.setText(text);
// set size for this TextField:
Dimension fieldSize = new Dimension(
textFieldMaxLength * 13, this.componentHeight);
oText.setPreferredSize(fieldSize);
oText.setMinimumSize(fieldSize);
oText.setMaximumSize(fieldSize);
}
private void createCheckBox(){
JCheckBox chkbox = new JCheckBox();
this.add(chkbox);
}
private void createCombo(){
JComboBox combo = new JComboBox(new String[]{"apple", "orange", "plum", "grapefruit"});
Dimension fieldSize = new Dimension(5 * 13, this.componentHeight);
combo.setPreferredSize(fieldSize);
combo.setMinimumSize(fieldSize);
combo.setMaximumSize(fieldSize);
this.add(combo);
}
}
//ComponentCellEditor.java
public class ComponentCellEditor extends AbstractCellEditor
implements TableCellEditor, Serializable{
protected JComponent editorComponent = null;
public Component getComponent() {
return editorComponent;
}
public Object getCellEditorValue() {
return editorComponent;
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
//
// Implementing the TreeCellEditor Interface
//
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
this.editorComponent = (TestComponent)value;
return editorComponent;
}
}
//ComponentCellRenderer.java
public class ComponentCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
TestComponent oComp = (TestComponent) value;
if (isSelected) {
oComp.setForeground(table.getSelectionForeground());
oComp.setBackground(table.getSelectionBackground());
} else {
oComp.setForeground(table.getForeground());
oComp.setBackground(table.getBackground());
}
return oComp;
}
}
//TestTable.java
public class TestTable
{
public static void main(String []args){
String columns[] = {"Text", "Value"};
Class types[] = {String.class, TestComponent.class};
boolean editable[] = {false, true};
AsrTable table = new AsrTable(new Dimension(200, 150), columns, types, editable);
table.addRow(new Object[]{"Field1", new TestComponent(0)});
table.addRow(new Object[]{"Field2", new TestComponent(1)});
table.addRow(new Object[]{"Field3", new TestComponent(2)});
table.setDefaultEditor(TestComponent.class, new ComponentCellEditor());
table.setDefaultRenderer(TestComponent.class, new ComponentCellRenderer());
table.setShowGrid(false);
table.setRowHeight(20);
table.setRowSelectionAllowed(false);
JFrame frame = new JFrame();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}
} );
JScrollPane pane = new JScrollPane(table);
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
}
}

