Setting the JAVA Look and Feel

The following code should be contained in the main method fo the main class.

You DO NOT need to set the look and feel of ever swing class in your project.

The look and feel in the main method provides scope accress all swing classes of the application.

Add the following import for the UI Manager.

import javax.swing.UIManager;

Add the following code at the top of the main method in the main class.
This will set the look and feel of the current platform.

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
    System.out.println(“Error setting native LAF: ” + e);
}

Advertisements

2 thoughts on “Setting the JAVA Look and Feel

  1. Thanks Rob!

    I have a basic question for a laf expert: is it possible to have multiple look and feels in one app? here’s my problem, my app uses laf A and I want a popup that displays a JFrame to keep its original look and feel, Thanks a lot

    • Hi Elsanto

      When the look and feel is set it defines the laf for the scope of the whole application, so any subclasses called after setting the laf will inherit its properties. You can override the initial laf with a new one by adding the following code to the constructor of the subclass:

      try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch(Exception e) {
      System.out.println(”Error setting native LAF: ” + e);
      }

      Selecting the look and feel required from the UIManager.
      You could aslo use the following for example to get a different look and feel for a subclass:

      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s