【发布时间】:2018-07-02 15:56:12
【问题描述】:
我的问题是关于 tableView 中化合物的奇怪行为。 目的是在 tableView 中显示参与比赛的玩家列表。显示的信息是玩家的姓名、他的得分、他的连续失败次数以及一个指示是否轮到他上场的指示器。
这个指示器是一个 RadioButton,因为它看起来比一个复选框更好。当轮到玩家时,RadioButton 将被 setSelected(true),否则,它将被 setSelected(false)。真假信息由tableView中使用的玩家信息给出。当然,RadioButton 处于“只读”模式。
这是我的 tableView 代码:
TableView<PlayerProgressInformations> playersProgressTable = new TableView<PlayerProgressInformations>();
defineColumns(playersProgressTable);
playersProgressTable.setItems(playersAtThisTable);
对于defineColumns方法:
TableColumn<PlayerProgressInformations, Boolean> colPlaying = new TableColumn<PlayerProgressInformations, Boolean>("Tour");
colPlaying.setPrefWidth(70);
TableCell<PlayerProgressInformations, Boolean>>) new RadioButton());
colPlaying.setCellValueFactory(new Callback<CellDataFeatures<PlayerProgressInformations, Boolean>, ObservableValue<Boolean>>() {
public ObservableValue<Boolean> call(CellDataFeatures<PlayerProgressInformations, Boolean> p) {
return new SimpleBooleanProperty(p.getValue().isPlaying());
}
});
colPlaying.setCellFactory(new Callback<TableColumn<PlayerProgressInformations, Boolean>, TableCell<PlayerProgressInformations, Boolean>>() {
@Override
public TableCell<PlayerProgressInformations, Boolean> call( TableColumn<PlayerProgressInformations, Boolean> param) {
RadioButtonCell<PlayerProgressInformations, Boolean> radioButtonCell =
new RadioButtonCell<PlayerProgressInformations, Boolean>();
return radioButtonCell;
}
});
还有 RadioButtoCell 类:
private class RadioButtonCell<S, T> extends TableCell<S,T> {
public RadioButtonCell () {
}
@Override
protected void updateItem (T item, boolean empty) {
System.out.println("Count value : "+count); //Indicator to check how many times is used the method "updateItem"
count+=1;
if (item instanceof Boolean) {
Boolean myBoolean = (Boolean) item;
if (!empty) {
System.out.println("Valeur du boolean : "+item);
RadioButton radioButton = new RadioButton();
radioButton.setDisable(true);
radioButton.setSelected(myBoolean);
radioButton.setStyle("-fx-opacity: 1");
setGraphic(radioButton);
}
}
}
}
奇怪的行为如下:
问题 1:当我将第一个玩家加入游戏桌时,updateItem 方法被调用了 17 次。 如果第二个玩家加入,第一个玩家的这个数字会增加到 57,或者第二个玩家会增加到 60 和 17。 最后,如果第三个人加入,第一个玩家是 90 次,第二个是 57 或 60 次,第三个是 17 次。 为什么这种方法经常被调用?为什么这些具体数字? 更重要的是,在这个“初始化”之后,该方法在每一轮之后被调用了 2 次,正如我预期的那样:一次取消选择 RadioButton,一次选择下一个。
问题 2:当第一个玩家加入牌桌时,他当然是第一个玩的,并且在他的屏幕上选择了 RadioButton。 当第二个玩家加入牌桌时,该第二个玩家会看到为第一个玩家选择的 RadioButton 和未为自己选择的 RadioButton。这是预期的行为。但是对于第一个玩家,2 个 RadioButtons 未被选中。 如果第三位玩家加入牌桌,他将看到为第一位玩家选择了 RadioButton,而为他自己和第二位玩家取消了选择。这也是预期的结果。但是,对于第二个和第一个玩家,所有 3 个 RadioButtons 都未选中。 为什么会出现这种奇怪的行为?此外,在第一轮之后,所有 RadioButtons 都按预期显示为选中或未选中,就好像 bug 消失了一样。
您能否帮助我了解正在发生的事情以及如何解决这些错误?
非常感谢
【问题讨论】:
-
好的。我将使用您的提示来更好地了解它是如何工作的。谢谢詹姆斯
-
另外,不清楚你在描述问题时的意思:“当第二个玩家加入牌桌时,第二个玩家看到一个 RadioButton 被选中......但对于第一个玩家,2单选按钮未选中。”所以有两个屏幕?或者这是一个点对点类型的应用程序?不同玩家如何查看同一个应用程序?
-
屏幕很多。游戏在服务器上运行,每个玩家都运行一个“查看器”来玩游戏。因此,在 Eclipse 上,我启动了一个项目服务器并启动了 2 或 3 个项目客户端来模拟 2 或 3 个游戏玩家。
-
如果不同的播放器显示的内容不同,这似乎是客户端和服务器之间的通信问题。大概你有一些机制让服务器在新玩家加入时通知现有客户端等。这个问题似乎完全是一个不同的问题,与你发布的代码无关。
-
是的,你是对的!这是一个 bean 的通信问题。谢谢詹姆斯:-D
标签: java javafx radio-button tableview