my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Is this a bug..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is this a bug..?" Watch "Is this a bug..?" New topic

Is this a bug..?

shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
Hi friends,

I downloded a java file from sun's swing tutorial. (

I edited the code and added the following two lines.

menu.setBackground(Color.RED);// This is the first line that I added
submenu.setBackground(Color.GREEN);// This is the seccond line that I added

It worked as expected when I tested with java version "1.4.2_04"
But, unfortunately, it is not working with java version "1.5.0"
The background color is set properly when I use java version other than "1.5.0"
But when I use java version "1.5.0", The back ground is still the default background color.

Am I doing anything wrong..?
Or is this a bug..?
I tested in windows 98 and XP. Both produced the same result.
So I guess the problem is with jre.
Here is the code

Shahabas E Shabeer

The greatest pleasure in life is doing what peoples say you can not do.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The evaluation in this bug report points toward the change from j2se 1.4 to 1.5

Method setBackground() does not work when JMenu is a submenu

which is that JMenu is non–opaque by default in j2se 1.5 to accomodate the Ocean LookAndFeel. You can try this before you instantiate any menus

As an aside, this is why it is recommended that we call setOpaque(true) on any JComponent that is used as an argument in the JFrame setContentPane method.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Indeed, at this point it's a good rule of thumb to explicitly set components opaque (or not opaque) because what they instantiate to is not always predictable. As the above poster hinted at, this can be a problem with JPanel's as well, especially when used for a content pane.
shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
Yes .. you are right..
I think the transparancy of the component depends on the look and feel used.
The code that I posted worked fine when I set native look and feel than the default swing look and feel.
I do now believe that it is always better to explicitly set the transparancy of the component.
Thanks a lot for all your help
Shahabas E Shabeer
I agree. Here's the link:
subject: Is this a bug..?
It's not a secret anymore!