O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

434
|
Chapter 11, Native Integration and Packaging
#86 Make Quick Look and Feel Changes
HACK
H A C K
#86
Make Quick Look and Feel Changes
Hack #86
Customize Metal with custom fonts, colors, and even system-bound
properties using just a few API calls.
Swing is a very customizable UI toolkit. The most advanced way of chang-
ing the look of your application is with a custom Look and Feel (L&F), but
they can be tricky to build. Swing’s L&F API is very complicated, often
requiring thousands of lines of code for a complete custom theme. Fortu-
nately, if you want to change just a few colors or fonts, the L&F API pro-
vides a much easier way. This hack shows how to create simple visual
changes using UI properties.
Look and Feel Properties
Every Look and Feel that extends the javax.swing.plaf.basic.* classes can
accept special properties that define the behavior of each Swing component.
For example, there is property to control the background color of every
JButton. If you change this property at the start of your program, then every
JButton you create will have that background color.
The UI properties are stored in a static class called the
UIManager. To set a
property, just put in the name of the property and a value object like a
Color. To set the background color of a button to green, you would do
something like this:
UIManager.put("Button.background", Color.green);
It is important to set these properties before any compo-
nents are created or they won’t pick up the new settings.
Below is the code for a simple program that shows a few components in a
frame. It has a button, label, and text field along the top and a text area in a
scroll pane in the middle. There is also a simple file menu at the top. Before
creating any components, it sets the foreground and background colors for
the button, label, text field, and panel to light and dark green:
public static void main(String[] args) throws Exception {
Color bg = Color.green.brighter( );
Color fg = Color.green.darker( );
UIManager.put("Button.background",bg);
UIManager.put("Button.foreground",fg);
Make Quick Look and Feel Changes #86
Chapter 11, Native Integration and Packaging
|
435
HACK
UIManager.put("Label.background",bg);
UIManager.put("Label.foreground",fg);
UIManager.put("TextField.background",bg);
UIManager.put("TextField.foreground",fg);
UIManager.put("Panel.background",bg);
UIManager.put("Panel.foreground",fg);
JTextArea jta = new JTextArea( );
jta.setText("text\ntext\ntext\ntext\ntext\ntext"+
"\ntext\ntext\ntext\ntext\ntext");
JScrollPane scroll = new JScrollPane(jta);
JButton button = new JButton("A Button");
JLabel label = new JLabel("A Label");
JTextField text = new JTextField("A TextField");
JMenuBar mb = new JMenuBar( );
JMenu file = new JMenu("File");
file.add(new JMenuItem("Open"));
file.add(new JMenuItem("Close"));
mb.add(file);
JFrame frame = new JFrame("Custom LaF Defaults");
JPanel top = new JPanel( );
top.setLayout(new BoxLayout(top,BoxLayout.X_AXIS));
top.add(button);
top.add(label);
top.add(text);
JPanel panel = new JPanel( );
panel.setLayout(new BorderLayout( ));
panel.add("North",top);
panel.add("Center",scroll);
frame.getContentPane( ).add(panel);
frame.setJMenuBar(mb);
frame.pack( );
frame.setSize(300,200);
frame.setVisible(true);
}
When compiled, this will look like Figure 11-11.
Swing provides over 300 properties that define the colors and fonts of each
standard component. This lets you customize almost anything in your pro-
gram. You can even set a color to be transparent, which may look interest-
ing if you have a pattern background.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required