【问题标题】:how to link two jcomboboxes without repeat the values如何在不重复值的情况下链接两个 jcombobox
【发布时间】:2016-09-22 21:47:59
【问题描述】:

我想从组合框中选择一个项目,该项目调用名称以在组合框中显示项目 asnaf .. 我做到了,但该项目出现了两次,当我从组合框名称中选择另一个项目时,其他项目附加在前面的项目上为 As它如上图所示..这是我的代码

 public void agent_comboBoxA() {
    try {

        String sql = "select * from `trading`";
        myStmt = connection.prepareStatement(sql);
        rs = myStmt.executeQuery();
        while (rs.next()) {
            String agent = rs.getString("agent_name");
            names.addItem(agent);

        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

 public void kind_comboBoxA()
{
    String sql = "select kind from trading where agent_name = ?";


    try {

        myStmt = connection.prepareStatement(sql);
        myStmt.setString(1,String.valueOf(names.getSelectedItem()));
        rs = myStmt.executeQuery();
        while (rs.next()) {
            String kind = rs.getString("kind");
            asnaf.addItem(kind);
        }
    }catch (SQLException e) {
        e.printStackTrace();
    }
}

public the defualt Constructor {

        agent_comboBoxA();

        names.addItemListener(e -> {
            kind_comboBoxA();
        });
    }

this when repeat the same value

this when repeat the same value and the other value

【问题讨论】:

  • 我可以在这里看到错误:public the defualt Constructor。还是只是演示?
  • 是的,只是演示@YoungMillie

标签: java swing jdbc combobox


【解决方案1】:

其他项目附加在前面的项目上

在开始添加新项目之前,您需要删除旧项目。

查看JComboBox API 中的removeAllItems() 方法。

【讨论】:

  • 把removeAllItems()放在代码的哪个位置?
  • @AbdelrahmanNashaat,你读过我回答的第一行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多