2 回答

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個贊
經(jīng)過大量調(diào)查和反復(fù)試驗(yàn),下面的代碼現(xiàn)在可以正常運(yùn)行,同時提供正確的視覺反饋,使用 checkbox.setIndeterminate() 并實(shí)現(xiàn)與 'item.isSelected().getValue()' 配對的“當(dāng)前閾值”、“最大閾值”整數(shù)變量觸發(fā)復(fù)選框狀態(tài)的條件。當(dāng)然有更清潔的方法來實(shí)現(xiàn),但這對我有用。固定為:
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貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個贊
這是一個簡單的解決方案。您在方法聲明中缺少類型。例如void
。onChanged
應(yīng)該是方法,而不是構(gòu)造函數(shù)。還要刪除 abstract
限定符。
public void onChanged(ObservableValue<? extends GroupEntry> ov
EDIT ListChangeListener
是一個界面。您需要正確覆蓋該onChanged
方法。該方法需要與定義中相同的參數(shù):
void onChanged(ListChangeListener.Change<? extends E> c)
添加回答
舉報