Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JList + setEnabled(false)

Posted on 2002-05-22
4
Medium Priority
?
1,617 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 200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

688 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