I have a simple JTable in my application. When the user clicks on a cell to edit the value a Jcombo box should appear. This combo Box is created in a table cell editor.
I have to stop the cell editing (Since my original application has some rules to execute on the "change value" event of the table.) when the user selects a value from the combo box. Since
Java swing1.5 does not provide any api to catch the selection event (bug filed in jdk. bugid - 4199622), I am doing some workaround to catch the event using keyListener and ItemListener. (I haven't given the complete work around here, since it may confuse people.)
In the key event listener, I stop the cell editing for the key entries ENTER and SPACE bar. (Both of these are considered as a selection key in swing.) When i press ENTER key to select an item from the drop down, it works well. When I press the SPACE bar to select an item it throws
IllegalStateException . Exception is as follows.
Exception in
thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location.
Any help on tis will be really appreciated.
Here is my sample code... After running the sample code, press F2 or click on the cell to get the drop down box..
// Use this as a main class
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Frame1 extends JFrame {
public Frame1() {
super();
this.setLayout( null );
this.setSize( new Dimension(400, 300) );
DefaultTableModel tmodel = new DefaultTableModel(3, 1);
tmodel.setValueAt("0 0 1",0,0);
tmodel.setValueAt("1 0 1",1,0);
tmodel.setValueAt("2 0 1",2,0);
JTable custLayersTable = new JTable(tmodel);
custLayersTable.getColumnModel().getColumn(0).
setCellEditor(new ComboEditor());
custLayersTable.setBounds(new Rectangle(40, 40, 280, 145));
this.add(custLayersTable, null);
}
public static void main(
String[] args)
{
Frame1 a = new Frame1();
a.setVisible(true);
}
}
//Editor class
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
final class ComboEditor extends AbstractCellEditor
implements TableCellEditor
{
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
Vector<String> layerValSet = new Vector<String>();
for(int i=0; i<3; i++)
layerValSet.add(row+" "+column+" "+i);
mComboModel = new DefaultComboBoxModel(layerValSet);
mComboModel.setSelectedItem(value);
mEditorComp = new JComboBox(mComboModel);
mEditorComp.addKeyListener(new KeyAdapter() {
public void keyPressed( KeyEvent event ) {
super.keyPressed(event);
if((event.getKeyCode() == KeyEvent.VK_ENTER ||
event.getKeyCode() == KeyEvent.VK_SPACE) &&
mEditorComp.isPopupVisible())
{
stopCellEditing();
}
}
});
return mEditorComp;
}
public Object getCellEditorValue()
{
return mEditorComp.getSelectedItem();
}
private DefaultComboBoxModel mComboModel;
private JComboBox mEditorComp;
}