Changing colour of panel title in Java Swing
Asked Answered
P

4

6

I am running a Swing application on Win7 using a 144 dpi flatscreen monitor. The titles of my frames, option panes, etc., all appear in white on a transparent background (Aero?). I like Aero in general, but the white titles are very hard to read! I have gone through all the UIManager properties I can think of and none of them seem to have any effect. Is there a level below the UIManager that I can get to - and preferably make the change once off for my app, as I don't want to have to add code for every single option pane invocation. Maybe I am missing something very basic... but help would be appreciated!

I'd like to change the title fonts as well, but this is not as urgent!

Also, BTW, do you put UIManager.put calls before or after setting the Look and Feel? Or doesn't it matter?


More info: I changed over my Win7 personalization to use an Aero theme called Canada, and the titles are now in black, using a reasonable font. So my question becomes:

How do UIManager attributes relate to Win7 themes, and, given that I can't predict what theme users will be using, how do I control the pane title appearance?


I am going to put this question another way: why can't I change the colour of every pixel in my application's display panel? Is there a level below the UIManager, which seems to be very limited in what you are allowed to modify...?

Penneypenni answered 27/11, 2011 at 15:46 Comment(2)
+1 good question, I'm still WinXp user, what' happened when you to try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());Excruciation
I believe that the reason you can't easily control the look and feel of the border and top strip of a top-level window is because this is enforced from outside by the operating system, not internally by Java. The easiest way to change this is for the user to change the operating system look settings. To do this internally with Java would require JNI or JNA and the code would vary depending on the OS.Generation
E
5

This way will be little bit complicated, but if you needed own Container with full-control,

1) create Un_decorated JFrame/JDialog

2) on the NORTH possition you can put JPanel with GradientPaint that simulating container that came from Win7

3) and put 3 JButtons with Icon to the JPanel with GradientPaint

4) add required events to the JButtons and for nicer output to the GUI would be to set JButtons#setContentAreaFilled(false)

Excruciation answered 27/11, 2011 at 18:1 Comment(4)
Thanks, @mKorbel, I didn't know about GradientPaint! However, as you say, this seems a bit over-complicated. I am guessing that the Nimbus LAF may have tools to do what I need, but I couldn't figure out how to change my fonts on the fly with Nimbus (defaultfont didn't work for me), so I will give this problem a rest for now. We can see which answer gets the most votes in a few weeks' time :-) Thanks anyway for your help.Penneypenni
@Paul Morrison you have to check this #8288644 and another one #8247089Excruciation
its actually very simple, you can even use graphics 2d to do thisBeshrew
unless you plan on making it look exactly like the windows bar :PBeshrew
G
3

Use the setUndecorated(false) method to turn off the window decorations of the host platform.

Below is an example of this, though it is for the Mac OS platform, it may provide you with a starting point. Re-paint on translucent frame/panel/component.

Goatsucker answered 27/11, 2011 at 15:55 Comment(1)
I think you meant setUndecorated(true), but that gets rid of the entire top of the frame (including minimize, maximize, close)! Plus I would have to it for all panes. I have therefore set up a new Win7 screen theme using the picture and background colour I want, and that fixes my problem, but IMO it's not a general solution... I guess you can't get there from here...!Penneypenni
D
1

Perhaps this tutorial will be of help; it explains how to change the look and feel of the decoration of a JFrame. It also has a demonstration program so you can examine the differences.

By default, the look and feel of the decoration of a JFrame is managed by the OS since this way, applications fit more nicely with the overall look and feel of the platform one is using. But you can change that and control it yourself; there is no need to remove the decorations and "fake" them within your frame.

The downside is that if you set the look and feel for the decorations yourself instead of letting the OS do that, you might have to think about what look and feel to use so your window doesn't clash with any platform the application might be run from.

Dissever answered 10/12, 2011 at 15:47 Comment(2)
Thanks, that puts it into perspective for me. However, it seems that, if I use the System L&F (which I am), I am saying that I basically want my display to look like the system's. If I then want to override specific features, it should be honoured. Also, with the default personalization that was installed on my system, some of the Windows System L&F look was very hard to read - and users should not have to change their personalization to make a particular application look good. BTW everybody helped with good answers, so I will have to allow the bounty to be handled automatically.Penneypenni
I agree, the developer's choice to override specific aspects of the system's native L&F should be honoured. Possibly this can be achieved by first getting a clone of the platform's L&F, then changing the decoration of the frame as described in my answer, and using the clone with one's own adjustments as the (then developer-controlled) L&F. However, I'm not sure this will work; possibly the native L&F cannot be translated to a Java object (and thus not manipulated as desired) if it isn't part of the JVM. Not sure how that works.Dissever
B
0

If you want control of the window decorators, then use JFrame.setDefaultLookAndFeelDecorated(boolean). Set this to true, then set your Look and Feel.

To see this work for you, set the decorators (as above) first, then select a Look and Feel that is more usable (even though it may be ugly), such as Metal: Steel, or Metal: Ocean:

java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel -Dswing.metalTheme=steel MyApp.jar

or

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

Verifying that this is what you want, you can come back and change the look and feel or its color palette.

Boatman answered 8/12, 2011 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.