- Java com sun java swing plaf windows windowslookandfeel
- How to Set the Look and Feel
- Available Look and Feels
- Programatically Setting the Look and Feel
- Specifying the Look and Feel: Command Line
- Specifying the Look and Feel: swing.properties File
- How the UI Manager Chooses the Look and Feel
- Changing the Look and Feel After Startup
- An Example
- Themes
- The SwingSet2 Demonstration Program
Java com sun java swing plaf windows windowslookandfeel
Implements the Windows95/98/NT/2000 Look and Feel. UI classes not implemented specifically for Windows will default to those implemented in Basic.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. A future release of Swing will provide support for long term persistence.
- author: unattributed —
Field Summary | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
static final Object | HI_RES_DISABLED_ICON_CLIENT_KEY | A client property that can be used with any JComponent that will end up calling the LookAndFeel.getDisabledIcon method. This client property, when set to Boolean.TRUE, will cause getDisabledIcon to use an alternate algorithm for creating disabled icons to produce icons that appear similar to the native Windows file chooser |
Fields inherited from javax.swing.plaf.basic.BasicLookAndFeel: |
---|
needsEventHelper, invocator |
Method from com.sun.java.swing.plaf.windows.WindowsLookAndFeel Summary: |
---|
createAudioAction, getDescription, getDisabledIcon, getID, getLayoutStyle, getName, initClassDefaults, initComponentDefaults, initSystemColorDefaults, initialize, isClassicWindows, isMnemonicHidden, isNativeLookAndFeel, isOnVista, isSupportedLookAndFeel, provideErrorFeedback, repaintRootPane, setMnemonicHidden, uninitialize |
Methods from javax.swing.plaf.basic.BasicLookAndFeel: |
---|
access$000, access$100, access$200, access$202, createAudioAction, getAudioActionMap, getDefaults, getUIOfType, initClassDefaults, initComponentDefaults, initSystemColorDefaults, initialize, installAWTEventListener, installAudioActionMap, loadSystemColors, playSound, playSound, uninitialize |
Methods from javax.swing.LookAndFeel: |
---|
getDefaults, getDescription, getDesktopPropertyValue, getDisabledIcon, getDisabledSelectedIcon, getID, getLayoutStyle, getName, getSupportsWindowDecorations, initialize, installBorder, installColors, installColorsAndFont, installProperty, isNativeLookAndFeel, isSupportedLookAndFeel, loadKeyBindings, makeComponentInputMap, makeIcon, makeInputMap, makeKeyBindings, provideErrorFeedback, toString, uninitialize, uninstallBorder |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from com.sun.java.swing.plaf.windows.WindowsLookAndFeel Detail: | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Platform | Look and Feel |
---|---|
Solaris, Linux with GTK+ 2.2 or later | GTK+ |
Other Solaris, Linux | Motif |
IBM UNIX | IBM* |
HP UX | HP* |
Classic Windows | Windows |
Windows XP | Windows XP |
Windows Vista | Windows Vista |
Macintosh | Macintosh* |
* Supplied by the system vendor.
You don’t see the System L&F in the API. The GTK+, Motif, and Windows packages that it requires are shipped with the Java SDK as:
Note that the path includes java , and not javax .
All of Sun’s L&Fs have a great deal of commonality. This commonality is defined in the Basic look and feel in the API ( javax.swing.plaf.basic ). The Motif and Windows L&Fs are each built by extending the UI delegate classes in javax.swing.plaf.basic (a custom L&F can be built by doing the same thing). The «Basic» L&F is not used without being extended.
In the API you will see four L&F packages:
javax.swing.plaf.basic —basic UI Delegates to be extended when creating a custom L&F
javax.swing.plaf.metal —the Java L&F, also known as the CrossPlatform L&F («Metal» was the Sun project name for this L&F) The current default «theme» (discussed below) for this L&F is «Ocean, so this is often referred to as the Ocean L&F.
javax.swing.plaf.multi —a multiplexing look and feel that allows the UI methods to delegate to a number of look and feel implementations at the same time. It can be used to augment the behavior of a particular look and feel, for example with a L&F that provides audio cues on top of the Windows look and feel. This is a way of creating a handicapped-accessible look and feel.
javax.swing.plaf.synth —an easily configured L&F using XML files (discussed in the next section of this lesson)
You aren’t limited to the L&Fs supplied with the Java platform. You can use any L&F that is in your program’s class path. External L&Fs are usually provided in one or more JAR files that you add to your program’s class path at runtime. For example:
Once an external L&F is in your program’s class path, your program can use it just like any of the L&Fs shipped with the Java platform.
Programatically Setting the Look and Feel
The L&F that Swing components use is specified by way of the UIManager class in the javax.swing package. Whenever a Swing component is created,the component asks the UI manager for the UI delegate that implements the component’s L&F. For example, each JLabel constructor queries the UI manager for the UI delegate object appropriate for the label. It then uses that UI delegate object to implement all of its drawing and event handling.
To programatically specify a L&F, use the UIManager.setLookAndFeel() method with the fully qualified name of the appropriate subclass of LookAndFeel as its argument. For example, the bold code in the following snippet makes the program use the cross-platform Java L&F:
Alternatively, this code makes the program use the System L&F:
You can also use the actual class name of a Look and Feel as the argument to UIManager.setLookAndFeel() . For example,
You aren’t limited to the preceding arguments. You can specify the name for any L&F that is in your program’s class path.
Specifying the Look and Feel: Command Line
You can specify the L&F at the command line by using the -D flag to set the swing.defaultlaf property. For example:
Specifying the Look and Feel: swing.properties File
Yet another way to specify the current L&F is to use the swing.properties file to set the swing.defaultlaf property. This file, which you may need to create, is located in the lib directory of Sun’s Java release (other vendors of Java may use a different location). For example, if you’re using the Java interpreter in javaHomeDirectory\bin , then the swing.properties file (if it exists) is in javaHomeDirectory\lib . Here is an example of the contents of a swing.properties file:
How the UI Manager Chooses the Look and Feel
Here are the look-and-feel determination steps that occur when the UI manager needs to set a L&F:
- If the program sets the L&F before a look and feel is needed, the UI manager tries to create an instance of the specified look-and-feel class. If successful, all components use that L&F.
- If the program hasn’t successfully specified a L&F, then the UI manager uses the L&F specified by the swing.defaultlaf property. If the property is specified in both the swing.properties file and on the command line, the command-line definition takes precedence.
- If none of these steps has resulted in a valid L&F, Sun’s JRE uses the Java L&F. Other vendors, such as Apple, will use their default L&F.
Changing the Look and Feel After Startup
You can change the L&F with setLookAndFeel even after the program’s GUI is visible. To make existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components. For example:
An Example
In the following example, LookAndFeelDemo.java , you can experiment with different Look and Feels. The program creates a simple GUI with a button and a label. Every time you click the button, the label increments.
You can change the L&F by changing the LOOKANDFEEL constant on line 18. The comments on the preceding lines tell you what values are acceptable:
Here the constant is set to «Motif», which is a L&F that will run on any platform (as will the default, «Metal»). «GTK+» will not run on Windows, and «Windows» will run only on Windows. If you choose a L&F that will not run, you will get the Java, or Metal, L&F.
In the section of the code where the L&F is actually set, you will see several different ways to set it, as discussed above:
You can verify that both arguments work by commenting/un-commenting the two alternatives.
Here is a listing of the LookAndFeelDemo source file:
Themes
Themes were introduced as a way of easily changing the colors and fonts of the cross-platform Java (Metal) Look and Feel. In the sample program, LookAndfeelDemo.java , listed above, you can change the theme of the Metal L&F by setting the THEME constant on line 23 to one of three values:
Ocean , which is a bit softer than the pure Metal look, has been the default theme for the Metal (Java) L&F since Java SE 5. Despite its name, DefaultMetal is not the default theme for Metal (it was before Java SE 5, which explains its name). The Test theme is a theme defined in TestTheme.java , which you must compile with LookAndfeelDemo.java . As it is written, TestTheme.java sets the three primary colors (with somewhat bizarre results). You can modify TestTheme.java to test any colors you like.
The section of code where the theme is set is found beginning on line 92 of LookAndfeelDemo.java . Note that you must be using the Metal L&F to set a theme.
The SwingSet2 Demonstration Program
When you download the JDK and JavaFX Demos and Samples bundle and open it up, there is a demo\jfc folder that contains a demonstration program called SwingSet2 . This program has a graphically rich GUI and allows you to change the Look and Feel from the menu. Further, if you are using the Java (Metal) Look and Feel, you can choose a variety of different themes. The files for the various themes (for example, RubyTheme.java ) are found in the SwingSet2\src folder.
This is the «Ocean» theme, which is the default for the cross-platform Java (Metal) Look and Feel:
This is the «Steel» theme, which was the original theme for the cross-platform Java (Metal) Look and Feel:
To run the SwingSet2 demo program on a system that has the JDK installed, use this command:
This will give you the default theme of Ocean.