chencc77
asked on
Disabled JCheckBox in JList
Hi, I am using a JList that contain JCheckBox, i use the follow ListRender to add the checkbox into JList. In my code, sometimes i need to disabled the JList, but the problem is the JList has been disabled but the JCheckBox still can be checked.
//list renderer
class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public CheckListRenderer() {
setBackground(UIManager.ge tColor("Li st.textBac kground")) ;
setForeground(UIManager.ge tColor("Li st.textFor eground")) ;
}
public Component getListCellRendererCompone nt(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled( ));
setSelected(((CheckableIte m)value).i sSelected( ));
setFont(list.getFont());
setText(value.toString());
return this;
}
}
//list renderer
class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public CheckListRenderer() {
setBackground(UIManager.ge
setForeground(UIManager.ge
}
public Component getListCellRendererCompone
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled(
setSelected(((CheckableIte
setFont(list.getFont());
setText(value.toString());
return this;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should not call setSelected if the list is disabled.
ASKER
hi, i am not clear with ur comment that how to not call setSelected.
Somewhere in your code you must call setSelected as a result of a checkbox being selected in the list.
If the list is disabled then you should not be doing this.
If the list is disabled then you should not be doing this.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- points to objects
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
girionis
Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
- points to objects
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
girionis
Cleanup Volunteer
ASKER
class CheckableItem {
private String str;
private boolean isSelected;
public CheckableItem(String str) {
this.str = str;
isSelected = false;
}
public void setSelected(boolean b) {
isSelected = b;
}
public boolean isSelected() {
return isSelected;
}
public String toString() {
return str;
}
}