【问题标题】:How to set textfield value based on combobox options in Java?如何根据 Java 中的组合框选项设置文本字段值?
【发布时间】:2014-01-10 04:04:35
【问题描述】:

我在数据库中有一个名为 customer 的表,它具有代码和名称等属性。我在其他表中调用了客户的值并使用组合框显示它。我已经在组合框中显示了代码,我想做的就是当我在组合框中选择代码时,值'name'可以显示在 text-field 中,'name' 根据代码出现。

这是我的代码:

try {
        Connections con = new Connections();
        con.setConnections();
        String sql = "select * from customer";
        Statement stat = con.conn.createStatement();
        ResultSet rs=stat.executeQuery(sql);        
        while (rs.next()){
        cmb_custCode.addItem(rs.getString("custCode"));

        txt_custName.setText(rs.getString("custName")); // i'm confused in here, how can i call the name based on the code 
        }
}

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

例如:

当在组合框中选择代码“B0001”时,Jtextfield 也必须显示“Bob”,因为代码 B0001 属于 Bob。

【问题讨论】:

  • 你能发布一些代码吗?
  • 如何命名存储/检索?它是否与您在组合框中放置的值相关联?问题是用语言和抽象来回答
  • 你可以使用javascript来做到这一点..
  • @jack,你从来没有使用过str 变量。
  • @jack 我看到你说你已经解决了这个问题。请分享你的答案。你来这里寻找答案,如果你来了,你应该完成这个。

标签: java swing combobox jtextfield


【解决方案1】:

编辑:

好的。假设您有一个用户Bob,他的codeB001

ItemStateChanged方法

...
String code = (String) cmb.getSelectedItem();
try{

String sql = "SELECT * FROM customer WHERE code='"+code"'"
PreparedStatement prst = con.prepareStatement();
ResultSet rs = prst.executeQuery(sql);
String name = "";

while(rs.next()){

name = rs.getString('name');
}
txt.setText(name);
}catch(Exception ex){
}

您实际上不应该在 itemStateChanged 内部进行连接,但这只是一个示例。

【讨论】:

  • ehm ,但这不是我的目标,例如:当在组合框中选择代码 'B0001' 时,Jtextfield 也必须显示 "Bob" ,因为代码 B0001 属于 Bob
  • 是的,我遇到了一些错误,但现在已经解决了,感谢您到目前为止对我的帮助
  • 能否将您的解决方案发布为答案或编辑您的问题以包含它?仅供其他用户使用:) 谢谢
【解决方案2】:

看看这篇文章。它告诉您有关如何使用组合框的所有信息。 http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

【讨论】:

    【解决方案3】:

    在您自己的示例代码中,您可以将封闭类声明为 ActionListener。然后在声明您的 cmb_custCode 后使用以下内容

    ...
    cmb_custCode.addActionListender(this);
    ...
    

    在实现 ActionListener 时,您必须实现方法 actionPerformed()。我抄袭了以下内容:http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners,但已适应您的代码

     public void actionPerformed(ActionEvent e) {
         JComboBox cb = (JComboBox)e.getSource();
         String custCode = (String)cb.getSelectedItem();
         updateLabel(custCode);
    }
    

    我维护了跟踪示例中 updateLabel(String custCode) 的封装。您可以假设该方法被定义为:

    private void updateLabel(String code) {
        txt_custName.setText(map_cust.get(code));
    }
    

    我已经使用 map_cust 将地图带入其中。它只是代码和名称之间的映射,存储在一个字段中

    Map<String, String> map_cust = new HashMap<String, String>();
    

    并填充它,这将在您的代码中,就在检索 ResultSet 之后

    ...
    cmb_custCode.addItem(rs.getString("custCode"));
    map_cust.put(rs.getString("custCode"), rs.getString("custName");
    

    因此,当您获得结果集时,您会填充地图和组合框,当客户端从组合框中选择和项目时,他会触发并 actionPerformed 进入已注册的侦听器,在该侦听器中操纵事件以获取选中的item,其String为map_cust的key,包含映射的key、value对、custCode映射到custName。当从地图中提取该名称时,它可用于更新标签的文本。

    【讨论】:

      【解决方案4】:

      这是我的代码,它解决了我的问题。希望它对你也有用:)。

      private void fillText(){
       String code = (String) cmb_custCode.getSelectedItem();
       try {
      
              Connections con = new Connections();
              con.setConnections();
              String sql = "select * from customer WHERE custCode='"+code +"'";
              Statement stat = con.conn.createStatement();
              ResultSet rs=stat.executeQuery(sql);
      
      
                while (rs.next()){
                 txt_custName.setText(rs.getString("custName"));
             }
      
      
      
       }
      
      
       catch(Exception e){
          e.printStackTrace();
      
       }
      

      【讨论】:

        猜你喜欢
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        相关资源
        最近更新 更多