Click to See Complete Forum and Search --> : problem getting an ItemListener of a JComboBox to work..


hoping4jannah
04-10-2006, 03:27 PM
Hello there everyone! I'm back with another problem! :D
It's just a simple GUI event handling issue..

Let me give you the picture.

There's a panel, which is made up of three other panels. Say, panel1, panel2, and panel3.

panel1 also has a JComboBox in it. If i change its value, I want the panel3 to change. Here's the ItemListener..

public void itemStateChanged(ItemEvent e)
{
JComboBox cb = (JComboBox)e.getItem();

if(cb.getSelectedItem().equals("House"))
{
propPanel.removeAll();
propPanel.add(topPanel, BorderLayout.NORTH);
propPanel.add(commonPropertyPanel, BorderLayout.CENTER);
propPanel.add(housePanel, BorderLayout.SOUTH);
}

if(cb.getSelectedItem().equals("Appartment"))
{
propPanel.removeAll();
propPanel.add(topPanel, BorderLayout.NORTH);
propPanel.add(commonPropertyPanel, BorderLayout.CENTER);
propPanel.add(apptPanel, BorderLayout.SOUTH);
}
}

..you get the idea. Please tell me if there's anything wrong with this above piece of code. It gives me these errors:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String
at RealEstate.itemStateChanged(RealEstate.java:509)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1162)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1210)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1266)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:551)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:597)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:808)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:232)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:476)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I'm using BLUEJ. My package/class is called RealEstate. Thanks in advance.

Khalid Ali
04-10-2006, 07:24 PM
you are getting error at the following line (my guess in absence of enough description)

JComboBox cb = (JComboBox)e.getItem();

Now what you need to do here is make sure that ur rest of the code(if and else block) only happens if the event triggerer was JCombobox. Its very simple, here is what u need to do, surround all of ur event handling code in the following inf statement

if(e.getItem() instanceof JComboBax){
//ur event handling routine here
}

hoping4jannah
04-11-2006, 10:37 AM
You were right! I put in an if else block, and apparently the event trigger isnt a JCheckBox.. but hows that possible?

This is my creation of the checkbox..

private final String[] propertyChoice = {"House","Appartment","Shop","Office","Garage"};
private JComboBox type = new JComboBox(propertyChoice);

this part is outside the constructor or any method.. and I've added the ItemListener inside a method which sets up part of the GUI..

private void setupTopPanel()
{
.....
topPanel.add(type);
type.addItemListener(this);
......
}

and this is exactly the thing which triggers the event.. Are there any details I should provide to help you point out the error? Thanks a lot..