TableView与自定义类型组合框

时间:2014-02-14 08:27:06

标签: combobox javafx-2 tableview

我想创建一个TableView,其列引用了类型:

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

}

我有一个

private TableView<Indicators> tableviewIndicators;

在本专栏中

private TableColumn<Indicators, WindowsItem> tablecolumnFrame;

public static class WindowsItem {

InternalWindow chrt;

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

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

}

我想有一个组合框或选择框,其中每个项目类型都是

WindowsItem

我该如何做到这一点?

我试过这段代码

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();
                    InternalWindow winItem;

                    for (int i = 0; i < itemsInTab; i++) {
                        if (chartsInTab.getChildren().get(i) instanceof InternalWindow) {
                            winItem = (InternalWindow) 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 :(得分:2)

你可以试试这个:

private TableColumn<Indicators, WindowsItem> tablecolumnFrame;

public static class Indicators {

    private final SimpleStringProperty tool_col;        
    private final SimpleStringProperty pane_col;
    private final SimpleBooleanProperty on_col;
    private final SimpleObjectProperty<WindowsItem> chrt;

    private Indicators(String tl, WindowsItem chrt, String pne, Boolean sel) {
        this.tool_col = new SimpleStringProperty(tl);            
        this.chrt = new SimpleObjectProperty<>(chrt);
        this.pane_col = new SimpleStringProperty(pne);
        this.on_col = new SimpleBooleanProperty(sel);            

    }

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

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


    public WindowsItem getChart(){
        return chrt.getValue();
    }


    public void setChart(WindowsItem _chrt) {
        chrt.set(_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 SimpleObjectProperty<WindowsItem> chartProperty() {
        return chrt;
    }

    public SimpleStringProperty paneProperty() {
        return pane_col;
    }
}
小心:

 public SimpleObjectProperty<WindowsItem> chartProperty() {
        return chrt;
    }
您之前的代码中的

是:

 public SimpleStringProperty chartProperty() {
   return chart_col;
 }

这就是

的原因
  SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'StringProperty