javafx:如何以编程方式更改TableCell中ComboBox的项?

javafx:如何以编程方式更改TableCell中ComboBox的项?,combobox,tableview,javafx-8,tablecell,Combobox,Tableview,Javafx 8,Tablecell,正如您已经看到的,我有一个TableView,它有两列ComboBoxTableCell 这些是祖先的价值观 ObservableList<String> ancestors = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); ancestorColumn.setCellFactory(ComboBoxTableCell.forTableColumn(ances

正如您已经看到的,我有一个TableView,它有两列ComboBoxTableCell

这些是祖先的价值观

ObservableList<String> ancestors = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
ancestorColumn.setCellFactory(ComboBoxTableCell.forTableColumn(ancestors));
descendantColumn.setCellFactory(ComboBoxTableCell.forTableColumn());

Map<String, List<String>> descendantMap = new HashMap();
descendantMap.put("1", Arrays.asList("A", "B", "C"));
descendantMap.put("2", Arrays.asList("Z", "L", "C"));
descendantMap.put("3", Arrays.asList("A", "B", "R"));
descendantMap.put("4", Arrays.asList("C", "B", "E"));
descendantMap.put("5", Arrays.asList("A", "E", "C"));
descendantMap.put("6", Arrays.asList("M", "V", "T"));
descendantMap.put("7", Arrays.asList("A", "G", "F"));
descendantMap.put("8", Arrays.asList("J", "O", "N"));
descendantMap.put("9", Arrays.asList("X", "G", "E"));
descendantMap.put("10", Arrays.asList("H", "I", "J"));

我已经创建了一个我将如何做到这一点的示例。但是请注意:我在半个小时左右就完成了这个-代码还远远不够完美,好吗

这是完整的代码

FXML:

主要类别:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("TableTest.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root, 1200, 800);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
TableTestController(执行绑定工作的一个):

包应用;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.value.ChangeListener;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.scene.control.CheckBox;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.ContentDisplay;
导入javafx.scene.control.Spinner;
导入javafx.scene.control.SpinnerValueFactory;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
公共类TableTestController{
@FXML
私有TableView TableView;
@FXML
私有TableColumn tableColumnText;
@FXML
私有TableColumn tableColumnFlag;
@FXML
私有表列表列祖先;
@FXML
私有TableColumn TableColumn后代;
@FXML
私有表列表列点;
私有地图子代地图;
私人观察者名单;
@FXML
受保护的void初始化(){
后代映射=新的HashMap();
grandantmap.put(“1”,Arrays.asList(“A”、“B”、“C”);
gradenantmap.put(“2”,array.asList(“Z”,“L”,“C”);
子代映射.put(“3”,数组.asList(“A”,“B”,“R”);
grandantmap.put(“4”,Arrays.asList(“C”、“B”、“E”);
grandantmap.put(“5”,Arrays.asList(“A”,“E”,“C”);
grandantmap.put(“6”,数组.asList(“M”,“V”,“T”);
grandantmap.put(“7”,Arrays.asList(“A”、“G”、“F”);
gradenantmap.put(“8”,Arrays.asList(“J”,“O”,“N”);
grandantmap.put(“9”,Arrays.asList(“X”,“G”,“E”);
gradenantmap.put(“10”,Arrays.asList(“H”,“I”,“J”);
ancestorList=FXCollections.observableArrayList(genderantMap.keySet());
ObservableList data=FXCollections.observableArrayList();
TableTestObject test=新建TableTestObject();
test.setText(“测试”);
test.setFlag(真);
试验设定点(1);
数据。添加(测试);
tableView.setItems(数据);
tableColumnText.setCellValueFactory(新属性ValueFactory(“文本”);
tableColumnFlag.setCellValueFactory(param->new SimpleObject属性(param.getValue());
TableColumnSector.setCellValueFactory(param->new SimpleObject属性(param.getValue());
TableColumnDegenant.setCellValueFactory(param->new SimpleObject属性(param.getValue());
tableColumnPoints.setCellValueFactory(param->new SimpleObject属性(param.getValue());
tableColumnFlag.setCellFactory(参数->新建TableCell(){
@凌驾
受保护的void updateItem(TableTestObject项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
}否则{
复选框cb=新复选框();
cb.selectedProperty().bindproduction(item.flagProperty());
设定图形(cb);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
}
});
tableColumnSentor.setCellFactory(参数->新建TableCell(){
@凌驾
受保护的void updateItem(TableTestObject项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
}否则{
ComboBox cb=新的ComboBox(取消存储列表);
cb.getSelectionModel().SelectEditeProperty().addListener((ChangeListener)(可观察、旧值、新值)->{
item.ancestorProperty().set(newValue);
System.out.println(item.degenantProperty().get());
});
设定图形(cb);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
}
});
TableColumnDegenant.setCellFactory(参数->新建TableCell(){
@凌驾
受保护的void updateItem(TableTestObject项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
}否则{
ComboBox cb=新ComboBox();
item.ancestorProperty().addListener((ChangeListener)(可观察、旧值、新值)->{
item.degenantProperty().set(“”);
setItems(FXCollections.observableAryList(genderantmap.get(newValue));
});
cb.getSelectionModel().SelectEditeProperty().addListener((ChangeListener)(可观察、旧值、新值)->{
item.degenantProperty().set(newValue);
System.out.println(item.degenantProperty().get());
});
设定图形(cb);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
}
});
tableColumnPoints.setCellFactory(参数->新建TableCell(){
@凌驾
受保护的void updateItem(TableTestObject项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
}否则{
微调器微调器=新微调器();
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    Do not edit this file it is generated by e(fx)clipse from ../src/application/TableTest.fxgraph
-->

<?import java.lang.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="application.TableTestController">

    <center>
        <TableView fx:id="tableView"> 
            <columns>
                <TableColumn fx:id="tableColumnText" text="Text" prefWidth="150"/> 
                <TableColumn fx:id="tableColumnFlag" text="Flag" prefWidth="50"/> 
                <TableColumn fx:id="tableColumnAncestor" text="Ancestor" prefWidth="75"/> 
                <TableColumn fx:id="tableColumnDescendant" text="Descendant" prefWidth="100"/> 
                <TableColumn fx:id="tableColumnPoints" text="Points" prefWidth="75"/> 
            </columns>
            <BorderPane.margin>
                <Insets top="5" left="5" right="5" bottom="5"/> 
            </BorderPane.margin>
        </TableView>
    </center>
</BorderPane>
package application;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class TableTestObject {

    private StringProperty text = new SimpleStringProperty("");
    private BooleanProperty flag = new SimpleBooleanProperty(false);
    private StringProperty ancestor = new SimpleStringProperty("");
    private StringProperty descendant = new SimpleStringProperty("");
    private IntegerProperty points = new SimpleIntegerProperty(0);

    public final StringProperty textProperty() {
        return this.text;
    }

    public final String getText() {
        return this.textProperty().get();
    }

    public final void setText(final String text) {
        this.textProperty().set(text);
    }

    public final BooleanProperty flagProperty() {
        return this.flag;
    }

    public final boolean isFlag() {
        return this.flagProperty().get();
    }

    public final void setFlag(final boolean flag) {
        this.flagProperty().set(flag);
    }

    public final StringProperty ancestorProperty() {
        return this.ancestor;
    }

    public final String getAncestor() {
        return this.ancestorProperty().get();
    }

    public final void setAncestor(final String ancestor) {
        this.ancestorProperty().set(ancestor);
    }

    public final StringProperty descendantProperty() {
        return this.descendant;
    }

    public final String getDescendant() {
        return this.descendantProperty().get();
    }

    public final void setDescendant(final String descendant) {
        this.descendantProperty().set(descendant);
    }

    public final IntegerProperty pointsProperty() {
        return this.points;
    }

    public final int getPoints() {
        return this.pointsProperty().get();
    }

    public final void setPoints(final int points) {
        this.pointsProperty().set(points);
    }
}
package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("TableTest.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root, 1200, 800);
        stage.setScene(scene);
        stage.show();
    }

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class TableTestController {

    @FXML
    private TableView<TableTestObject> tableView;
    @FXML
    private TableColumn<TableTestObject, String> tableColumnText;
    @FXML
    private TableColumn<TableTestObject, TableTestObject> tableColumnFlag;
    @FXML
    private TableColumn<TableTestObject, TableTestObject> tableColumnAncestor;
    @FXML
    private TableColumn<TableTestObject, TableTestObject> tableColumnDescendant;
    @FXML
    private TableColumn<TableTestObject, TableTestObject> tableColumnPoints;

    private Map<String, List<String>> descendantMap;
    private ObservableList<String> ancestorList;

    @FXML
    protected void initialize() {

        descendantMap = new HashMap<>();
        descendantMap.put("1", Arrays.asList("A", "B", "C"));
        descendantMap.put("2", Arrays.asList("Z", "L", "C"));
        descendantMap.put("3", Arrays.asList("A", "B", "R"));
        descendantMap.put("4", Arrays.asList("C", "B", "E"));
        descendantMap.put("5", Arrays.asList("A", "E", "C"));
        descendantMap.put("6", Arrays.asList("M", "V", "T"));
        descendantMap.put("7", Arrays.asList("A", "G", "F"));
        descendantMap.put("8", Arrays.asList("J", "O", "N"));
        descendantMap.put("9", Arrays.asList("X", "G", "E"));
        descendantMap.put("10", Arrays.asList("H", "I", "J"));

        ancestorList = FXCollections.observableArrayList(descendantMap.keySet());

        ObservableList<TableTestObject> data = FXCollections.observableArrayList();
        TableTestObject test = new TableTestObject();
        test.setText("Test");
        test.setFlag(true);
        test.setPoints(1);
        data.add(test);

        tableView.setItems(data);

        tableColumnText.setCellValueFactory(new PropertyValueFactory<>("text"));
        tableColumnFlag.setCellValueFactory(param -> new SimpleObjectProperty<TableTestObject>(param.getValue()));
        tableColumnAncestor.setCellValueFactory(param -> new SimpleObjectProperty<TableTestObject>(param.getValue()));
        tableColumnDescendant.setCellValueFactory(param -> new SimpleObjectProperty<TableTestObject>(param.getValue()));
        tableColumnPoints.setCellValueFactory(param -> new SimpleObjectProperty<TableTestObject>(param.getValue()));

        tableColumnFlag.setCellFactory(param -> new TableCell<TableTestObject, TableTestObject>() {
            @Override
            protected void updateItem(TableTestObject item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    CheckBox cb = new CheckBox();
                    cb.selectedProperty().bindBidirectional(item.flagProperty());
                    setGraphic(cb);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }
            }
        });
        tableColumnAncestor.setCellFactory(param -> new TableCell<TableTestObject, TableTestObject>() {
            @Override
            protected void updateItem(TableTestObject item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    ComboBox<String> cb = new ComboBox<>(ancestorList);
                    cb.getSelectionModel().selectedItemProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
                        item.ancestorProperty().set(newValue);
                        System.out.println(item.descendantProperty().get());
                    });
                    setGraphic(cb);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }
            }
        });
        tableColumnDescendant.setCellFactory(param -> new TableCell<TableTestObject, TableTestObject>() {
            @Override
            protected void updateItem(TableTestObject item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    ComboBox<String> cb = new ComboBox<>();
                    item.ancestorProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
                        item.descendantProperty().set("");
                        cb.setItems(FXCollections.observableArrayList(descendantMap.get(newValue)));
                    });
                    cb.getSelectionModel().selectedItemProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
                        item.descendantProperty().set(newValue);
                        System.out.println(item.descendantProperty().get());
                    });
                    setGraphic(cb);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }
            }
        });
        tableColumnPoints.setCellFactory(param -> new TableCell<TableTestObject, TableTestObject>() {
            @Override
            protected void updateItem(TableTestObject item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    Spinner<Integer> spinner = new Spinner<>();
                    spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(-5, +5, item.getPoints()));
                    spinner.valueProperty().addListener((ChangeListener<Integer>) (observable, oldValue, newValue) -> {
                        item.pointsProperty().set(newValue);
                        System.out.println(item.pointsProperty().get());
                    });
                    setGraphic(spinner);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }
            }
        });
    }
}