Java Look And Feel Example

Now that you have learned the basic components in JavaSE, its about time to add life to your application by setting the Java Look and Feel of your application.

In our past tutorial, the default swing lookandfeel of java is not so beautiful and not the same as in other applications that we are installing. Java Look And Feel sets the overall looks of your GUI to whatever lookandfeel you want. For example, the windows lookandfeel is like this:

windows look and feel

The above source code is this:

package com.javapointers.javase;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Login {

    JFrame frame;
    JButton btnLogin, btnRegister;
    JTextField tfUsername;
    JPasswordField tfPassword;
    JLabel lblUsername, lblPassword;
    GridBagLayout gbl;
    GridBagConstraints gbc;

    public Login() {
     try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     } catch (ClassNotFoundException ex) {
            Logger.getLogger(Login.class.getName())
                .log(Level.SEVERE, null, ex);
     } catch (InstantiationException ex) {
            Logger.getLogger(Login.class.getName())
                .log(Level.SEVERE, null, ex);
     } catch (IllegalAccessException ex) {
            Logger.getLogger(Login.class.getName())
                .log(Level.SEVERE, null, ex);
     } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Login.class.getName())
                .log(Level.SEVERE, null, ex);
     }

        frame = new JFrame("Login");
        btnLogin = new JButton("Login");
        btnRegister = new JButton("Register");
        tfUsername = new JTextField(15);
        tfPassword = new JPasswordField(15);
        lblUsername = new JLabel("Username");
        lblPassword = new JLabel("Password");
        gbc = new GridBagConstraints();
        gbl = new GridBagLayout();

        frame.setLayout(gbl);
        layoutComponents(0, 0, 1, 1, lblUsername, frame);
        layoutComponents(0, 1, 3, 1, tfUsername, frame);
        layoutComponents(0, 2, 1, 1, lblPassword, frame);
        layoutComponents(0, 3, 3, 1, tfPassword, frame);
        layoutComponents(1, 4, 1, 1, btnLogin, frame);
        layoutComponents(2, 4, 1, 1, btnRegister, frame);

        frame.setSize(400, 200);
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }

    private void layoutComponents(int x, int y, int width, int height, 
                                  JComponent addThis, JFrame addTo) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        addTo.add(addThis, gbc);
    }

    public static void main(String args[]) {

        new Login();

    }
}

All the codes above are in the previous posts. The only new code is this:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

This line of code will get the system’s lookandfeel, in our case, the windows lookandfeel, and set this to our application by calling the UIManager.

There are also different look and feel that you can choose from. Other developers develop their own lookandfeel. Some are free, some are not.

Here are some java look and feel that you can use:

javax.swing.plaf.nimbus.NimbusLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
com.sun.java.swing.plaf.gtk.GTKLookAndFeel

To implement them, just replace the UIManager.getSystemLookAndFeelClassName() to the string above:

UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

The result is the nimbus look and feel:

nimbus look and feel

This concludes our tutorial in Java SE. We have discussed the JFrame, the container which holds other Java SE components, the JButton, JLabel, JTextField, JPasswordField. We also discussed Layout Managers which are the Flow Layout, Border Layout, Grid Layout, Card Layout, and GridBag Layout. Adding listeners to our java application such as the Action Listener, Mouse Listener, and Key listener was also tackled. Finally, we learned how to change the look and feel of our application in java.

Share this tutorial!