小编sen*_*ulz的帖子

需要帮助重置/清除jcombobox值

我使用下面的编码使用另一个jcombobox将值添加到jcombobox我需要根据jcombobox1中选择的一个值将值添加到jcombobox2而不附加值,所以有人可以告诉我重置或清除组合框的方法选择其他选项时的值?下面是我的编码,我是java和netbeans的新手,所以如果有人可以帮助我会感激不尽:)

    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));                               
    }
 }
Run Code Online (Sandbox Code Playgroud)

java swing reset jcombobox

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

jcombobox ×1

reset ×1

swing ×1