Solved

Disabled JCheckBox in JList

Posted on 2002-07-04
7
513 Views
Last Modified: 2012-06-27
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;
          }
     }
0
Comment
Question by:chencc77
  • 3
  • 2
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 30 total points
Comment Utility
Your renderer just handles painting the list.
You must have some code elsewhere that toggles the CheckableItem value, it is that code that you need to disable.
0
 

Author Comment

by:chencc77
Comment Utility
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;
          }
     }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
You should not call setSelected if the list is disabled.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:chencc77
Comment Utility
hi, i am not clear with ur comment that how to not call setSelected.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now