File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with colors on JLabels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with colors on JLabels" Watch "Problem with colors on JLabels" New topic
Author

Problem with colors on JLabels

Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
I am having problems with setting colors
of JLabels and JTextFields on my JPanels/JFrames.

Specifically, the setBackground method does not
seem to work on JLabels. And I also cannot get
the JButton face to be a specific color in some
programs.

I have read the Java method descriptions several times.
I have searched the Java Ranch forums and cannot
find a similar problem.

The two code pieces at the bottom show the JLabel problem
when you run them. I have chosen really bright colors
to show the problem (I don't use them normally).

setBackground Colors are as follows;
JLabel - dark green
JTextField - dark red
JButton - dark blue
"test" - Pink

The comment items in the code are as follows:
Item 1 - this setBG is pink and does color the top JPanel.
Item 2 - this setBG is commented out. The default color
produced in the JPanel is a medium gray.
Item 3 - these setBGs should make the JLabels dark green.
Item 4 - the setBackground for the button here does change the
color to blue.

QUESTIONS:
1) When the programs run, the North JPanel has a pink
background, which it should. But the JLabel is also pink.
And in the Center JPanel with no setBackground, the color
is gray. And the JLabels are also gray.

Why do the Item 3 setBackgrounds not cause the JLabels to
be dark green? Is there a parameter
somewhere that I have not found? I have read much
in the J objects (actually the setBackground is
under the JComponent) but don't see one. Any
ideas?

2) In Item 2 above, the gray seems to be a "default" color.
I have read the Sun Java descriptions and there is
no mention of what the default colors are for the
various J objects. Can someone point me to a reference?

3) The JButton setBackgrounds for Item 4 work in this program.
In others, where I have several menus open and data is being keyed,
the setBackground only changes the border of the button. And the
face of the button is an off-white. (Another "default" color?)
The cause may be the same as Question 1 above. But if not,
any ideas?

Thank you.
Will B

Programs 1: (Run this one - it runs the real one.)



Program 2: (This is the real program.)

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Will Barbee wrote:Specifically, the setBackground method does not
seem to work on JLabels.

JLabels by default have their opaque property set to false. To have a JLabel draw its background color, you must set the opaque to true by calling setOpaque(true) on the label.


And I also cannot get
the JButton face to be a specific color in some
programs.

I know less about this except that buttons often are more look and feel dependent, and that you may need to take care with a button's borders.

For instance, if your class had these methods:


Then you could easily create your labels via...


edit: In my mind, I believe though that there's a reason that the Swing developers made a JLabel non-opaque by default because in most apps a label is mainly used to add text to something else, and that its background color is usually that of whatever holds the label.

edit 2: nice color scheme by the way! lol
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
THANKS, Pete.

I knew I had overlooked something.
Your methods work great.

WillB

P.S. Glad you like the colors. (I do them like that
so I know which are mine and which are defaults.)
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
Just another note of thanks to Pete Stein.

Those createXXX methods are really good. I added ComboBox and CheckBox.

They really keep the mainline code clean.

Will
eric aro
Greenhorn

Joined: Dec 04, 2009
Posts: 15
Will Barbee wrote:Just another note of thanks to Pete Stein.

Those createXXX methods are really good. I added ComboBox and CheckBox.

They really keep the mainline code clean.

Will


Or you could have just used a visualBuilder , and when looking at the JLabel bean attributes see that "opaque" one, and just give it a try ( yes, in a developer mind, opaque is somehow related to color, isn't it ? )

with a visual Builder you get the feedback in a matter of seconds, whereas, posting to a forum (even a nice one) is matter of hours, or days.

Regards,
eric,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Or you could have just used a visualBuilder...


Completely disagree.

When you learn the proper method in Java you learn something that will work anywhere you write Java code.

When you learn how to do it in an IDE you don't learn anything about Java and you won't know how to to solve the problem when you switch IDE's.

Its better to spend the time learning Java and the proper coding techniques, instead of depending on an IDE to generate the code for you.
eric aro
Greenhorn

Joined: Dec 04, 2009
Posts: 15
Rob Camick wrote:
Or you could have just used a visualBuilder...


Completely disagree.

When you learn the proper method in Java you learn something that will work anywhere you write Java code.

When you learn how to do it in an IDE you don't learn anything about Java and you won't know how to to solve the problem when you switch IDE's.

Its better to spend the time learning Java and the proper coding techniques, instead of depending on an IDE to generate the code for you.


JavaBeans are pretty clear:
JLabel has an attribute called "opaque", and you just have to turn it on. And that's the actual lesson he got.

Where the hell did you see any "IDE dependent way to do" ?

But the most interesting part is the "learning curve" :
I had several trainees, and without visual builders, they almost all get "stuck" on this classic pitfall. On the other hand, with a visual builder, they all give this attribute "opaque" a shot, and learn by themselves this trick.

Therefore, IMHO, It's not fair to let newbies believe that visual building is evil. They will loose so much time, and code visual interface with so much fear !! Instead drawing a GUI, can be/should be fun !

I know that's a kind of religion war. I was in the "no visual builder" side, long time ago. But I've experimented both (that's what trainees are for, aren' they?) , and, bottom line, it's pretty clear.

I don't want to convince you, but just leave the message for the newcomers to java: learn MVC, and draw you V in a visual builder.

Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
eric aro wrote:
Therefore, IMHO, It's not fair to let newbies believe that visual building is evil. They will loose so much time, and code visual interface with so much fear !! Instead drawing a GUI, can be/should be fun !

I agree, my profession is not programmer (yet, heh), Java just stayed on my head in and after school (almost 4 years now). And i would say that i feared GUI programming because it is so massive and a bit complicated area to work on. Therefore GUI Builder should be a proper tool for newbie to learn But cant say if i would have learned GUI programming any faster, if i would have used a builder for programming which i haven't (just tried)

But at the time i dont and wont use any GUI builder...

OFF TOPIC...
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
LESSON LEARNED:

Thanks again to Pete Stein for the wonderful quickies to define labels easily.

I ended up with several - JLabels, JTextFields, JComboBox, etc.

They really, really clean up messy code but also really save me time when
programming.

 
GeeCON Prague 2014
 
subject: Problem with colors on JLabels