Solved

JList + setEnabled(false)

Posted on 2002-05-22
4
1,479 Views
Last Modified: 2010-08-05
Hi I am using a JList

When I use setEnabled(false);
the JList gets disabled but the entries in it I mean
the text content also gets grayed out

I want to keep the text color as usual so that it is
easier to read even if JList is disabled.

What shoud I do ?

Is there any UIManager method I can use so that
even if JList is disabled the foreground text
still remains usual.


Please help
0
Comment
Question by:sanjay_thakur
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7027962
You need to install your own ListCellRenderer that ignores the enabled property. Try this:

    JList list = new JList(...);
    list.setCellRenderer(new DefaultListCellRenderer() {
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {

            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            this.setEnabled(true);
            return this;
        }
    });

JLabel is the component that is typically used to draw the items of a JList. DefaultListCellRenderer extends JLabel and configures itself in the getListCellRendererComponent method. The only difference between DefaultListCellRenderer and the above is that I explicitly call setEnabled(true) so that the JLabel will paint itself as if enabled. This has no functional impact -- only presentation. The JList will still not allow selection but the text will not be greyed.

Best regards,
Jim Cakalic
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 7027973
Forgot to mention that I don't think setting a UIResource through UIManager is the right solution here -- although a good idea. There are no List resources for controlling disabledForeground color. There's List.foreground, List.background, List.selectionForeground, and List.selectionBackground, but no List.disabledForeground. That's again because JLabel is used as the ListCellRenderer. To effect the desired change using a UIResource you'd have to set Label.disabledForeground to the same color as Label.foreground. That might work, but I haven't tried it.

Jim
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 7028346
Hi Jim,

I just did this and it works fine

 UIManager.put("Label.disabledForeground" , Color.BLACK);

But I have one question though


somehow the foreground text in JList doesnot appear as plain text
rather it looks raised or with some 3D effect

Is this normal.

I am not sure if this is happening because I am using Jdk1.4 merlin

Any ideas?

0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 7044924
Thanks

The answer did solve my problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

932 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

10 Experts available now in Live!

Get 1:1 Help Now