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 {
     } catch (ClassNotFoundException ex) {
                .log(Level.SEVERE, null, ex);
     } catch (InstantiationException ex) {
                .log(Level.SEVERE, null, ex);
     } catch (IllegalAccessException ex) {
                .log(Level.SEVERE, null, ex);
     } catch (UnsupportedLookAndFeelException ex) {
                .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();

        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);


    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:


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:


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


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!