简体   繁体   English

如何基于Java中的组合框选项设置文本字段值?

[英]How to set textfield value based on combobox options in Java?

I've a table in database named customer that have attribute like code and name. 我在数据库中有一个名为customer的表,该表具有诸如代码和名称之类的属性。 I've called the value of the customer in other table and displayed it using combo-box. 我已经在其他表中调用了客户的价值,并使用组合框显示了它。 I've displayed the code in combo-box, and all I wanna do is when I choose the code in combo-box, value 'name' can be displayed in text-field , the 'name' appear based on code. 我已经在组合框中显示了代码,而我想要做的就是当我在组合框中选择代码时,可以在文本字段中显示值“名称”,而“名称”则基于代码显示。

here is my code : 这是我的代码:

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();
  }
 }

for example : 例如 :

when the code 'B0001' is selected in the combobox, the Jtextfield must also display "Bob" , because code B0001 is belongs to Bob. 当在组合框中选择代码“ B0001”时,Jtextfield也必须显示“ Bob”,因为代码B0001属于Bob。

EDIT: 编辑:

Ok. 好。 So let's say that you have a user Bob and his code is B001 . 假设您有一个用户Bob ,他的codeB001

ItemStateChanged Method 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){
}

You shouldn't actually connect inside the itemStateChanged but this is just an example. 您实际上不应该在itemStateChanged内部进行连接,但这只是一个示例。

Take a look at this article. 看一下这篇文章。 It tells you everything you need to know on how to use combo boxes. 它告诉您有关如何使用组合框的所有知识。 http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

In your own example code, you can declare the enclosing class as an ActionListener. 在您自己的示例代码中,可以将封闭的类声明为ActionListener。 then use the following after declaring your cmb_custCode 然后在声明您的cmb_custCode之后使用以下命令

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

when implementing an ActionListener, you have to implement the method actionPerformed(). 在实现ActionListener时,必须实现actionPerformed()方法。 I've cribbed the following from : http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners but adapted to your code 我从以下网站获取了以下内容: 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);
}

I've maintained the encapsulation of updateLabel(String custCode) from the example on the trail. 我在跟踪示例中维护了updateLabel(String custCode)的封装。 You can assume that that method is defined as something like : 您可以假定该方法定义为:

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

I've brough a Map into it with map_cust. 我用map_cust将Map标记为其中。 It's just a map between code and name, stored in a field 它只是代码和名称之间的映射,存储在字段中

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

and populated it, this would go in your code, just after retrieving the ResultSet 并填充它,这将在检索ResultSet之后进入您的代码

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

So, when you get your result set, you populate a map and your combo box, when the client picks and item from the combo box, he fires and actionPerformed, into the registered listener, where the event is manipulated to get the selected item, the String of which is the key to the map_cust, containing mapped key, value pairs, custCode mapped to custName. 因此,当您获得结果集时,将填充一个地图和组合框,当客户端从组合框中选择项目时,他将触发并执行actionPerformed到注册的侦听器中,在该侦听器中操作事件以获取选定的项目,其字符串是map_cust的键,包含映射的键,值对,映射到custName的custCode。 When that name is pulled from the map, it may be used to update the text of the label. 从地图上提取该名称时,可以使用它来更新标签的文本。

Here is my code, and it solved my problem. 这是我的代码,它解决了我的问题。 Hope it's useful for you too :). 希望它对您也有用:)。

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();

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM