JavaFX Tableview没有将数据填充到列

时间:2015-06-03 16:42:12

标签: javafx java-8 javafx-8

我正在尝试使用1个计数器+4个值列创建一个TableView。但由于某些未知原因,列val3,val4未在表视图中填充。我用“val3.get()”更新了getVal2()方法,我可以看到Value2列中的val3值。因此,我怀疑读取Value3列时存在一些问题。我还检查了命名约定/ camel-casing。它看起来很好。可以帮我解决这个问题吗?

        ObservableList<Counters> data = FXCollections.observableArrayList();
    Iterator<String> counterIterator = counterMap.keySet().iterator();
    while(counterIterator.hasNext()) {
        String counter = counterIterator.next();
        String[] values = counterMap.get(counter);
        String val1 = "a", val2 = "b", val3 = "c", val4 = "";
    Counters counters = new Counters(counter, val1, val2, val3, val4);
        data.add(counters);
    }

    TableView<Counters> table = new TableView<Counters>();
    table.setEditable(true);

    TableColumn<Counters, String> counter = new TableColumn<Counters, String>("Counter");
    counter.setCellValueFactory(new PropertyValueFactory<Counters, String>("counter"));
    TableColumn<Counters, String> val1 = new TableColumn<Counters, String>("Value1");
    val1.setCellValueFactory(new PropertyValueFactory<Counters, String>("val1"));
    TableColumn<Counters, String> val2 = new TableColumn<Counters, String>("Value2");
    val2.setCellValueFactory(new PropertyValueFactory<Counters, String>("val2"));
    TableColumn<Counters, String> val3 = new TableColumn<Counters, String>("Value3");
    val3.setCellValueFactory(new PropertyValueFactory<Counters, String>("val3"));
    TableColumn<Counters, String> val4 = new TableColumn<Counters, String>("Value4");
    val3.setCellValueFactory(new PropertyValueFactory<Counters, String>("val4"));

    table.getColumns().add(counter);
    table.getColumns().add( val1);
    table.getColumns().add( val2);
    table.getColumns().add( val3);
    table.getColumns().add( val4);
    table.setItems(data);




public static class Counters {

    private SimpleStringProperty counter, val1, val2, val3, val4;

    public Counters(String counter, String val1, String val2, String val3, String val4) {
        this.counter = new SimpleStringProperty(counter);
        this.val1 = new SimpleStringProperty(val1);
        this.val2 = new SimpleStringProperty(val2);
        this.val3 = new SimpleStringProperty(val3);
        this.val4 = new SimpleStringProperty(val4);
    }

    public String getCounter() {
        return counter.get();
    }

    public void setCounter(String counter) {
        this.counter.set(counter);
    }

    public String getVal1() {
        return val1.get();
    }

    public void setVal1(String val1) {
        this.val1.set(val1);
    }

    public String getVal2() {
        return val2.get();
    }

    public void setVal2(String val2) {
        this.val2.set(val2);
    }

    public String getVal3() {
        return val3.get();
    }

    public void setVal3(String val3) {
        this.val3.set(val3);
    }

    public String getVal4() {
        return val4.get();
    }

    public void setVal4(String val4) {
        this.val4.set(val4);
    }
}

1 个答案:

答案 0 :(得分:2)

您有

中的复制粘贴拼写错误
TableColumn<Counters, String> val4 = new TableColumn<Counters, String>("Value4");
val3.setCellValueFactory(new PropertyValueFactory<Counters, String>("val4"));

应该是

TableColumn<Counters, String> val4 = new TableColumn<Counters, String>("Value4");
val4.setCellValueFactory(new PropertyValueFactory<Counters, String>("val4"));

和val4始终是val4 = "",这就是为什么它们看起来都是空的