[英]java get particular value from combobox
I'm new to java,so I have not so clear about how to get only getString("id")
value when button clicked.For now what I get is the id and full name,but what I actually want to get is only id.我是 Java 新手,所以我不太清楚如何在单击按钮时只获取
getString("id")
值。现在我得到的是 id 和全名,但我真正想要的只是ID。
jComboBox1.addItem(rs.getString("id")+" > "+rs.getString("first_name") +" "+ rs.getString("surname"));
. . .
.
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
String value = (String)jComboBox1.getSelectedItem();//need to change
jTextField1.setText(value);
}
});
I have no idea of how to Store objects and having properties,can you please give me a reference or link
我不知道如何存储对象和拥有属性,你能给我一个参考或链接吗
It is actually not hard to find tutorials and references on the net, for example,其实网上不难找到教程和参考资料,例如,
Anyway, here is an example how you could use it:无论如何,这是一个如何使用它的示例:
Implement a Java object (let us name it Person
) which should contains the attributes id
, first name and
surname`:实现一个 Java 对象(让我们将其命名为
Person
),它应该包含属性id
、 first name and
surname`:
public class Person { private String id; private String firstName; private String surname; public Person(String id, String fname, String sname) { this.id = id; this.firstName = fname; this.surname = sname; } public String getId() { return id; } public String getFirstName() { return firstName; } public String getSurname() { return surname; } // toString() method will be called when the ComboBox is rendered @Override public String toString() { return id + " > " + firstName + " " + surname; } }
Add instances of the above object into the ComboBox:将上述对象的实例添加到 ComboBox 中:
jComboBox1.addItem(new Person(rs.getString("id"), rs.getString("first_name"), rs.getString("surname")));
And modify the action listener as:并将动作侦听器修改为:
new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); Person value = (Person)jComboBox1.getSelectedItem(); jTextField1.setText(value.getId()); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.