Javafx: TableView gets a graphical node of a specific table cell based on a row column

I have a JavaFX table view component and dynamically load data with ComboBoxboth setGraphicfor individual columns. I want to add ComboBoxin cellFactory.

Now that the user selects the first ComboBox, the next column ComboBoxshould be set accordingly. I used a select listener for this, but how can I get ComboBoxothers TableColumn?

Find the snapshot that I need: enter image description here

Here is a snippet TableView:

        TableColumn< ModelInput, String > colCalibration = new TableColumn<>( "Calibration" );
        TableColumn< ModelInput, String > colSamplingX = new TableColumn<>( "Sampling point X" );
        TableColumn< ModelInput, String > colSamplingY = new TableColumn<>( "Sampling point Y" );
        List< TableColumn< ModelInput, String > > tableColList =
            Stream.of( colCalibration, colSamplingX, colSamplingY )
                  .collect( Collectors.toList() );
        tableviewCalibMatching.getColumns().addAll( tableColList );

        //initialize
        colCalibration.setCellFactory( param -> new TableCell< ModelInput, String >() {

          @Override
          public void updateItem( String item, boolean empty ) {
            super.updateItem( item, empty );
            if( empty ) {
              setText( null );
            } else {
              ComboBox< String > comboBoxCalibMatchingNames = new ComboBox<>( listCalibNames );
              comboBoxCalibMatchingNames.setPrefWidth( splitWidth );
              comboBoxCalibMatchingNames.getSelectionModel().selectedItemProperty()
                                        .addListener( (ChangeListener< String >)( observable, oldValue,
                                            newValue ) -> {

//TODO
//How can I get ComboBox of other TableColumn, need to set according to current //selection

                                        } );
              setGraphic( comboBoxCalibMatchingNames );
              setContentDisplay( ContentDisplay.GRAPHIC_ONLY );
            }
          }
        } );

        colSamplingX.setCellFactory( param -> new TableCell< ModelInput, String >() {

          @Override
          public void updateItem( String item, boolean empty ) {
            super.updateItem( item, empty );
            if( empty ) {
              setText( null );
            } else {
              final ComboBox< String > comboBox = new ComboBox<>();
              setGraphic( comboBox );
              setContentDisplay( ContentDisplay.GRAPHIC_ONLY );
            }
          }
        } );

        colSamplingY.setCellFactory( param -> new TableCell< ModelInput, String >() {

          @Override
          public void updateItem( String item, boolean empty ) {
            super.updateItem( item, empty );
            if( empty ) {
              setText( null );
            } else {
              final ComboBox< String > comboBox = new ComboBox<>();
              setGraphic( comboBox );
              setContentDisplay( ContentDisplay.GRAPHIC_ONLY );
            }
          }
        } );
+4
source share
2 answers

Try the following:

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

TableCell:: getIndex(). -, , TableView.

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

TableCell:: updateItem (T, boolean) . ComboBox updateItem.

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

Source: https://habr.com/ru/post/1674798/


All Articles