JavaFX - 如何在 TableView 中以与一个 ObservableList 不同的顺序显示两个 table 列的值?
JavaFX - how to display values of two table columns in TableView with different order from one ObservableList?
这里的大学生目前正在从事锦标赛应用程序项目,我遇到了 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);
}
我需要对列进行哪些更改才能并排显示对方球员而不是彼此重叠?非常感谢任何建议。
table 中的每一行代表一场有两名球员的比赛,而不是一个 Player
。所以你的 TableView
的数据类型需要是代表两个玩家的数据类型。
例如定义一个 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);
}
这里的大学生目前正在从事锦标赛应用程序项目,我遇到了 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);
}
我需要对列进行哪些更改才能并排显示对方球员而不是彼此重叠?非常感谢任何建议。
table 中的每一行代表一场有两名球员的比赛,而不是一个 Player
。所以你的 TableView
的数据类型需要是代表两个玩家的数据类型。
例如定义一个 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);
}