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.
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`:
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:
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()); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.