Java- Changing swing background colour? - java

Ok so ive got a swing app going using the "System" look and feel. Now, I want to change the background colour of the main panels to black. Too easy right?
UIManager.put("Panel.background", Color.BLACK);
Well yeah, except now the controls in the app look stupid, because their 'shadows', for want of a better word, are graduated to fade towards the old system default colour(gross windows grey). So there are light grey 'corners' on all the controls, especially the tabs on JTabbedPane.
I know it can be fixed, because if you change the windowsXP theme to one with a different default application colour, the controls take on this changed colour and their shadows 'fade' towards it.
But I have no idea what UIManager key it is, or even if you can do it with UIManger.
I dont really want to change the L&F engine, because apart from this it looks good.

You might try these:
control
controlDkShadow
controlHighlight
controlLtHighlight
controlShadow
(I just found them in this list: Swing [Archive] - UIManager: setting background and JScrollBar )

In general this is a little bit tricky. It depends on exact LaF you are using.
For example. JGoodies use own color scheme which redefine this stuff.
In general the property names are composed like
COMPONENT_NAME_WITHOUT_J + '.' + PROPERTY.
Unfortunately, property names can be only obtained from implementation classes of LaF. These aren't shared or something. Each component has its own. Or better, it depends on laziness of author which pairs he used. In general.
A lot of help makes redefine Panel.* and Button.. A lot of components use Button. properties.
Try, play, win :). I wish you luck :).
PS: It is a lot of properties to overwrite. But this is the way how LaFs works.

You can see what the default settings (and their keys) are by using UIManager.getDefaults();
You can then iterate over the resulting keySet (it is an instance of Map).
So something like this will show all the default keys:
for (Object key: UIManager.getDefaults().keySet())
{
System.out.println(key);
}

Some controls like JButton need to have setOpaque(false) called to allow the new background colors to fade through.

To list out all the possible options that we can set to UIManager to change LaF, run the code below ........
import java.util.*;
import javax.swing.UIManager;
public class UIManager_All_Put_Options
{
public static void main (String[] args)
{
Hashtable properties = UIManager.getDefaults();
Enumeration keys = properties.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
Object value = properties.get (key);
System.out.printf("%-40s \t %-200s \n", key,value);
}
}
}
enjoy...

Related

what do these swing color names mean

Some of the names are clear, like background, foreground, focus etc. But some are just confusing, like light, hightlight, shadow, darkshardow etc. I noticed that these are consistently used in swing UI, so I infer these are part of java's jargon. Does any body know if there is a document out there that explains these names ?
RadioButton.background
RadioButton.darkShadow
RadioButton.disabledText
RadioButton.focus
RadioButton.foreground
RadioButton.highlight
RadioButton.light
RadioButton.select
RadioButton.shadow
These are UIResource elements related to JRadionButton. Each Look & Feel provides different radio button appearance, and may set different defaults for these elements. It is also to up to L&F implementation to use these keys or not.
For example, here is a method from javax.swing.plaf.basic.BasicBorders that uses RadioButton.light and RadioButton.highlight:
public static Border getRadioButtonBorder() {
UIDefaults table = UIManager.getLookAndFeelDefaults();
Border radioButtonBorder = new BorderUIResource.CompoundBorderUIResource(
new BasicBorders.RadioButtonBorder(
table.getColor("RadioButton.shadow"),
table.getColor("RadioButton.darkShadow"),
table.getColor("RadioButton.light"),
table.getColor("RadioButton.highlight")),
new MarginBorder());
return radioButtonBorder;
}
However, it is may not be used by concrete L&F implementations.
PS:
UIManager Defaults by #camickr can be handy to visualize different keys.

Change text color of Substance LookAndFeel

I'm trying to change the text color of the Substance Look and Feel (in fact I'm using the SubstanceGraphiteGlassLookAndFeel .
I don't know how to do that...
Ultimately you have to mess with the color schemes.
Since you are using the Graphite Glass skin, messing with the color schemes is considerably easier since they values are not set in Java code. If you copy he file /org/pushingpixels/substance/api/skin/graphite.colorschemes into your classpath, you can go into the various scheme definitions and change the colorForeground entries to be whatever you want. Possibly black (#FFFFFF) or the various greys (#cccccc, #999999, #666666, #333333). But since they are hex colors you can do whatever you want.
change all Keys in UIManager by killing all things that to create this great theme
change value for all Keys in UIManager for all JComponents or concrete JComponent
change value for all value in Highlighter(s) for all JComponents or concrete JComponent
I'm not sure if this theme to use or not the Trident too, have to check on former Kirill's forum
or simpler to change for concrete JComponents instance, have to repeated for all JComponents
disclaimer
never to change Colors or Fonts for Custom Look and Feels, have to check if is or isn't there direct way implemented by author of L&F, never to tried that for all JComponents and this theme isn't my favorite, then point second is safer by eliminate side effect implemented in Custom L&F, but in this case you probably to loose implemented Highlighter(s)

Is it possible to remove the little dropdown arrow in a JInternalFrame?

I'm using a JInternalFrame and I want to remove the dropdown in the upper left of the frame, as it serves no purpose (I've disabled resizeable, closable, etc.)
I don't see a property for this, and I don't want to remove the entire title bar, as the title is necessary. I've looked around online, and basically come up empty here. I'm hoping it's something simple that I've just overlooked, as this is my first time using JInternalFrame, and I'm not exactly a GUI kind of guy to begin with.
internalframe.setFrameIcon(null);
Edit: hack to remove system menu in Windows:
BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame.getUI();
Container north = (Container)ui.getNorthPane();
north.remove(0);
north.validate();
north.repaint();
The relevant Icon in The Synth Look and Feel, among the Nimbus Defaults, appears to have this key:
InternalFrame:InternalFrameTitlePane:"InternalFrameTitlePane.menuButton".icon
You may be able to use the approach shown in A Synth Example to replace the Icon.
setFrameIcon(anyBigImageThatCantBeDisplayed);
I´ve tried null parameter and got some visual issues...
So i added a big image(no background) that was already on my resource folder and the icon was no longer displayed as the menu...

