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 8 of 8

Thread: Populating second combobox from a combobox help!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Populating second combobox from a combobox help!

    Can anyone recommend an Event Listener or some code, that would, when clicking on an item in one jcombobox, populate a different jcombobox.
    The event listener/code would have to be in a separate class to the class containing the jcomboboxes, because I have jbuttons that are extending actionlisteners within that class.

    Thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Populating second combobox from a combobox help!

    How about an ActionListener...

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

    Default Re: Populating second combobox from a combobox help!

    I did try both ActionListener and ListSelectionListener, and neither are working. Would it be easier if I uploaded the code I already have?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Populating second combobox from a combobox help!

    Quote Originally Posted by ComputerSaysNo View Post
    I did try both ActionListener and ListSelectionListener, and neither are working. Would it be easier if I uploaded the code I already have?
    If you want further help on this topic, yes, because otherwise we cannot advise as to why 'neither is working'. And preferably post an SSCCE

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Populating second combobox from a combobox help!

    There are 10 JComboBoxes with five being populated when the program runs. The JComboBoxes work in pairs, so when the user clicks on one of the items in one of populated JComboboxes, the relevant empty Jcombobox will be populated with the subitems of the clicked item.

    Below the code shows the many ways I have tried to sort out this problem. Also, I have only been trialling with the first JComboBox, because I just need to repeat the code four more times for the other JComboBoxes with, of course, changing a few of the variables.

    Hope this makes sense.

     public void actionPerformed(ActionEvent e) {
            try {
                try {
                    if (e.getSource() instanceof JButton) {
                        JButton source = (JButton)e.getSource();
                        if (source == export) {
                            Export exp = new Export();
                            exp.export();
                        }
                        if (source == newT) {
                            NewMenu newM = new NewMenu();
                            newM.newMenu();
                        }
                        if (source == costAll) {
                            costMon.setVisible(true);
                            costTue.setVisible(true);
                            costWed.setVisible(true);
                            costThu.setVisible(true);
                            costFri.setVisible(true);
                            costM.setVisible(true); //If true it shows the textfield
                            costM.setEditable(true);
                            costT.setVisible(true); //If true it shows the textfield
                            costT.setEditable(true);
                            costW.setVisible(true); //If true it shows the textfield
                            costW.setEditable(true);
                            costTh.setVisible(true); //If true it shows the textfield
                            costTh.setEditable(true);
                            costF.setVisible(true); //If true it shows the textfield
                            costF.setEditable(true);
                        }
                        if (source == edit) {
                            Edit edi = new Edit();
                            edi.edit();
                        }
                        if (source == delete) {
                            Delete del = new Delete();
                            del.delete();
                        }
                        if (source == save) {
                            Save sav = new Save();
                            sav.save();
                        }
                    }
                    if (e.getSource() instanceof JComboBox) {
                        JComboBox jcb = (JComboBox)e.getSource();
                        String choice = (String)jcb.getSelectedItem();
                        if (choice == "Soup") {
                            showSoup();
                        } else if (choice == "Main") {
                            showMain();
                        } else if (choice == "Rushmere") {
                            showRushmere();
                        } else if (choice == "Side") {
                            showSide();
                        } else if (choice == "Bar") {
                            showBar();
                        } else if (choice == "Jacket") {
                            showJacket();
                        } else if (choice == "Dessert") {
                            showDessert();
                        } else {
                            showBread();
                        }
                    }
                } catch (IOException es) {}
            } catch (StackOverflowError soe) {}
        }
     
        public void showMain() throws IOException {
            BufferedReader br = new BufferedReader(new FileReader("Courses.txt"));
            BufferedReader brMain = new BufferedReader(new FileReader("Main.txt"));
            String[] course = new String[7];
            String[][] main = new String[7][46];
            for (int a = 0; a < course.length; a++) {
                for (int k = 0; k < 1; k++) {
                    main[a][k] = br.readLine(); 
                }
                for (int j = 1; j < main.length; j++) {
                    main[a][j] = brMain.readLine();
                    mealM.addItem(main[a][j]);
                    mealT.addItem(main[a][j]);
                    mealW.addItem(main[a][j]);
                    mealTh.addItem(main[a][j]);
                    mealF.addItem(main[a][j]);
                }
            }
            br.close();
            brMain.close();
        }
     
        //class MondayListener implements ListSelectionListener {
        //    public void valueChanged(ListSelectionEvent lse) {
        //        if (!lse.getValueIsAdjusting() && courseM.getSelectedIndex() != -1) { 
        class MondayListener implements ActionListener {
            public void actionPerformed(ActionEvent lse) {
                System.out.println("BBB");
                try {
                    //int index = courseM.getSelectedIndex();
                    //System.out.println(index);
                    JComboBox cbM = (JComboBox)lse.getSource();
                    String choice = (String)cbM.getSelectedItem();
                    mealT.setEditable(false);
                    mealW.setEditable(false);
                    mealTh.setEditable(false);
                    mealF.setEditable(false);
                    if (choice == "Soup")
                        System.out.println("Soup!");
                    if (choice == "Rushmere")
                        showRushmere();
                    /*if (index == 0) {
                        showSoup();
                    } else if (index == 1) {
                        showMain();
                    } else if (index == 2) {
                        showRushmere();
                    } else if (index == 3) {
                        showSide();
                    } else if (index == 4) {
                        showBar();
                    } else if (index == 5) {
                        showJacket();
                    } else if (index == 6) {
                        showDessert();
                    } else if (index == 7){
                        showBread();
                    } */
                } catch (IOException ioe) {}
            }
        }
    }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Populating second combobox from a combobox help!

    As I said, preferably as an SSCCE...that is too much code for me to wade through. Perhaps someone else might come along and do so, but you are helping yourself if you trim it down to an SSCCE - say for instance containing only 2 JComboBox's to demonstrate the issue.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Populating second combobox from a combobox help!

    Sure, here's the SSCCE with one pair of JComboboxes:

     
    class TimetablePanel extends JPanel implements ActionListener {
     
    static JComboBox courseM, mealM;
     
    public TimetablePanel() throws IOException {                        //Method to create contents of panel
            courseM = new JComboBox(); //Creates a combo box
            courseM.setEditable(true);
            courseM.setPreferredSize(new Dimension(15, 25));
            courseM.addActionListener(new MondayListener());
            mealM = new JComboBox(); //Creates a combo box
            mealM.setEditable(true);
            mealM.setPreferredSize(new Dimension(15, 25));
     
    public void actionPerformed(ActionEvent e) {    //ActionListener for both JButton and JComboBoxes
            try {
                try {
                    if (e.getSource() instanceof JButton) {
                        JButton source = (JButton)e.getSource();
                        //Code for the JButtons
                    }
                    if (e.getSource() instanceof JComboBox) {
                        JComboBox jcb = (JComboBox)e.getSource();
                        String choice = (String)jcb.getSelectedItem();
                        if (choice == "Soup") {
                            showSoup();
                        } else if (choice == "Main") {
                            showMain();
                        }
                    }
                } catch (IOException es) {}
            } catch (StackOverflowError soe) {}
        }
     
        public void showMain() throws IOException {                                           //Method to populate second JComboBox
            BufferedReader br = new BufferedReader(new FileReader("Courses.txt"));
            BufferedReader brMain = new BufferedReader(new FileReader("Main.txt"));
            String[] course = new String[7];
            String[][] main = new String[7][46];
            for (int a = 0; a < course.length; a++) {
                for (int k = 0; k < 1; k++) {
                    main[a][k] = br.readLine(); 
                }
                for (int j = 1; j < main.length; j++) {
                    main[a][j] = brMain.readLine();
                    mealM.addItem(main[a][j]);            }
            }
            br.close();
            brMain.close();
        }
     
        class MondayListener implements ActionListener {             //ActionListener for the first JComboBox
            public void actionPerformed(ActionEvent lse) {
                try {
                    JComboBox cbM = (JComboBox)lse.getSource();
                    String choice = (String)cbM.getSelectedItem();
                    if (choice == "Soup")
                        showSoup();
                    if (choice == "Main")
                        showMain();
               } catch (IOException ioe) {}
            }
        }
    }

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Populating second combobox from a combobox help!

    See the following link for a description to a logic error in the actionPerformed method - use equals() and not == when comparing strings
    http://www.javaprogrammingforums.com...html#post18725

  9. The Following User Says Thank You to copeg For This Useful Post:

    ComputerSaysNo (October 18th, 2011)

Similar Threads

  1. How to programmatically set combobox width?
    By stab in forum AWT / Java Swing
    Replies: 1
    Last Post: June 3rd, 2011, 03:59 PM
  2. ComboBox and TextField
    By fahad in forum AWT / Java Swing
    Replies: 2
    Last Post: May 31st, 2010, 08:47 AM
  3. Struts combobox edit
    By kalees in forum Web Frameworks
    Replies: 2
    Last Post: November 1st, 2009, 12:27 AM
  4. do actions in ComboBox
    By java_cs in forum AWT / Java Swing
    Replies: 2
    Last Post: October 1st, 2009, 10:53 AM
  5. Java program to display 2D Array in ComboBox
    By crazydeo in forum AWT / Java Swing
    Replies: 1
    Last Post: May 29th, 2008, 09:13 AM

Tags for this Thread