Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Adding another related combo box ?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding another related combo box ?

    Hi,

    I have a question. How can I add a third combo box to this program which will do the same thing the second combo box does with the first. I mean I know how to add the combo box but I want the third combo box to depend on the action of the second combo box. As you can see the method that controls the action of the first and the second combo box operates by if/else, and if I did the same thing for the third combo box, then it would be a mile long code?

    So please help me think of another way of making this method, or post an example similar to this but with more combo boxes. Thank you.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Compare extends JFrame implements ItemListener {
            JComboBox combo, lcombo;
     
            public Compare() {
                    setLayout(null);
                    String a[] = { "Select", "Watches", "Mobiles", "shoes" };
                    combo = new JComboBox(a);
                    combo.setBounds(50, 50, 100, 20);
                    combo.addItemListener(this);
                    add(combo);
     
                    lcombo = new JComboBox();
                    lcombo.addItemListener(this);
                    lcombo.setEnabled(false);
                    add(lcombo);
                    lcombo.setBounds(50, 100, 100, 20);
     
                    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding another related combo box ?

    You could use a series of Maps instead of a series of arrays.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. java servlet related
    By vidi in forum Member Introductions
    Replies: 0
    Last Post: April 16th, 2011, 10:26 PM
  2. Socket related prog
    By amitabh in forum Java Networking
    Replies: 1
    Last Post: March 18th, 2011, 08:47 AM
  3. [SOLVED] Array related assignment
    By Melawe in forum Java Theory & Questions
    Replies: 36
    Last Post: July 21st, 2010, 03:22 AM
  4. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  5. Loading contents of a Database into a Combo box
    By tyolu in forum JDBC & Databases
    Replies: 3
    Last Post: June 22nd, 2009, 08:14 AM