ChoiceBox在一个TableView中自定义项目(ChoiceBox with custom

2019-10-19 08:08发布

我有一个

private TableView<Indicators> tableviewIndicators;

与列

private TableColumn<Indicators, WindowsItem> tablecolumnFrame;

public static class WindowsItem {

    CustomInternalWindow chrt;

    private WindowsItem(CustomInternalWindow _chrt) {
        chrt = _chrt;
    }

    public String toString() {
        return chrt.getTitle();
    }

}


private Indicators(String tl, WindowsItem chrt, String pne, Boolean sel) {
        this.tool_col = new SimpleStringProperty(tl);
        if (chrt == null) {
            this.chart_col = new SimpleStringProperty("");
        } else {
            this.chart_col = new SimpleStringProperty(chrt.toString());
        }
        this.pane_col = new SimpleStringProperty(pne);
        this.on_col = new SimpleBooleanProperty(sel);
        this.chrt   = chrt;

    }

    public String getTool() {
        return tool_col.get();
    }

    public void setTool(String tl) {
        tool_col.set(tl);
    }

    public WindowsItem getChart() {
        return chrt;
    }     

    public void setChart(WindowsItem _chrt) {
        System.out.println("Indicators::setChart "+chrt.toString());
        chrt = _chrt;
    }

    public String getPane() {
        return pane_col.get();
    }

    public void setPane(String pne) {
        pane_col.set(pne);
    }

    public Boolean getOn() {
        return on_col.get();
    }

    public void setOn(boolean sel) {
        on_col.set(sel);
    }

    public SimpleBooleanProperty onProperty() {
        return on_col;
    }

    public SimpleStringProperty toolProperty() {
        return tool_col;
    }

    public SimpleStringProperty chartProperty() {
        return chart_col;
    }

    public SimpleStringProperty paneProperty() {
        return pane_col;
    }
}
tablecolumnFrame.setCellValueFactory(new PropertyValueFactory<Indicators, WindowsItem>("chart"));

如何添加在tablecolumnFrame ComboBox或choicebox?

下面的代码

tablecolumnFrame.setCellFactory(new Callback<TableColumn<Indicators, WindowsItem>, TableCell<Indicators, WindowsItem>>() {
        @Override
        public TableCell<Indicators, WindowsItem> call(TableColumn<Indicators, WindowsItem> param) {
            TableCell<Indicators, WindowsItem> cell = new TableCell<Indicators, WindowsItem>() {
                @Override
                public void updateItem(WindowsItem item, boolean empty) {
                    super.updateItem(item, empty);
                    if(empty){
                        return;
                    }

                    if (item != null) {
                        //final ChoiceBox<WindowsItem> choice = new ChoiceBox<>();
                        final ComboBox<WindowsItem> choice = new ComboBox<>();
                        int itemsInTab = chartsInTab.getChildren().size();// dimensione del contenuto del tab, compreso il pane
                        CustomInternalWindow winItem;
                        //
                        for (int i = 0; i < itemsInTab; i++) {
                            if (chartsInTab.getChildren().get(i) instanceof CustomInternalWindow) {
                                winItem = (CustomInternalWindow) chartsInTab.getChildren().get(i);                                    
                                choice.getItems().add(new WindowsItem(winItem));
                                //choice.getItems().add(winItem.toString());
                                System.out.println("winItem.toString() "+winItem.toString());
                            }
                        }

返回此错误

SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'StringProperty [bean: TableRow[id=null, styleClass=cell indexed-cell table-row-cell], name: skinClassName, value: com.sun.javafx.scene.control.skin.TableRowSkin]' for control TableRow[id=null, styleClass=cell indexed-cell table-row-cell]

Answer 1:

为什么你需要特殊的属性? 您可以创建ListViewComboBox非常容易 :

    ObservableList<WindowsItem> windowsItems = FXCollections.observableArrayList();
    ObservableList<WindowsItem> data = FXCollections.observableArrayList();

    final ListView<WindowsItem> listView = new ListView<>(data);
    listView.setEditable(true);

    listView.setItems(data);
    listView.setCellFactory(ComboBoxListCell.forListView(windowsItems));


文章来源: ChoiceBox with custom Item in a TableView