Javafx:TableView基于行列

时间:2017-04-14 03:40:24

标签: java uitableview javafx combobox

我有一个JavaFX表视图组件,并且动态加载ComboBox的数据作为setGraphic用于各个列。我想在ComboBox添加cellFactory

现在,当用户选择第一个ComboBox时,必须相应地设置下一列ComboBox。为此,我使用了一个选择监听器,但是如何获得其他ComboBox的{​​{1}}?

请找到我需要的快照: enter image description here

以下是TableColumn的摘录:

TableView

2 个答案:

答案 0 :(得分:1)

试试这个:

public class Test
{
    private final TableView<ModelInput> tableviewCalibMatching = new TableView<>();

    private final TableColumn<ModelInput, String> colCalibration = new TableColumn<>("Calibration");
    private final TableColumn<ModelInput, String> colSamplingX = new TableColumn<>("Sampling Point X");
    private final TableColumn<ModelInput, String> colSamplingY = new TableColumn<>("Sampling Point Y");

    private final ObservableList<String> listCalibNames = FXCollections.observableArrayList();
    private final ObservableList<String> listSamplingXNames = FXCollections.observableArrayList();
    private final ObservableList<String> listSamplingYNames = FXCollections.observableArrayList();

    private final ObservableList<ModelInput> items = FXCollections.observableArrayList();

    public Test()
    {
        tableviewCalibMatching.setItems(items);
        tableviewCalibMatching.getColumns().addAll(colCalibration, colSamplingX, colSamplingY);

        colCalibration.setCellFactory(ComboBoxTableCell.forTableColumn(listCalibNames));
        colSamplingX.setCellFactory(ComboBoxTableCell.forTableColumn(listSamplingXNames));
        colSamplingY.setCellFactory(ComboBoxTableCell.forTableColumn(listSamplingYNames));

        colCalibration.setCellValueFactory(new PropertyValueFactory<>("calibration"));
        colSamplingX.setCellValueFactory(new PropertyValueFactory<>("samplingX"));
        colSamplingY.setCellValueFactory(new PropertyValueFactory<>("samplingY"));

        colCalibration.setOnEditCommit(event ->
        {
            final String newCalibrationValue = event.getNewValue();

            event.getRowValue().setCalibration(newCalibrationValue);

            // You can change the logic here based on what you need
            event.getRowValue().setSamplingX(listSamplingXNames.get(1));
            event.getRowValue().setSamplingY(listSamplingXNames.get(1));
        });

        colSamplingX.setOnEditCommit(event ->
        {
            final String newSamplingXValue = event.getNewValue();

            event.getRowValue().setSamplingX(newSamplingXValue);
        });

        colSamplingY.setOnEditCommit(event ->
        {
            final String newSamplingYValue = event.getNewValue();

            event.getRowValue().setSamplingY(newSamplingYValue);
        });
    }

    public static class ModelInput
    {
        private final StringProperty calibration = new SimpleStringProperty();
        private final StringProperty samplingX = new SimpleStringProperty();
        private final StringProperty samplingY = new SimpleStringProperty();

        public final StringProperty calibrationProperty()
        {
            return this.calibration;
        }

        public final String getCalibration()
        {
            return this.calibrationProperty().get();
        }

        public final void setCalibration(final String calibration)
        {
            this.calibrationProperty().set(calibration);
        }

        public final StringProperty samplingXProperty()
        {
            return this.samplingX;
        }

        public final String getSamplingX()
        {
            return this.samplingXProperty().get();
        }

        public final void setSamplingX(final String samplingX)
        {
            this.samplingXProperty().set(samplingX);
        }

        public final StringProperty samplingYProperty()
        {
            return this.samplingY;
        }

        public final String getSamplingY()
        {
            return this.samplingYProperty().get();
        }

        public final void setSamplingY(final String samplingY)
        {
            this.samplingYProperty().set(samplingY);
        }
    }
}

答案 1 :(得分:-1)

您可以从TableCell :: getIndex()获取当前行。 首先,更新TableView项目中包含的对象。

comboBoxCalibMatchingNames.setOnAction(event -> {
    tableView.getItems().get(getIndex()).someProperty().set(anyValue1);
    tableView.getItems().get(getIndex()).otherProperty().set(anyValue2);
});

它使用新值触发更新列的TableCell :: updateItem(T,boolean)。 您必须在updateItem上更新ComboBox的选择。

// in updteItem code on colSamplingX
final ComboBox< String > comboBox = new ComboBox<>();
comboBox.getSelectionModel().select(item);    // Select updated item.
setGraphic( comboBox );
setContentDisplay( ContentDisplay.GRAPHIC_ONLY );