2 回答
TA贡献1843条经验 获得超7个赞
经过大量调查和反复试验,下面的代码现在可以正常运行,同时提供正确的视觉反馈,使用 checkbox.setIndeterminate() 并实现与 'item.isSelected().getValue()' 配对的“当前阈值”、“最大阈值”整数变量触发复选框状态的条件。当然有更清洁的方法来实现,但这对我有用。固定为:
public void setAllRoles()
{
thrshld = 0;
ObservableList<GroupEntry> groups = this.list2.getItems();
for (GroupEntry item : groups) {
thrshld--;
}
thrshldMax = thrshld;
if (this.allRoles) {
this.allRoles = false;
for (GroupEntry item : groups) {
item.setSelected(new SimpleBooleanProperty(false));
item.isSelected().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
BooleanProperty thatCB = item.isSelected();
if (thatCB.getValue() == true) {
checkbox2.setIndeterminate(true);
thrshld++; // = thrshld + 1;
} else {
thrshld--; // = thrshld - 1;
}
if (thrshld == thrshldMax) {
checkbox2.setIndeterminate(false);
checkbox2.setSelected(false);
}
if (thrshld == 0) {
checkbox2.setIndeterminate(false);
checkbox2.setSelected(true);
}
//status.setText("state: " +thatCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
}
});
}
this.list2.refresh();
} else {
this.allRoles = true;
thrshld = 0;
for (GroupEntry item : groups) {
item.setSelected(new SimpleBooleanProperty(true));
item.isSelected().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
BooleanProperty thisCB = item.isSelected();
if (thisCB.getValue() == true) {
thrshld++; // = thrshld + 1;
if (thrshld == 0) {
checkbox2.setIndeterminate(false);
checkbox2.setSelected(true);
}
} else {
checkbox2.setIndeterminate(true);
thrshld--; // = thrshld - 1;
if (thrshld == thrshldMax) {
checkbox2.setIndeterminate(false);
checkbox2.setSelected(false);
}
}
//status.setText("state: " +thisCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
}
});
}
this.list2.refresh();
}
}
TA贡献1820条经验 获得超9个赞
这是一个简单的解决方案。您在方法声明中缺少类型。例如void
。onChanged
应该是方法,而不是构造函数。还要删除 abstract
限定符。
public void onChanged(ObservableValue<? extends GroupEntry> ov
EDIT ListChangeListener
是一个界面。您需要正确覆盖该onChanged
方法。该方法需要与定义中相同的参数:
void onChanged(ListChangeListener.Change<? extends E> c)
添加回答
举报