Login Frame

As for simple starter (not too simple), let us learn how to build code for simple login frame with background image and a simple function that will exist the frame upon cancel button pressed.



package app.ams.gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.SoftBevelBorder;

/**
 *
 * @author prajin
 */
public class Login extends JDialog {

    private JLabel loginIdLbl;
    private JLabel passwordLbl;
    private JTextField loginIdTxt;
    private JPasswordField passwordTxt;
    private JButton okBtn;
    private JButton cancelBtn;
    Container container;
    Border border;
    Dimension dimension;
    JSeparator separator;

    public Login() {
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        setUndecorated(true);
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        border = new SoftBevelBorder(5, Color.BLACK, Color.DARK_GRAY);
        Font font = new Font("Batang", 0, 15);
        setBackground(Color.LIGHT_GRAY);
        setLayout(null);

        loginIdLbl = new JLabel("Login ID : - ");
        loginIdLbl.setBounds(10, 15, 100, 25);
        loginIdLbl.setFont(font);
        passwordLbl = new JLabel("Password : - ");
        passwordLbl.setBounds(10, 50, 100, 25);
        passwordLbl.setFont(font);

        loginIdTxt = new JTextField(35);
        loginIdTxt.setBounds(120, 10, 270, 25);
        loginIdTxt.setFont(font);
        loginIdTxt.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent fe) {
                txtFieldFocusGain(fe);
            }
        });

        passwordTxt = new JPasswordField(35);
        passwordTxt.setBounds(120, 50, 270, 25);
        passwordTxt.setFont(font);
        passwordTxt.requestFocusInWindow();
        passwordTxt.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent fe) {
                txtFieldFocusGain(fe);
            }
        });

        passwordTxt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
            }
        });

        loginIdTxt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                passwordTxt.requestFocusInWindow();
            }
        });

        separator = new JSeparator();
        separator.setOrientation(0);
        separator.setBackground(Color.GRAY);
        separator.setBounds(120, 80, 270, 10);
        okBtn = new JButton("OK");
        okBtn.setBounds(120, 90, 120, 30);
        okBtn.setFont(font);
        okBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

            }
        });
        cancelBtn = new JButton("Cancel?");
        cancelBtn.setBounds(270, 90, 120, 30);
        cancelBtn.setFont(font);
        cancelBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
//                dispose();
                System.exit(0);
            }
        });
        pack();

        add(loginIdLbl);
        add(loginIdTxt);
        add(passwordLbl);
        add(passwordTxt);
        add(separator);
        add(okBtn);
        add(cancelBtn);
        ImageIcon icon = createImageIcon("/app/ams/images/login_background.png");
        JLabel lbl = new JLabel(icon);
        lbl.setSize(400, 130);
        add(lbl);

        setSize(400, 130);
        setResizable(false);
        setLocation(((int) dimension.width / 2) - 200, ((int) dimension.height / 2) - 65);
        setVisible(true);
    }

    private void txtFieldFocusGain(FocusEvent ae) {
        loginIdTxt.setBorder(null);
        passwordTxt.setBorder(null);
        loginIdTxt.setBackground(Color.WHITE);
        passwordTxt.setBackground(Color.WHITE);
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Login.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Login login = new Login();
            }
        });
    }
}

Here’s the output of login window frame : –

Swing based Login Frame

Leave a comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.