【发布时间】:2020-02-22 00:48:30
【问题描述】:
我从事 Java FX 工作。
我有一个表格视图,在其中创建了一个“选择复选框”列。目标是当用户单击复选框时,会出现一条警告消息。
为了测试它,我首先尝试显示 System.out.println 消息。
问题是:
- 当我只选择一个复选框时,我会收到一条 System.out.println 消息,其中会写入所有复选框:
com.calculatrice.app.model.Person@34752060 com.calculatrice.app.model.Person@654bd7bc com.calculatrice.app.model.Person@74bbebe0 com.calculatrice.app.model.Person@2e23be4f com.calculatrice.app.model.Person@3348edcb com.calculatrice.app.model.Person@8052a29 com.calculatrice.app.model.Person@23ca3422 com.calculatrice.app.model.Person@102837d2 com.calculatrice.app.model.Person@647ab6a9
- 当我什么都不选择时,我有相同的 System.out.println 消息
我怎么能有一个与项目对应的消息,对应于选中的复选框?
这是我的 sn-p:
private void selectCheckBox(ActionEvent ae) {
// personTable is the Tableview ; Person is the class where the getter is declared
for(Person p : personTable.getItems()){
if (p.getSelect().isSelected()){
System.out.println (p + " is selected");
}
}
}
【问题讨论】:
-
我认为覆盖
toString方法将有助于解决这个问题。好吧,我想这不是一个真正的问题,因为这是预期的行为。 -
好的,谢谢您的回答。可以问一下怎么做吗?
-
请提供一个minimal reproducible example 来说明问题。
-
我不知道你的实现是什么,但如果 Person 上的选定字段不是完整的 BooleanProperty 并且你没有使用 CheckboxTableCell,你可能应该是。您可能还存在我们看不到的代码中的其他问题。