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

时间:2017-01-13 12:35:24

标签: java sql javafx combobox

我有一个包含机场的数据库表,每个机场都有一个名称和一个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机场设置机场ID,而不是机场名称。

我该怎么做?

2 个答案:

答案 0 :(得分:15)

您可以创建例如包含AirPortID成员的name班级以及显示这些对象的ComboBoxComboBox<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

// 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

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

然后当值发生变化时,您将获得包含所需ID的AirPort个对象:

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

答案 1 :(得分:2)

您的机场舱..

public class Airport {

private int id;
private String name;

public Airport(int id, String name) {
    super();
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

} //类机场

创建可观察的机场列表

ObservableList<AirPort> airports = FXCollections.observableArrayList();
airports.add(new Airport(1, "Beijing Capital International Airport"));
airports.add(new Airport(2, "Los Angeles International Airport"));
airports.add(new Airport(3, "London Heathrow Airport"));

设置组合框的项目。 。

combo.setItems(airports);

此后,当您运行程序时,将得到如下输出。 .. enter image description here

要获取机场的名称,您需要重写Airport类中的toString方法。

@Override
public String toString() {
    return this.getName();
}

在此之后,您将获得类似的输出。 enter image description here


现在要获取所选机场的ID,您可以设置事件处理程序。 。

private void setEventOnAirport() {
    combo.setOnKeyReleased(event -> {
        if (event.getCode().equals(KeyCode.ENTER)) {
            Airport airport = combo.getSelectionModel().getSelectedItem();
            System.out.println(airport.getId());
        }
    });
}

通过此功能,您可以查看所选机场的 ID 。 。