Updated Lesson: Setting the Look and Feel in Swing

The architecture of Swing is designed so that you may change the "look and feel" (L&F) of your application's GUI. "Look" refers to the appearance of GUI widgets (more formally, JComponents) and "feel" refers to the way the widgets behave.

In the Swing Tutorial, the How to Set the Look and Feel page has been updated to reflect the L&Fs that are currently available for a particular operating system.

Two commonly used L&Fs are:

  1. The CrossPlatformLookAndFeel—this is the "Java L&F" (also called "Metal") that looks the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the default that will be used if you do nothing in your code to set a different L&F.
  2. The SystemLookAndFeel—here, the application uses the L&F that is native to the system it is running on. The System L&F is determined at runtime, where the application asks the system to return the name of the appropriate L&F.

Code examples show how your program can set the L&F. An extended example allows you to select alternative L&Fs and see the results.

New Information in the Updated Lesson

There are two new additions to this lesson:
  • A discussion of Themes.
  • The SwingSet2 Demonstration Program

Themes were introduced as a way of easily changing the colors and fonts of the cross-platform Java (Metal) Look and Feel.

The SwingSet2 demonstration program allows you to select different L&Fs from a menu (provided that they will run on your system) so you can see what they look like. You can also choose different Themes from a menu when you are running the cross-platform Java (Metal) L&F. You can launch this demonstration program directly from the lesson using Java Web Start.

-- Tom Risser

Comments:

Personally when catching the exceptions from setting a PLAF I would catch the generic Exception rather than go into details. Its simpler and people might be intimidated by everything that can go wrong. After all it isn't likely that cross platform or system look and feel will throw any exception, so you only need to go into these for 3rd party PLAF's and even then when an exception is thrown I just have a fallback no need to handle all the cases...

I would also add some small tutorial about customizing the UIManager to put properties into the PLAF. This is a subject that isn't covered in the JavaDoc and understood only by advanced Swing developers yet is a remarkable power of the Swing PLAF concept! We can use the UIManager to increase the font size throughout the application without going to individual components. That is a huge use case that I personally leveraged in a touch screen application (larger font increases widgets so they are big enough for human fingers).

Posted by Shai Almog on December 22, 2006 at 04:52 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Blog about Java technology documentation and news about Java releases.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today