gpt4 book ai didi

Javafx:TableView根据行列获取特定表格单元格的图形节点

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:20 25 4
gpt4 key购买 nike

我有一个 JavaFX TableView 组件,并且正在使用 ComboBox 作为单个列的 setGraphic 动态加载数据。我想在 cellFactory 中添加 ComboBox

现在,当用户选择第一个 ComboBox 时,必须相应地设置下一列 ComboBox。为此,我使用了一个选择监听器,但我如何才能获得其他 TableColumnComboBox

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

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

最佳答案

试试这个:

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

关于Javafx:TableView根据行列获取特定表格单元格的图形节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43404863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com