• Java Look And Feel

    Now that you have learned basic components in javase, its about time to add life to your application by setting the lookandfeel of your application. In our past tutorial, the default swing lookandfeel of java is not so beautiful and not the same like in other application that we are installing. LookAndFeel sets the overall looks of your GUI to whatever lookandfeel you want. For example, the windows lookandfeel is like this:

    Untitled

     

    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;
    
    /**
     *
     * @author javapointers
     */
    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 past chapters. The only new code is

    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 lookandfeel that you can choose from. Some developers develop their own lookandfeel. Some are free, some are not.

    Some lookandfeel are as follows:

    • 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:

    Untitled

    Related Post