Link to home
Start Free TrialLog in
Avatar of chencc77
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.getColor("List.textBackground"));
               setForeground(UIManager.getColor("List.textForeground"));
          }

          public Component getListCellRendererComponent(JList list, Object value,
               int index, boolean isSelected, boolean hasFocus) {
               setEnabled(list.isEnabled());
               setSelected(((CheckableItem)value).isSelected());
               setFont(list.getFont());
               setText(value.toString());
               return this;
          }
     }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chencc77
chencc77

ASKER

Hi objects, this is the CheckableItem class, what should i edit. thanks.

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;
          }
     }
You should not call setSelected if the list is disabled.
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.
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