This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JCombo Box gives IllegalStateException while selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCombo Box gives IllegalStateException while selection" Watch "JCombo Box gives IllegalStateException while selection" New topic
Author

JCombo Box gives IllegalStateException while selection

ashok rajendran
Greenhorn

Joined: Mar 31, 2008
Posts: 5
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;
}
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ashok rajendran:
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.


As per the bug report you mention, you could call
mEditorComp.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE). This should prevent the IllegalComponentStateException, but I must confess I don't quite understand what you are trying to do here. Wouldn't your code work the same if you didn't even add the KeyListener? (With the LnF I'm using, both ENTER and SPACE will close the combo box's popup already.) Perhaps this has something to do with the workaround you aren't showing us??

two other things while I'm here:
1) It's pointless (but harmless) to call super.keyPressed(event) in your KeyListener.
2) I would prefer to see your ComboEditor class instantiate one JComboBox and reuse it, rather than instantiate a new one on each call to getTableCellEditorComponent().


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCombo Box gives IllegalStateException while selection
 
Similar Threads
Focus traversal problem in JTable
JTable selected row problem
ctrl+tab is not working in editing mode
JComboBox in a JTable
JComboBox in a JTable Cell