Hello. I'm trying to understand how I can take this combo box example a level deeper. The way that it works now is that the 2nd combo box will change its options based on whatever is selected from the first one. I'm wanting a 3rd combo box to change its options based on what is selected from the second. Hopefully that makes sense. I'm still in my first month of learning Java.

You have already added an ItemListener to all 3 of your JComboBoxes, however, the only time an action will be taken is if combo is the JComboBox being changed. That is because of this section in your code:

Code:

if(e.getSource() == combo){
//your code
}

What that basically says is get me the source from the ItemEvent(the component that triggered the event to be fired), and if it is combo, do something.
So what you need to do is add another few if statements beneath that to handle the case where the other 2 combo boxes are changed. That is, when e.getSource() is 1combo, or 2combo.

You have the listeners setup, but your itemChanged method is only concerned (so far) with when combo is changed.

Thanks for the response. I'm still having some issues trying to piece this together. In my revised code. i'm getting some errors when i play around with the first combo box. I am sure there is a step i'm leaving out or something I've overlooked.

In my program, when i select "watches" in the first combo box, "new" and "used" show up in combo box 3, however combo box 2 is empty, and I get a lot of errors in the console of my IDE.
The console errors says the following:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Compare.itemStateChanged(Compare.java:63)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.JComboBox.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved( Unknown Source)
at javax.swing.DefaultComboBoxModel.removeAllElements (Unknown Source)
at javax.swing.JComboBox.removeAllItems(Unknown Source)
at Compare.itemStateChanged(Compare.java:44)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged( Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(U nknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mou seReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMo useEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

04-04-2012, 08:36 PM

sehudson

Re: Taking this example further (Compare class)

The error points you right to the problem. Take a look at line 63
You need to use the == comparison operator.

Code:

if (lcombo.getSelectedItem() == null){
//your code
}

Otherwise, you will get a null pointer for trying to call .equals() on a null object, that being the selected item.