我有一个奇怪的复选框选择问题,使用带有自定义CheckBoxTreeCell的CheckBoxTreeItems,在JavaFX 8 TreeView中具有Children的节点.
问题是必须单击带有子项的节点的复选框两次而不是一次才能被选中.叶子只需要一次点击即可.
我的CheckBoxTreeItems采用Person Objects.我重写了CheckBoxTreeCells中的updateItem()方法,将显示的值设置为TreeCell中Person的名称.如果我没有在我的overcover updateItem方法中调用setText(),TreeCell会显示我的Person对象的默认toString()方法(这不是我想要的),并且所有节点在选择它们的复选框时都会表现出预期的行为.
我不想在Person类中更改默认的toString,所以我看到的唯一解决方法是为Person编写一个Wrapper类,在其toString()中返回Persons名称.但我更愿意正确解决这个问题,而不是使用解决方法!
有任何想法吗?非常感谢帮助!
这是我使用的代码:
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class TreeUtilTest extends Application {
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud)