• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JCombo Box gives IllegalStateException while selection

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic