I designed a swing interface with MySQL table. I put two comboboxes in a manner when the 1st combobox value is selected (Brand Name), the second combobox values (available items under thise selected brand) will be loaded via a mysql query. My code is...

String url = "jdbc:mysql://localhost:3306/databasename"; 
String login = "root"; String password = ""; Connection con = DriverManager.getConnection(url, login, password);
comboBox1 = new JComboBox();    comboBox1.setEditable(false);
comboBox1.addItem("- - -");
Statement stmt1=null;
String query1 = "SELECT brand FROM brands";
stmt1 = con.createStatement();
ResultSet rs1 = stmt1.executeQuery(query1);
while(rs1.next()) {comboBox1.addItem(rs1.getString(1));}

comboBox2 = new JComboBox(); comboBox2.setEditable(false);

comboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
    String comboBox1Selected=comboBox1.getSelectedItem().toString();
    Statement stmt2=null;
    String query2 = "SELECT item FROM "+comboBox1Selected+"";
    stmt2 = con.createStatement();
    ResultSet rs2 = stmt2.executeQuery(query2);
    while(rs2.next()) {comboBox2.addItem(rs2.getString(1));}
catch (SQLException ex1) {JOptionPane.showMessageDialog(null,"Failed to Item-List..!"); ex1.printStackTrace(); return;}
catch (SQLException ex2) {JOptionPane.showMessageDialog(null,"Failed to Brand-List..!"); ex2.printStackTrace(); return;}
catch (SQLException ex3) {ex3.printStackTrace(); JOptionPane.showMessageDialog(null,"Unable to Connect..!"); return;}

The problem is, eventhough the comboboxes are functioning correctly, if I select another choice from 1st combobox, the second combobox doesn't avoid the "older values" (they appears with the newer values).

What might be the reason..? Anyone could explain..?
Thanks in advance.

before you start filling combobox2, erase the values that are in there at that moment. looks to me like you're not doing that.

but it would be better if you started separating the business and UI code.

This article has been dead for over six months. Start a new discussion instead.