Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to change Java Application Look and Feel?

$
0
0

Java Look & FeelJava Swing API renders the GUI in the Java Look and Feel but at times we want it use a certain look and feel like Motif or Windows or want to change look and feel dynamically from the application itself based on user selection etc. Its very easy to change the l&f of the application using the UIManager. This is generally done at the startup of the application with using the setLookAndFeel method. The following code snippets shows how to do the same.

<!--break-->

 

package com.livrona.snippets.gui;

import java.awt.BorderLayout;
import java.awt.Label;

import javax.swing.JFrame;
import javax.swing.UIManager;

/**
* This snippet shows how to changes the look an feel
* of the gui application.
*
* @author mvohra
*
*/

publicclass LookAndFeel {

/**
* Main Method
*
* @param args
*/

publicstaticvoid main(String args[]) {

try {

// use one of the following method based on the desired look& feel

// set the look & feel based on OS itself
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

// java metal look and feel works across platform
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());

// set the Motif look and feel
String motifClassName ="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
UIManager.setLookAndFeel(motifClassName);

// set the look & feel based on
} catch (Exception e) {
System.out.println("Error failed to set the Look & Feel"
+ e.getMessage());
e.printStackTrace();
}

// rest of the gui code like creating frame etc. goes here
// 1. Create the frame.
JFrame frame =new JFrame("FrameDemo");

// 2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 3. Create components and put them in the frame.
// ...create emptyLabel...
frame.getContentPane().add(new Label("See the Look and Feel"),
BorderLayout.CENTER);

// 4. Size the frame.
frame.pack();

// 5. Show it.
frame.setVisible(true);

}
}




Viewing all articles
Browse latest Browse all 178

Trending Articles