0

I am using two JComboBox components. First combo box gets data from database table. Second combo box also gets its data from database table and based on data selected in first combo box. i.e

  1. combo box 1 - gets all courses from database.
  2. combo box 2 - gets all slots available for selected course from first combo box.

How can I do this?

    course = new JComboBox();
    // get all the courses from database
    DefaultComboBoxModel model = new DefaultComboBoxModel();
    ArrayList <String>c =   ConnectDB.getAllCourse();
    for(String co: c)
        {
        model.addElement(co);
        }
    course.setModel(model);
    course.setBounds(135, 136, 86, 20);
    jf.getContentPane().add(course);

Then:

course.addItemListener(new ItemListener()
    {
        @Override
        public void itemStateChanged(ItemEvent e) {
            String course = (String) e.getItem();
            System.out.println(course);
            try {
                ArrayList<String> AvailableSlots = ConnectDB.getAvailableSlots(course);
                System.out.println(ConnectDB.getAvailableSlots(course));
                System.out.println(AvailableSlots);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });
    slot = new JComboBox();
    slot.setModel(availableSlots);
    slot.setBounds(135, 164, 86, 20);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Deepak nigam
  • 99
  • 1
  • 15
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 30 '15 at 10:26

1 Answers1

0

You can register a listener to ComboBox1, take a look here

Then, based on the selected item you can populate ComboBox2

Community
  • 1
  • 1
dimplex
  • 494
  • 5
  • 9