首页 > 解决方案 > ComboBoxTableCell,回调

问题描述

我有一个无法以任何方式解决的问题。我已经阅读了我在网上找到的所有内容,并尝试了几十个代码,但我仍然没有解决方案。

它是一个遵循 MVC 模型的 JavaFX 项目。fxml 文件如下:

主要类:

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        AnchorPane root = null;
        try {
            root = (AnchorPane) FXMLLoader.load(getClass().getResource("sample.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }
}

fxml 文件的控制器是:

public class Controller {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<Fila> taula;

    @FXML
    private TableColumn<Fila, String> c1;

    @FXML
    private TableColumn<Fila, ObservableList<String>> c2;

    @FXML
    void initialize() {
        assert taula != null : "fx:id=\"taula\" was not injected: check your FXML file 'sample.fxml'.";
        assert c1 != null : "fx:id=\"c1\" was not injected: check your FXML file 'sample.fxml'.";
        assert c2 != null : "fx:id=\"c2\" was not injected: check your FXML file 'sample.fxml'.";

        ObservableList<Fila> dades = FXCollections.observableArrayList(
                new Fila("First", FXCollections.observableArrayList("1", "2", "3")),
                new Fila("Second", FXCollections.observableArrayList("4", "5", "6")),
                new Fila("Third", FXCollections.observableArrayList("7", "8", "9"))
        );

        c1.setCellValueFactory(cellData -> cellData.getValue().c1Property());

        c2.setCellValueFactory(cellData -> cellData.getValue().c2Property());



        taula.setItems(dades);

    }
}

豆类是:

public class Fila {

    private StringProperty c1;
    private ListProperty<String> c2;

    public Fila(String c1, ObservableList<String> c2) {
        this.c1 = new SimpleStringProperty(c1);
        this.c2 = new SimpleListProperty<>(c2);
    }

    public String getC1() {
        return c1.get();
    }

    public StringProperty c1Property() {
        return c1;
    }

    public void setC1(String c1) {
        this.c1.set(c1);
    }

    public ObservableList<String> getC2() {
        return c2.get();
    }

    public ListProperty<String> c2Property() {
        return c2;
    }

    public void setC2(ObservableList<String> c2) {
        this.c2.set(c2);
    }
}

我没有得到的是,在第二列ComboBox中出现了一个,现在出现的值是List......

我认为可以使用该setCellFactory方法与Callback我们想要的 for 列一起实现ComboBox,但我无法得到它......

对不起我的英语水平;很明显,我正在使用翻译器...

标签: javafxcomboboxtablecell

解决方案


你可以做

    c2.setCellFactory(column -> new TableCell<Fila, ObservableList<String>>() {

        private final ComboBox<String> combo = new ComboBox<>();

        {
            combo.valueProperty().addListener((obs, oldValue, newValue) -> {
                // in real life, update model appropriately here...
                System.out.println("Selected "+newValue+" for "+getTableView().getItems().get(getIndex()).getC1());
            });
        }

        @Override
        protected void updateItem(ObservableList<String> items, boolean empty) {
            super.updateItem(items, empty);
            if (empty) {
                setGraphic(null);
            } else {
                combo.setItems(items);
                // in real life, do combo.setValue(...) with some value from model
                setGraphic(combo);
            }
        }
    });

您的模型并没有真正为组合框提供足够的信息,因为它没有任何属性表示选择了组合框中的哪个项目。大概您只是没有在发布的示例中添加此内容;组合框上的侦听器valueProperty()应该适当地更新它,并且该updateItem()方法应该根据适当的数据设置组合框的值。


推荐阅读