Solved

JList + setEnabled(false)

Posted on 2002-05-22
4
1,460 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks

The answer did solve my problem.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 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

17 Experts available now in Live!

Get 1:1 Help Now