Solved

JList + setEnabled(false)

Posted on 2002-05-22
4
1,510 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
HashTable highest marks enumeration alternative 9 43
maven disable workspace resolution 1 24
Java class and jar 3 25
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

821 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