Results 1 to 8 of 8
  1. #1
    tomjw is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Taking this example further (Compare class)

    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.


    Java Code:
    import java.awt.*;
     
    public class Compare extends JFrame implements ItemListener {
            JComboBox combo, lcombo, l2combo;
     
            public Compare() {
                    getContentPane().setLayout(null);
                    String a[] = { "Select", "Watches", "Mobiles", "shoes" };
                    combo = new JComboBox(a);
                    combo.setBounds(50, 50, 100, 20);
                    combo.addItemListener(this);
                    getContentPane().add(combo);
     
                    lcombo = new JComboBox();
                    lcombo.addItemListener(this);
                    lcombo.setEnabled(false);
                    getContentPane().add(lcombo);
                    lcombo.setBounds(50, 100, 100, 20);
                    
                    l2combo = new JComboBox();
                    l2combo.addItemListener(this);
                    l2combo.setEnabled(false);
                    l2combo.setBounds(50, 157, 100, 20);
                    getContentPane().add(l2combo);
     
                    setSize(300, 300);
            }
     
            public void itemStateChanged(ItemEvent e) {
                    String b[] = { "Titan", "HMT" };
                    String c[] = { "Nokia", "Sony", "Motorola", "Samsung" };
                    String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" };
                    
     
                    if (e.getSource() == combo) {
                            if (combo.getSelectedItem().equals("Select")) {
                                    lcombo.setEnabled(false);
                            } else if (combo.getSelectedItem().equals("Watches")) {
                                    lcombo.setEnabled(true);
                                    lcombo.removeAllItems();
                                    for (int i = 0; i < b.length; i++) {
                                            lcombo.addItem(b[i]);
                                            
                                         
                                    }} else if (combo.getSelectedItem().equals("Mobiles")) {
                                    lcombo.setEnabled(true);
                                    lcombo.removeAllItems();
                                    for (int i = 0; i < c.length; i++) {
                                            lcombo.removeItem(c[i]);
                                            lcombo.addItem(c[i]);
                                    }
                            } else if (combo.getSelectedItem().equals("shoes")) {
                                    lcombo.setEnabled(true);
                                    lcombo.removeAllItems();
                                    for (int i = 0; i < d.length; i++) {
                                            lcombo.addItem(d[i]);
                                    }
                            }
                    }
            }
     
            
            
            public static void main(String args[]) {
                    (new Compare()).setVisible(true);
            }
    }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: Taking this example further (Compare class)

    You have a pretty good start.

    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:

    Java 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.

    Java Code:
    else if(e.getSource() == 1combo)
    {
    //do this when the 1combo box selection is changed
    }
    else if(e.getSource() == 2combo)
    {
    //do this when the 2combo box selection is changed
    }
    Last edited by sehudson; 04-04-2012 at 07:22 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Taking this example further (Compare class)

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    tomjw is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Taking this example further (Compare class)

    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.

    Java Code:
    import java.awt.*;
     
    public class Compare extends JFrame implements ItemListener {
            JComboBox combo, lcombo, l2combo;
     
            public Compare() {
                    getContentPane().setLayout(null);
                    String a[] = { "Select", "Watches", "Mobiles", "shoes" };
                    combo = new JComboBox(a);
                    combo.setBounds(50, 50, 100, 20);
                    combo.addItemListener(this);
                    getContentPane().add(combo);
     
                    lcombo = new JComboBox();
                    lcombo.addItemListener(this);
                    lcombo.setEnabled(false);
                    getContentPane().add(lcombo);
                    lcombo.setBounds(50, 100, 100, 20);
                    
                    l2combo = new JComboBox();
                    l2combo.addItemListener(this);
                    l2combo.setEnabled(false);
                    l2combo.setBounds(50, 157, 100, 20);
                    getContentPane().add(l2combo);
     
                    setSize(300, 300);
            }
     
            public void itemStateChanged(ItemEvent e) {
                    String b[] = { "Titan", "HMT" };
                    String c[] = { "Nokia", "Sony", "Motorola", "Samsung" };
                    String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" };
                    
                    
     
                    if (e.getSource() == combo) {
                            if (combo.getSelectedItem().equals("Select")) {
                                    lcombo.setEnabled(false);
                                    } else if (combo.getSelectedItem().equals("Watches")) {
                                    	lcombo.setEnabled(true);
                                    	lcombo.removeAllItems();
                                    	for (int i = 0; i < b.length; i++) {
                                            lcombo.addItem(b[i]);
                                    }        
                                    } else if (combo.getSelectedItem().equals("Mobiles")) {
                                    	lcombo.setEnabled(true);
                                    	lcombo.removeAllItems();
                                    	for (int i = 0; i < c.length; i++) {
                                            lcombo.removeItem(c[i]);
                                            lcombo.addItem(c[i]);
                                    }
                                    } else if (combo.getSelectedItem().equals("shoes")) {
                                    	lcombo.setEnabled(true);
                                    	lcombo.removeAllItems();
                                    	for (int i = 0; i < d.length; i++) {
                                            lcombo.addItem(d[i]);
                                    	}
                                    }
                    } else if (e.getSource() == lcombo) {
                    	if (lcombo.getSelectedItem().equals(null)) {
                    		l2combo.setEnabled(false);
                    	}else if (lcombo.getSelectedItem().equals("Titan")) {
                    		l2combo.setEnabled(true);
                    		l2combo.removeAllItems();
                    		l2combo.addItem("New");
                    		l2combo.addItem("Used");
                    	}
                    }
                   
            } 
          
            
            public static void main(String args[]) {
                    (new Compare()).setVisible(true);
            }
    }

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: Taking this example further (Compare class)

    what does the error say?

  6. #6
    tomjw is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Taking this example further (Compare class)

    Quote Originally Posted by sehudson View Post
    what does the error say?
    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)

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default 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.

    Java 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.
    Last edited by sehudson; 04-04-2012 at 09:40 PM.

  8. #8
    tomjw is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Taking this example further (Compare class)

    Thanks! it works perfectly!

Similar Threads

  1. Compare Abstract Class Values to User input?
    By FadedAura in forum New To Java
    Replies: 16
    Last Post: 11-25-2011, 05:49 PM
  2. Taking input from use
    By leepikamukharji in forum New To Java
    Replies: 1
    Last Post: 11-08-2011, 03:33 PM
  3. Replies: 2
    Last Post: 05-22-2011, 09:56 AM
  4. comparator to compare class year
    By javanub1990 in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 06:45 PM
  5. compare items in Purse class, regardless of order
    By Jojogamer in forum New To Java
    Replies: 0
    Last Post: 03-14-2010, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •