JavaFX-在表格视图中显示数据

时间:2019-04-23 18:06:42

标签: java javafx tableview fxml

我想在GUI的表视图中显示数据库中的数据。

我尝试了不同的方法都无济于事。该代码运行没有错误,但在表视图中仍然没有任何显示。

能帮您澄清一下我对此的理解。


public class LeaderBoardView implements Initializable {

    @FXML private TableColumn<LeaderBoardData, String> colUsername;
    @FXML private TableColumn<LeaderBoardData, Integer> colWins;
    @FXML private TableColumn<LeaderBoardData, Integer> colWinPercentage;
    @FXML private TableView<LeaderBoardData> tableLeaderboard;
    private ObservableList<LeaderBoardData> data;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        colUsername.setCellValueFactory(
                new PropertyValueFactory<LeaderBoardData,String>("username"));
        colWins.setCellValueFactory(
                new PropertyValueFactory<LeaderBoardData, Integer>("wins"));
        colWinPercentage.setCellValueFactory(
                new PropertyValueFactory<LeaderBoardData, Integer>("winPercentage"));

        buildLeaderBoardData();

    }


    public void buildLeaderBoardData() {
        data = FXCollections.observableArrayList();
        try {
            ResultSet rs = UserDB.getLeaderBoardData();

            while(rs.next()){
                LeaderBoardData cm = new LeaderBoardData();

                cm.username.set(rs.getString("username"));
                cm.wins.set(rs.getInt("wins"));
                cm.winPercentage.set(rs.getInt("winPercentage"));
                data.add(cm);

                //System.out.println(data.get());
            }
            tableLeaderboard.setItems(data);

        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Error on Building Data");
        }
    }


public class LeaderBoardData {

    public SimpleStringProperty username;
    public SimpleIntegerProperty wins;
    public SimpleIntegerProperty winPercentage;


//    public UserData(String username, int wins, int winPercentage) {
//        this.username = new SimpleStringProperty(username);
//        this.wins = new SimpleIntegerProperty(wins);
//        this.winPercentage = new SimpleIntegerProperty(winPercentage);
//    }


    public String getUsername() {
        return username.get();
    }



    public Integer getUserWins() {
        return wins.get();
    }



    public Integer getWinPercent() {
        return winPercentage.get();
    }



}

预期结果-获取表格视图以显示用户,获胜率和获胜百分比排行榜

1 个答案:

答案 0 :(得分:2)

每个Java Bean属性名称都通过删除方法名称的其余部分getdecapitalizing从相应的get方法的名称派生而来。不会以任何方式考虑字段,只会考虑方法。

您的LeaderBoardData类具有这些get-method,它们对应于以下属性:

  • getUsername()→属性"username"
  • getUserWins()→属性"userWins"
  • getWinPercent()→属性"winPercent"

您必须完全使用上述属性名称:

colUsername.setCellValueFactory(
        new PropertyValueFactory<LeaderBoardData,String>("username"));
colWins.setCellValueFactory(
        new PropertyValueFactory<LeaderBoardData, Integer>("userWins"));
colWinPercentage.setCellValueFactory(
        new PropertyValueFactory<LeaderBoardData, Integer>("winPercent"));

更好的方法是完全避免反射。像您这样的反射错误不能被编译器捕获,但是直接调用方法的代码由编译器检查。

首先,您需要使LeaderBoardData类以大多数JavaFX类提供它们的标准方式提供属性:一个属性方法和一个get方法(如果该属性是可写的,则使用set方法) 。 javafx.scene.Node documentation说明了这一点。

在您的情况下,您的课程如下所示:

public class LeaderBoardData {

    private StringProperty username = new SimpleStringProperty();
    private IntegerProperty wins = new SimpleIntegerProperty();
    private IntegerProperty winPercentage = new SimpleIntegerProperty();

    public StringProperty usernameProperty() {
        return username;
    }

    public String getUsername() {
        return username.get();
    }

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

    public IntegerProperty userWinsProperty() {
        return wins;
    }

    public int getUserWins() {
        return wins.get();
    }

    public void setUserWins(int wins) {
        this.wins.set(wins);
    }

    public IntegerProperty winPercentProperty() {
        return winPercentage;
    }

    public int getWinPercent() {
        return winPercentage.get();
    }

    public void setWinPercent(int percent) {
        this.winPercentage.set(percent);
    }
}

现在,您的单元格值工厂可以使用显式方法调用:

colUsername.setCellValueFactory(f -> f.getValue().usernameProperty());
colWins.setCellValueFactory(f -> f.getValue().userWinsProperty());
colWinPercentage.setCellValueFactory(f -> f.getValue().winPercentProperty());