java 需要有关重置/清除 jcombobox 值的帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10156167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:50:39  来源:igfitidea点击:

need help on reset/clear jcombobox values

javaswingresetjcombobox

提问by senrulz

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 :)

我使用下面的编码使用另一个 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));                               
    }
 }

回答by Andrew Thompson

回答by Greg Kopff

Set a newmodel into your combobox:

在组合框中设置一个模型:

final List<String> values = new ArrayList<String>();

while (rslt.next()) {
  values.add(rslt.getString(col));
}

jComboBox2.setModel(new DefaultComboBoxModel(values.toArray()));

See DefaultComboBoxModel.

请参阅DefaultComboBoxModel



As a further comment, however, depending on how much latency is involved in your query, you may like to split this work up into EDT and background thread parts using SwingWorker.

然而,作为进一步的评论,根据您的查询中涉及多少延迟,您可能希望使用SwingWorker将此工作拆分为 EDT 和后台线程部分。

回答by jose

Usually it happens because you have an event associated JComboBox. It is solved if you have control item in the JComboBox to act, for example:

通常发生这种情况是因为您有一个与 JComboBox 相关的事件。如果你在 JComboBox 中有控制项来操作就解决了,例如:

jComboBoxExample.addActionListener (new ActionListener () {
   public void actionPerformed (ActionEvent e) {
???? do_run ();
???}
});



public void do_run() {
  int n=jComboBoxPerfilDocumentos.getItemCount(); <--THIS IS THE SOLUTION
??if (n> 0) { 
??? String x = jComboBoxPerfilDocumentos.getSelectedItem (). ToString ();
??}
}