ChoiceBox与TableView中的自定义项

时间:2014-02-07 18:58:43

标签: properties combobox javafx-2 tableview tablecell

我有一个

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中添加组合框或选择框?

以下代码

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]

1 个答案:

答案 0 :(得分:0)

为什么需要特殊财产?您可以使用ListView非常easily创建ComboBox

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