Search code examples
javaswingresetjcombobox

need help on reset/clear jcombobox values


im using below coding to add values to a jcombobox using another jcombobox and i need to add the values to the jcombobox2 according to the one get selected in the jcombobox1 without appending values so can someone tell me a way to reset or clear the combo-box values when another option selected? below is my coding and i'm new to java and netbeans so if someone can help i'll be grateful :)

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database1", "root", "senura123");
    Statement stat = (Statement) con.createStatement();
    String val=jComboBox1.getSelectedItem().toString();
    String check; String col;
    if ("Vehicles".equals(val)){
        check = "select reg_no from vehicle;";
        col="reg_no";
    }
    else if ("Travel Guides".equals(val)){
        check = "select username from travelguide;";
        col="username";
    }
    else{
        check = "select username from transportofficer";
        col="username";
    }                   
    ResultSet rslt = stat.executeQuery(check);
    while (rslt.next()) {
        jComboBox2.addItem(rslt.getString(col));                               
    }
 }

Solution

  • See DefaultComboBoxModel.removeAllElements()

    Empties the list.