【问题标题】:Display attributes in a table在表格中显示属性
【发布时间】:2018-10-15 01:09:10
【问题描述】:

我必须在 javafx 中填充一个 2 列的表,我想知道最好的方法是什么?

基本上我有这门课

public class Name {

    private String _name;
    private Rating _rating;

    public Name(String name , Rating rating) {
        _name = name;
        _rating = Rating.NO_RATING;
    }

    public String getName() {
        return _name;
    }

    public Rating getRating() {
        return _rating;
    }
}

我想在 javafx 中填充一个表,使一列具有名称,一列具有评级。我尝试通过名称类填充并使用 getName() getRating() 方法,但这不起作用。我在网上搜索了一下,有一个东西叫属性,但是在这种情况下,我没有任何属性,我该如何使用属性呢?

【问题讨论】:

  • 请格式化您的代码。 “但这不起作用” - 显示你做了什么。
  • 您的字段似乎是不可变的;我建议在这种情况下添加 final 关键字。不过,我不确定您为什么不对 _rating 字段使用构造函数参数。如果您对项目的不变性感到满意,只需在列中使用new PropertyValueFactory("name")new PropertyValueFactory("rating") 作为cellValueFactorys。

标签: java javafx properties tableview


【解决方案1】:

在数据模型中,用属性扭曲你想在表格中显示的属性:

//data model 
public class Name {

    private SimpleStringProperty name;
    private SimpleIntegerProperty rating;

    public Name(String name , int rating) {
        this.name = new SimpleStringProperty(name);
        this.rating = new SimpleIntegerProperty(rating);
    }

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

    public int getRating() {
        return rating.get();
    }
}

并在表格列中使用这些属性

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class TableViewDemo extends Application {

    private final ObservableList<Name> data =
            FXCollections.observableArrayList(
                    new Name("Jacob", 12),
                    new Name("Isabella", 14),
                    new Name("Ethan", 5),
                    new Name("Emma", 17),
                    new Name("Michael",9));

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        stage.setTitle("Table View Sample");

        TableColumn<Name, String> nameCol = new TableColumn<>("Name");
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Name, Integer> ratingCol =  new TableColumn<>("Rating");
        ratingCol.setMinWidth(100);
        ratingCol.setCellValueFactory(new PropertyValueFactory<Name, Integer>("rating"));
        final TableView<Name> table = new TableView<>();
        table.setItems(data);
        table.getColumns().addAll(nameCol, ratingCol);

        Scene scene = new Scene(table);
        stage.setScene(scene);
        stage.show();
    }
}


编辑回复 kleopatra's comment

您还可以将属性用作单元格值工厂。将数据模型更改为:

public class Name {

    private SimpleStringProperty name;
    private SimpleIntegerProperty rating;

    public Name(String name , int rating) {
        this.name = new SimpleStringProperty(name);
        this.rating = new SimpleIntegerProperty(rating);
    }

    public SimpleStringProperty nameProperty() { return name ; }

    public SimpleIntegerProperty ratingProperty() { return rating; }
}

并通过以下方式定义列:

    TableColumn<Name, String> nameCol = new TableColumn<>("Name");
    nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
    nameCol.setMinWidth(100);

    TableColumn<Name, String> ratingCol =   new TableColumn<>("Rating");
    ratingCol.setCellValueFactory(cellData -> cellData.getValue().ratingProperty().asString()); 

【讨论】:

  • 如果您不使用内部属性,则不需要内部属性,即公开为属性以允许摆脱容易出错的 PropertyValueFactory ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2017-11-28
  • 1970-01-01
  • 2013-02-20
相关资源
最近更新 更多