JAVAFX 8 组合框和 ObservableList

JAVAFX 8 ComboBox and ObservableList

我需要一个通过 observablelist 填充的组合框,其中包含从数据库检索的特定数据。这是我的来源。

型号

public ObservableList<Bank> listBank = FXCollections.observableArrayList();

public static class Bank {
            private final StringProperty id;
            private final StringProperty name;

            private Bank(
                    String id, 
                    String name
            ) {
            this.id = new SimpleStringProperty(id);
            this.name = new SimpleStringProperty(name);        
            }

            public StringProperty idProperty() { return id; }
            public StringProperty nameProperty() { return name; }        
        }

查看

@FXML
private ComboBox comboBank<Bank>;

public final void getBankDataFields() {
        comboBank.setItems(model.listBank); 
    }

comboBank.setButtonCell(new ListCell<Bank>() {
                @Override
                protected void updateItem(Bank t, boolean bln) {
                    super.updateItem(t, bln);
                    if (t != null) {
                        setText(t.nameProperty().getValue().toUpperCase());
                    } else {
                        setText(null);
                    }
                }
            });

comboBank.setCellFactory(new Callback<ListView<Bank>, ListCell<Bank>>() {
                @Override 
                public ListCell<Bank> call(ListView<Bank> p) {
                    return new ListCell<Bank>() { 
                        @Override
                        protected void updateItem(Bank t, boolean bln) {
                            super.updateItem(t, bln);
                            if(t != null){
                                setText(t.nomeProperty().getValue().toUpperCase());
                            } else {
                                setText(null);
                            }    

                        }                       
                    };
                }
            });

comboBank.valueProperty().addListener((ObservableValue<? extends Bank> observable, Bank oldValue, Bank newValue) -> {
                setIdBank(newValue.idProperty().getValue());
            });

ComboBox 填充了 NAME 字段,侦听器用于获取相对 ID 并将其传递给用于在 DB 上存储数据的查询。

好的,似乎一切正常,但我有两个问题:

  1. 当用户需要修改这条记录时,我需要从数据库中获取 ID 和 select ComboBox 中的相对名称。我该怎么做?

    comboBank.setValue(????);

  2. 有没有更好的方法来实现这个目标? ObservableMap 可以替代 ObservableList?

提前致谢。

有一种更简单的方法可以实现您想要实现的目标。您应该在 ComboBox 上使用 StringConverter 来显示 Bank 实例的名称。

comboBox.setConverter(new StringConverter<Bank>() {

    @Override
    public String toString(Bank object) {
       return object.nameProperty().get();
    }

    @Override
    public Bank fromString(String string) {
       // Somehow pass id and return bank instance
       // If not important, just return null
       return null;
    }
});

要获取选定值,即选定银行的实例,只需使用:

comboBox.getValue();

MCVE

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.StringConverter;

import java.util.stream.Collectors;

public class Main extends Application {

    @Override
    public void start(Stage stage) {

        ComboBox<Bank> comboBox = new ComboBox<>();
        ObservableList<Bank> items = FXCollections.observableArrayList(
                new Bank("1", "A"), new Bank("2", "B"),
                new Bank("3", "C"), new Bank("4", "D"));
        comboBox.setItems(items);
        StringConverter<Bank> converter = new StringConverter<Bank>() {
            @Override
            public String toString(Bank bank) {
                return bank.nameProperty().get();
            }

            @Override
            public Bank fromString(String id) {
                return items.stream()
                        .filter(item -> item.idProperty().get().equals(id))
                        .collect(Collectors.toList()).get(0);
            }
        };
        comboBox.setConverter(converter);
        // Print the name of the Bank that is selected
        comboBox.getSelectionModel().selectedItemProperty().addListener((o, ol, nw) -> {
            System.out.println(comboBox.getValue().nameProperty().get());
        });
        // Wait for 3 seconds and select the item with id = 2
        PauseTransition pauseTransition = new PauseTransition(Duration.seconds(3));
        pauseTransition.setOnFinished(event -> comboBox.getSelectionModel().select(converter.fromString("2")));
        pauseTransition.play();
        VBox root = new VBox(comboBox);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 200, 200);
        stage.setScene(scene);
        stage.show();
    }
}