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
- combo box 1 - gets all courses from database.
- 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);