JavaFX ComboBox - 显示文本但在选择时返回ID [英] JavaFX ComboBox - Display text but return ID on selection
问题描述
我有一个带机场的数据库表,每个机场都有一个名称和一个ID。
I have a Database table with airports, each airport has a name and an ID.
在JavaFX中,我有一个表格, ComboBox
,组合框需要显示所有机场名称,提交表单时需要将机场ID插入数据库(不是其名称)。
In JavaFX I have a form, with a ComboBox
, the combobox needs to display all the airport names and when the form is submitted it needs to insert the ID of the airport into the database (not its name).
但我真的不知道解决方案是什么。
But I'm not really figuring out what the solution is.
我有一个
ObservableList vliegveldenList = FXCollections.observableArrayList();
ObservableList vliegveldenIDList = FXCollections.observableArrayList();
数据库连接填充 ComboBox
ResultSet rs = Project_Fasten_Your_Seatbelt.conn.createStatement()
.executeQuery("SELECT vliegveldnaam, vliegveld_id FROM fys_project.vliegvelden;");
while (rs.next()) {
vliegveldenList.add(rs.getString(1));
vliegveldenIDList.add(rs.getString(2));
}
填充组合框:
vliegveldHerkomst.setValue("Luchthaven ...");
vliegveldHerkomst.setItems(vliegveldenList);
当按下按钮时,这会添加到数据库中:
And this is added to the database when button is pressed:
String registratieValue = registratieNmrTxt.getText();
String vluchtValue = vluchtNrmTxt.getText();
String vliegveldValue = (String) vliegveldHerkomst.getSelectionModel().getSelectedItem();
String bestemmingValue = (String) vliegveldBestemming.getSelectionModel().getSelectedItem();
String gevondenValue = (String) vliegveldGevonden.getSelectionModel().getSelectedItem();
LocalDate dGevondenValue = datumGevondenDate.getValue();
LocalDate dVluchtValue = datumVluchtDate.getValue();
String gewichtValue = gewichtBagageTxt.getText();
String kleurenValue = (String) kleuren.getSelectionModel().getSelectedItem();
String kofferValue = (String) kofferMerken.getSelectionModel().getSelectedItem();
String opmerkingValue = opmerkingArea.getText();
//Data gevonden bagage invoeren
Project_Fasten_Your_Seatbelt.conn.createStatement().executeUpdate(
"INSERT INTO gevondenbagage "
+ "(registratienummer, datumgevonden, datumaangemeld, vliegveldherkomst, "
+ "vliegveldbestemming, vliegveldgevonden, vluchtnummer, vluchtdatum, gewicht, "
+ "kleur, merk, `speciale opmerkingen`, userid)"
+ "VALUES ('" + registratieValue + "','" + dGevondenValue + "','" + today.format(localDate) + "','"
+ vliegveldValue + "','" + bestemmingValue + "','" + gevondenValue + "','"
+ vluchtValue + "','" + dVluchtValue + "','" + gewichtValue + "','"
+ kleurenValue + "','" + kofferValue + "','" + opmerkingValue + "','"
+ Project_Fasten_Your_Seatbelt.getUserId() + "')");
这一切都运转正常,但我想设置机场名称而不是机场名称机场 vliegveldValue
。
This all works okay, but instead of the name of the airport I want to set the ID for the airport for vliegveldValue
.
我该怎么办?
推荐答案
你可以创建例如一个 AirPort
类,其中 ID
和 name
成员和一个 ComboBox
显示这些对象: ComboBox< AirPort>
。
You can create e.g. an AirPort
class with ID
and name
members and a ComboBox
that displays these objects: ComboBox<AirPort>
.
AirPort
类:
public class AirPort {
private int ID;
private String name;
public AirPort(int id, String name) {
this.ID = id;
this.name = name;
}
public int getID() { return ID; }
public String getName() { return name; }
}
从数据库中获取项目并创建 ComboBox
:
Get the items from the DB and create the ComboBox
:
// Fill the list from the DataBase
ObservableList<AirPort> airports = FXCollections.observableArrayList();
airports.addAll(new AirPort(0, "Heathrow"),
new AirPort(1, "Frankfurt"),
new AirPort(2, "NewYork"));
ComboBox<AirPort> combo = new ComboBox<>();
combo.setItems(airports);
最后显示可以使用的对象的名称,例如 StringConverter
:
Finally to display the name of the objects you can use for example a StringConverter
:
combo.setConverter(new StringConverter<AirPort>() {
@Override
public String toString(AirPort object) {
return object.getName();
}
@Override
public AirPort fromString(String string) {
return combo.getItems().stream().filter(ap ->
ap.getName().equals(string)).findFirst().orElse(null);
}
});
然后当价值变化时,你会回来 AirPort
包含所需ID的对象:
And then when the value is changing you get back AirPort
objects which contains the needed ID:
combo.valueProperty().addListener((obs, oldval, newval) -> {
if(newval != null)
System.out.println("Selected airport: " + newval.getName()
+ ". ID: " + newval.getID());
});
这篇关于JavaFX ComboBox - 显示文本但在选择时返回ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!