java – TableView中RadioButton的奇怪行为
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – TableView中RadioButton的奇怪行为,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5932字,纯文字阅读大概需要9分钟。
内容图文
我的问题是关于tableView中的一个奇怪的行为.
目的是在tableView中显示参与比赛的球员列表.显示的信息是玩家的名字,他的得分,他连续的半身像的数量以及一个知道轮到他玩的指标.
这个指标是RadioButton,因为它看起来比checkBox好.当转向玩家时,RadioButton将被设置为选择(true),否则,它将被设置为选择(假).真实或虚假信息由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个RadioButton都未被选中.
为何这种奇怪的行为?更重要的是,在第一次转弯之后,所有RadioButton都会按预期显示为已选中或未选中,就像错误消失一样.
你能帮我理解发生了什么以及如何解决这些错误吗?
非常感谢你
解决方法:
示例代码
这是一个有效的表实现.我想你可以将它与你的实现进行比较,看看有什么不同之处.可能主要的“修复”是对空值和空值的updateItem处理.
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class PlayerViewer extends Application {
private final ObservableList<Player> data =
FXCollections.observableArrayList(
new Player("Jacob", true),
new Player("Isabella", false),
new Player("Ethan", true)
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableView<Player> table = new TableView<>(data);
table.setPrefHeight(130);
table.setPrefWidth(150);
TableColumn<Player, String> handleCol = new TableColumn<>("Handle");
handleCol.setCellValueFactory(new PropertyValueFactory<>("handle"));
table.getColumns().add(handleCol);
TableColumn<Player, Boolean> playingCol = new TableColumn<>("Playing");
playingCol.setCellValueFactory(new PropertyValueFactory<>("playing"));
playingCol.setCellFactory(param -> new TableCell<>() {
RadioButton indicator = new RadioButton();
{
indicator.setDisable(true);
indicator.setStyle("-fx-opacity: 1");
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
protected void updateItem(Boolean isPlaying, boolean empty) {
super.updateItem(isPlaying, empty);
if (empty || isPlaying == null) {
setGraphic(null);
} else {
indicator.setSelected(isPlaying);
setGraphic(indicator);
}
}
});
table.getColumns().add(playingCol);
stage.setScene(new Scene(table));
stage.show();
}
public static class Player {
private final SimpleStringProperty handle;
private final SimpleBooleanProperty playing;
private Player(String handle, boolean playing) {
this.handle = new SimpleStringProperty(handle);
this.playing = new SimpleBooleanProperty(playing);
}
public SimpleStringProperty handleProperty() {
return handle;
}
public String getHandle() {
return handle.get();
}
public void setHandle(String handle) {
this.handle.set(handle);
}
public SimpleBooleanProperty playingProperty() {
return playing;
}
public boolean isPlaying() {
return playing.get();
}
public void setPlaying(boolean playing) {
this.playing.set(playing);
}
}
}
对您的问题的其他评论
在“问题1”中,对于调用updateItem的次数,这是一个内部工具包的事情,你的代码不应该真正关心它,它只需要确保无论何时调用它,它都做正确的事情.
关于你的“问题2”,对于多个玩家的多个视图的交互,谁知道?没有进一步的额外代码就不可能说,这可能最终会使这个问题过于宽泛.如果您有关于如何处理多个玩家的显示交互的特定问题,您需要扩展并澄清您的问题(可能是mcve的新问题).
对于您的实现,我建议重新设置单选按钮(通过CSS),以使其看起来不像标准的用户可选单选按钮(因为您已禁用选择功能,然后删除了默认的禁用不透明度设置).或者,您可以使用自定义指标控件,例如this answer中的Bulb类,这可能是首选.
内容总结
以上是互联网集市为您收集整理的java – TableView中RadioButton的奇怪行为全部内容,希望文章能够帮你解决java – TableView中RadioButton的奇怪行为所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。