试试这个演示,其中id_value
是name
的相关id
。
public class PopulateComboBoxDemo extends Application {
private ComboBox<String> people = new ComboBox<>();
private List<String> ids = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
this.populateData();
BorderPane root = new BorderPane();
root.setCenter(people);
Scene scene = new Scene(root, 300, 250);
people.setOnAction(e -> {
int index = people.getSelectionModel().getSelectedIndex();
//here is the id_value
String id_value = ids.get(index);
System.out.println("The id of " + people.getItems().get(index) + " is : " + id_value);
//
//.........
//
});
primaryStage.setScene(scene);
primaryStage.show();
}
private void populateData() {
this.people.getItems().clear();
this.ids.clear();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","user","password");
String sql = "select name, id from person";
PreparedStatement st = con.prepareStatement(sql);
ResultSet rs = st.executeQuery();
int index = 0;
while(rs.next()) {
this.people.getItems().add(index, rs.getString("name"));
this.ids.add(index, String.valueOf(rs.getInt("id")));
index++;
}
con.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(PopulateComboBoxDemo.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
launch(args);
}
}
感谢您的快速回复。我会稍后检查你的代码,如果我打算使用JPA因为我目前使用我的自定义代码实体的人..即时通讯首先要学习jpa ..非常感谢你提前.. –
不客气! – 2017-03-03 15:27:00