JComboBox not updating the selected item
Hi, for some reason whenever I try to use the combo boxes in my main JFrame they do not update as expected. Instead, after clicking for the drop down menu and selecting a new item, it defaults back to the original selection.
The purpose for this program is to create a new table in a database based upon the selections by the user, and the combo boxes represent the table names pulled from the database.
I have noticed that while the table name combo boxes do not seem to work, the combo boxes for defining the where clause work exactly as expected.
Please note that some of the code was pregenerated as part of Netbeans IDE form editor.I will only include the code for one of the JComboBoxes as other than variable name, they are identical. All code used to create and initialize the code as well as change the list is as follows:
creation and init:
listTables1 =new javax.swing.JComboBox();
listTables1.setEnabled(false);
JCombo jc =new JCombo(listTables1, dcbm);// dcbm is an instantiation of DefaultComboBoxModel
listTables1.addItemListener(new java.awt.event.ItemListener(){
publicvoid itemStateChanged(java.awt.event.ItemEvent evt){
listTableChosen(evt);
}
});
*JCombo is a class i made thinking that the problem was with the setModel method of JComboBox. I overrode it with the following:
publicvoid setModel(ComboBoxModel aModel){
Object selection = aModel.getSelectedItem();
super.setModel(aModel);
setSelectedItem(selection);
}
After the user chooses the type of query that they want, I fill the table:
listTables1.removeAllItems();
listTables2.removeAllItems();
for(int i = 0; i < tabs.length; i++){
listTables1.addItem((Table) tabs[i]);//tabs represents the tables pulled from the database
listTables2.addItem((Table) tabs[i]);//The Table type comes from the schemacrawler api
}
listTables1.setSelectedIndex(0);//set the selectedIndex to be the first item
listTables2.setSelectedIndex(0);
the user then decides whether they want to pull from 1 or 2 tables
if(radOneTab.isSelected()){
listTables1.setEnabled(true);
listTables2.setEnabled(false);
}
if(radTwoTab.isSelected()){
listTables1.setEnabled(true);
listTables2.setEnabled(true);
}
finally, I added an itemlistener in the case that the events were not being handled properly:
if(evt.getStateChange() == ItemEvent.SELECTED){
JComboBox cb1 = (JComboBox) evt.getItemSelectable();
Table t1 = (Table) evt.getItem();
cb1.setSelectedItem(t1);
}
if(evt.getStateChange() == ItemEvent.DESELECTED){
}
If anyone can help me, that would be grand. I've been struggling with this for close to a week now.

