【问题标题】:Many table columns with getter and setter许多带有 getter 和 setter 的表列
【发布时间】:2017-01-17 04:53:42
【问题描述】:

我知道如果我有 2 列,我必须像下面这样设置 getter 和 setter

private SimpleIntegerProperty num;
private SimpleStringProperty name;

public GradeCat1st1(){
    this.num = new SimpleIntegerProperty();
    this.name = new SimpleStringProperty();
}

public GradeCat1st1(Integer num, String name){
    this.num = new SimpleIntegerProperty(num);
    this.name = new SimpleStringProperty(name);
}

public Integer getNum(){
    return num.get();
}

public void setNum(Integer num){
    this.num.set(num);
}

public String getName(){
    return name.get();
}

public void setName(String name){
    this.name.set(name);
}

这是对属性值代码的调用。

tcName.setCellValueFactory(new PropertyValueFactory("name"));
tcNum.setCellValueFactory(new PropertyValueFactory("num"));

但是如果我有超过 10 或 20 列,我是否必须将每列都放入 getter 和 setter?

我想做类似 simpleStringproperty 的数组列表,但不知道。

【问题讨论】:

    标签: arraylist javafx tableview getter-setter tablecolumn


    【解决方案1】:

    通过使用自定义cellValueFactory,您可以从自定义数据结构中检索数据。

    我建议使用ObservableList 并使用Bindings 类检索数据。以下示例还显示了如何使表格可编辑。

    @Override
    public void start(Stage primaryStage) {
        TableView<ObservableList<String>> table = new TableView<>(FXCollections.observableArrayList(
                FXCollections.observableArrayList("A", "B", "C"),
                FXCollections.observableArrayList("D")
        ));
        // find size of longest list
        final int columns = table.getItems().stream().mapToInt(ObservableList::size).max().orElse(0);
    
        table.setEditable(true);
    
        for (int i = 0; i < columns; i++) {
            TableColumn<ObservableList<String>, String> column = new TableColumn<>("column "+i);
            column.setCellFactory(TextFieldTableCell.forTableColumn());
            final int index = i;
    
            // use index in list corresponding to column index
            column.setCellValueFactory(cd -> Bindings.valueAt(cd.getValue(), index));
    
            // write to index on a commit
            // (necessary, since Bindingx.valueAt doesn't return a WritableValue)
            column.setOnEditCommit(evt -> {
                ObservableList<String> item = evt.getRowValue();
                String newValue = evt.getNewValue();
                if (item.size() <= index) {
                    // pad with null
                    for (int j = item.size(); j < index; j++) {
                        item.add(null);
                    }
    
                    item.add(newValue);
                } else {
                    item.set(index, newValue);
                }
            });
            table.getColumns().add(column);
        }
    
        // button for printing the contents to console to show the lists are updated
        Button btn = new Button("Print");
        btn.setOnAction((ActionEvent event) -> {
            System.out.println(table.getItems());
        });
    
        Scene scene = new Scene(new VBox(table, btn));
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多