Solved

JList + setEnabled(false)

Posted on 2002-05-22
4
1,569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

617 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