当前位置:  开发笔记 > 编程语言 > 正文

JavaFX ComboBox - 显示文本但在选择时返回ID

如何解决《JavaFXComboBox-显示文本但在选择时返回ID》经验,为你挑选了1个好方法。

我有一个机场数据库表,每个机场都有一个名称和一个ID.

在JavaFX中,我有一个表单,其中ComboBox,组合框需要显示所有机场名称,并且在提交表单时,需要将机场的ID插入数据库(而不是其名称).

但我真的不知道解决方案是什么.

我有一个

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

按下按钮时会将其添加到数据库中:

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.

我该怎么做呢?



1> DVarga..:

您可以AirPort使用IDname成员创建一个类,并ComboBox显示这些对象:ComboBox.

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:

// Fill the list from the DataBase
ObservableList airports = FXCollections.observableArrayList();
airports.addAll(new AirPort(0, "Heathrow"), 
    new AirPort(1, "Frankfurt"),
    new AirPort(2, "NewYork"));

ComboBox combo = new ComboBox<>();
combo.setItems(airports);

最后显示您可以使用的对象的名称,例如StringConverter:

combo.setConverter(new StringConverter() {

    @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的对象:

combo.valueProperty().addListener((obs, oldval, newval) -> {
    if(newval != null)
        System.out.println("Selected airport: " + newval.getName() 
            + ". ID: " + newval.getID());
});

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有