I have implemented a ComboBoxModel that is populated based on data from a database:
As you can see, refreshModel() is a template method and subclasses fill in the details by implementing three abstract methods: getSQLString(), setSQLParameters(), and getDatabaseRecord(). I have several subclasses for different tables in the database I am working with. TeacherComboBoxModel and SchoolComboBoxModel are the ones I am working with at the moment.
Maybe I need to back up a little. This is being used in an "attendance tracker" application. The details are not too important at the moment. What I want to do is have two ComboBoxes on a JPanel. One uses SchoolComboBoxModel and another uses TeacherComboBoxModel. When the user selects a school in the former, the later should be populated with only teachers that work at that particular school.
I figure a good way to do this is to add an ItemListener to the school ComboBox. When itemStateChanged() is called with a SELECTED event, then I can call the refreshModel() method on the model used for the teacher ComboBox, which happens to be an instance of TeacherComboBoxModel. Here's the code that I'm using:
I thought this would work. However, when I selected a school name in schoolComboBox, the drop list in teacherComboBox is blank. The drop down list is drawn, but there are no Strings painted in it. I can't even see the "(Choose One)" option that should be listed first. Also, no matter where I click on the drop down list, it always selects the same teacher.
Does anyone have any ideas how I can fix this? Is there another method that I need to override in my ComboBoxModel subclass? I will greatly appreciate any suggestions that can point me in the right direction.
Thanks,
Layne
As you can see, refreshModel() is a template method and subclasses fill in the details by implementing three abstract methods: getSQLString(), setSQLParameters(), and getDatabaseRecord(). I have several subclasses for different tables in the database I am working with. TeacherComboBoxModel and SchoolComboBoxModel are the ones I am working with at the moment.
Maybe I need to back up a little. This is being used in an "attendance tracker" application. The details are not too important at the moment. What I want to do is have two ComboBoxes on a JPanel. One uses SchoolComboBoxModel and another uses TeacherComboBoxModel. When the user selects a school in the former, the later should be populated with only teachers that work at that particular school.
I figure a good way to do this is to add an ItemListener to the school ComboBox. When itemStateChanged() is called with a SELECTED event, then I can call the refreshModel() method on the model used for the teacher ComboBox, which happens to be an instance of TeacherComboBoxModel. Here's the code that I'm using:
I thought this would work. However, when I selected a school name in schoolComboBox, the drop list in teacherComboBox is blank. The drop down list is drawn, but there are no Strings painted in it. I can't even see the "(Choose One)" option that should be listed first. Also, no matter where I click on the drop down list, it always selects the same teacher.
Does anyone have any ideas how I can fix this? Is there another method that I need to override in my ComboBoxModel subclass? I will greatly appreciate any suggestions that can point me in the right direction.
Thanks,
Layne