JavaFX - 如何在 TableView 中以与一个 ObservableList 不同的顺序显示两个表列的值?

发布于 2025-01-17 04:56:16 字数 1366 浏览 0 评论 0原文

这里的大学生目前正在从事锦标赛应用程序项目,我遇到了 GUI 的当前问题:

我的匹配算法采用玩家的号码并将最低号码与最高号码进行匹配,例如,如果我们有 8 名玩家,那么我们将有; P1 与 P8、P2 与 P7 等等。我目前正在尝试使用两列在 TableView 中显示它。这意味着同一行上第一列需要显示 P1,第二列需要显示 P8。遗憾的是,我无法弄清楚如何进行这项工作,因为当前的代码将玩家堆叠在一起,如下所示:

在此处输入图像描述

我当前的代码如下:

@FXML
private void initialize() {
    playerViewer.setPlaceholder(new Label("Players will be shown here"));

    SingleElimTournament tournament = new SingleElimTournament();

    for(int i = 1; i < 9; i++) {
        tournament.addPlayer(new Player("Player " + i, i));
    }

    int start = 0; //The id for first player according to a seed
    int end = tournament.getPlayerAmount() - 1; //The id for last player according to a seed
    while (start < end) {
        playerList.add(tournament.getPlayersList().get(start));
        playerList.add(tournament.getPlayersList().get(end));

        start++; //Moving on to the next match
        end--; //Moving on to the next match
    }

    columnOne.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));
    columnTwo.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));

    playerViewer.setItems(playerList);
}

我需要更改什么用于显示的列敌方球员并排而不是互相压在上面?非常感谢任何建议。

University student here currently working on a tournament application project, and I've got this current issue with GUI:

My matchmaking algorithm takes the player's numbers and matches the lowest number with the highest, for example if we have 8 players then we will have; P1 vs P8, P2 vs P7 and so on. I am currently trying to display this in a TableView using two columns. This means that column one needs to show P1, and column two needs to show P8 on the same row. I am sadly unable to figure out how to make this work, because the current code stacks the players on top of eachother like this:

enter image description here

My current code for this is as follows:

@FXML
private void initialize() {
    playerViewer.setPlaceholder(new Label("Players will be shown here"));

    SingleElimTournament tournament = new SingleElimTournament();

    for(int i = 1; i < 9; i++) {
        tournament.addPlayer(new Player("Player " + i, i));
    }

    int start = 0; //The id for first player according to a seed
    int end = tournament.getPlayerAmount() - 1; //The id for last player according to a seed
    while (start < end) {
        playerList.add(tournament.getPlayersList().get(start));
        playerList.add(tournament.getPlayersList().get(end));

        start++; //Moving on to the next match
        end--; //Moving on to the next match
    }

    columnOne.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));
    columnTwo.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));

    playerViewer.setItems(playerList);
}

What would I need to change for the columns to display the opposing players side-to-side instead of on top of eachother? Any advice is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2025-01-24 04:56:16

表中的每一行代表两名玩家的比赛,而不是单个玩家。因此,TableView 的数据类型需要是代表两个玩家的数据类型。

例如定义一个记录

record Match(Player first, Player second) {} ;

@FXML
private TableView<Match> playerViewer ;
@FXML
private TableColumn<Match, Player> columnOne ;
@FXML
private TableColumn<Match, Player> columnTwo ;

private ObservableList<Match> matchList = FXCollections.observableArrayList();

@FXML
private void initialize() {
    playerViewer.setPlaceholder(new Label("Players will be shown here"));

    SingleElimTournament tournament = new SingleElimTournament();

    for(int i = 1; i < 9; i++) {
        tournament.addPlayer(new Player("Player " + i, i));
    }

    int start = 0; //The id for first player according to a seed
    int end = tournament.getPlayerAmount() - 1; //The id for last player according to a seed
    while (start < end) {
        Player player1 = tournament.getPlayersList().get(start);
        Player player2 = tournament.getPlayersList().get(end);

        Match match = new Match(player1, player2);
        matchList.add(match);

        start++; //Moving on to the next match
        end--; //Moving on to the next match
    }

    columnOne.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue().first()));
    columnTwo.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue().second()));

    Callback<TableColumn<Match, Player>, TableCell<Match, Player>> cellFactory = col ->
        new TableCell<>() {
            @Override
            protected void updateItem(Player player, boolean empty) {
                super.updateItem(player, empty);
                if (empty || player == null) {
                    setText("");
                } else {
                    setText(player.getName());
                }
            }
        };

    columnOne.setCellFactory(cellFactory);
    columnTwo.setCellFactory(cellFactory);

    playerViewer.setItems(matchList);
}

Each row in the table represents a match with two players, not a single Player. So the data type for your TableView needs to be a data type that represents two players.

E.g. define a record:

record Match(Player first, Player second) {} ;

@FXML
private TableView<Match> playerViewer ;
@FXML
private TableColumn<Match, Player> columnOne ;
@FXML
private TableColumn<Match, Player> columnTwo ;

private ObservableList<Match> matchList = FXCollections.observableArrayList();

@FXML
private void initialize() {
    playerViewer.setPlaceholder(new Label("Players will be shown here"));

    SingleElimTournament tournament = new SingleElimTournament();

    for(int i = 1; i < 9; i++) {
        tournament.addPlayer(new Player("Player " + i, i));
    }

    int start = 0; //The id for first player according to a seed
    int end = tournament.getPlayerAmount() - 1; //The id for last player according to a seed
    while (start < end) {
        Player player1 = tournament.getPlayersList().get(start);
        Player player2 = tournament.getPlayersList().get(end);

        Match match = new Match(player1, player2);
        matchList.add(match);

        start++; //Moving on to the next match
        end--; //Moving on to the next match
    }

    columnOne.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue().first()));
    columnTwo.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue().second()));

    Callback<TableColumn<Match, Player>, TableCell<Match, Player>> cellFactory = col ->
        new TableCell<>() {
            @Override
            protected void updateItem(Player player, boolean empty) {
                super.updateItem(player, empty);
                if (empty || player == null) {
                    setText("");
                } else {
                    setText(player.getName());
                }
            }
        };

    columnOne.setCellFactory(cellFactory);
    columnTwo.setCellFactory(cellFactory);

    playerViewer.setItems(matchList);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文