LookAndFeel-independent reference of color keys

I am currently working on a set of custom controls for a product of the company I'm working in. For this, I am extending a lot of Swing controls and also overriding a lot of paint methods.
In order to maintain a consistent color scheme, I receive the colors for my paint, setBackground etc. methods using UIManager.getColor.
This was perfectly fine until we noticed that the Nimbus LookAndFeel, which is shipped with current JRE versions, uses totally different color keys, thus many things looks totally out of place.
For instance, while all other stock LookAndFeels (Metal, Windows Classic, Windows, CDE/Motif, GTK) have defined the key "text" as a bright background for texts and "textText" as the corresponding foreground color, "text" in Nimbus is actually a black foreground color, and a standard text background color does not seem to exist.
"TextField.background" would work, but that, for instance, doesn't exist for the Windows LookAndFeels.
I suppose you get the problem by now. I don't want to have to maintain a set of color keys for each LAF, who knows what LAFs will be added in the future and which my company may decide to use.
A simple solution would be getting rid of Nimbus, of course, but understandably my boss doesn't like this idea at all, besides Nimbus is part of the JRE these days and should be supported.
So I wonder whether there is any standardized way to get LAF-dependent colors like, say, "text background / foreground", "selected text bg/ fg", etc.?
I'm not sure that there is a "standardized" way of getting these values.
As you noticed, Nimbus uses its own names for colors. Specifically, the properties textForeground and textBackground.
This oddness is likely because Nimbus uses a small selection of basic colors (listed as Primary in the chart), which have Secondary colors calculated from them, which in turn are used as the basis for all the remaining colors.
Yea, as #josefx implied, unfortunately this isn't how the UIs work. They're not a pool of generic portable properties, rather they're a set of actual components and specific implementations for each of those components. It's not really an extensible system that's friendly to custom components.
The level of abstraction IS the component, not something finer grained. If you try to ask for a ComponentUI for a component that the L&F does not know about, you're out of luck. And the ComponentUI is little more than a wrapper for a paint method, so it's not obligated to expose any meta data at all.
Simply put, you're stuck basically doing the JTextField (or some other appropriate component) "color scrape" technique that josefx suggests, or adding specific support in your code to handle the eccentricities of the property names of the L&F's that you wish to support well.
Another suggestion is to "pre-empt" the L&F change, and subclass the L&Fs you wish to support to make your components more of a first class citizen within those L&Fs. When the L&F changes to a L&F that you support, silently switch out their class name with your subclasses class name, and then implement ComponentUIs for your custom components, and extend the parent LookAndFeel.createUI method so that it "knows" about your new components.
None of these are pretty, but the Swing component system isn't designed to be extensible at run time to handle custom components. The entire component suite is done all at once when the L&F is created.
There is no way around it - you have to create your own abstraction layer for color names (and possibly other property names).
Basically you have stock LookAndFeels (Metal, Windows Classic, Windows, CDE/Motif, GTK) that use their own color names and Nimbus that uses different names.
Create a class e.g. LafProperties so that for each property/color you have method (e.g. "getTextColor"). This class returns properties for classic Laf styles. Then extend this class for Nimbus, and change only methods that differ in Nimbus.
If stock Lafs and Nimbus have most of the properties differently named, than use you might use an interface and two implementing classes.
Not a nice way but it should work for the basics:
Create a JTextField
call getForeground,getBackground,getSelectionColor to get the laf dependent values
Update:
The ComponentUI class and its subclasses which are the base classes for all look and feels only provide a method to initialise the laf dependent default values, they provide no direct way to access these values.
The java.awt.SystemColor class provides variables for a lot of the common attributes.
For text foreground/background use the member fields text/textText; for selected text, use textHighlight/textHighlightText.
Few things ppop into my mind reading your question, which is a tad spiced with underbelly frustration if I have sensed well:
dependancy on JRE standards/support versus independancy/freedom solutions
standards & conformity versus creativity & customisation
And thus ultimately:
practical approach of your boss versus programmers far future insights

Reset/remove a border in Swing

Here's a very specific coding question:
I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:
I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:
input.setBorder(BorderFactory.createLineBorder(Color.RED));
This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.
Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.
Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?
Or without caching anything, you could tell the JComponent to update its UI back to the look and feel's defaults via component.updateUI. That should make the component reset its colors, borders, fonts, etc to match the original settings.
input.getBorder()
Wouldn't it be awesome if no one ever saw this and I got away free without the ass-beating I deserve for asking this question?
Not sure how your system is build, but I think you can store the original border before changing it. So you can change it back later
// assuming the Border was not null before
if (!super.verify(input)) {
original = input.getBorder();
input.setBorder(...);
} else {
if (original != null) {
input.setBorder(original);
original = null; // not needed
}
}
You need to preserve the existing border when you change it.
One way to do this is to use the methods putClientProperty() and getClientProperty(), which you'll find documented in the API.
Another possibility, if there are only a few input widgets you need this for is to subclass, e.g. JTextField, add setBorderOverride() and modify getBorder() to return "overriddingBorder" if it is not null.
Then you just use setBorderOverride(redBorder) to make it red and setBorderOverride(null) to clear it.
This of course depends on the painting to use getBorder(), which it may or may not do, and which may be implementation specific.
Incidentally, you only need a single static reference to the border-- it's the selfsame border instance used by all JTextFields.

Categories