I spent quite a few minutes today trying to find a login form, usable in a Java application and was surprised at being unable to find one, so I decided to write one myself.

For the purposes of login functionality, it makes sense to use a CardLayout - one card for the login form and another for the 'logged in' application. Below, however, we can see just the login form, the source code of which is HERE.
The parameter to the constructor is a javax.swing.AbstractAction that will perform authentication when logging in. It can been seen in a sample application HERE.

import java.awt.Component;
import java.awt.Container;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class LoginForm extends JPanel {
        private static final String LOGIN_COMMAND = "Log in";
        private JLabel labUserName;
        private JTextField tfUserName;
        private JLabel labPassword;
        private JPasswordField tfPassword;
        private JButton bnLogin;

        public LoginForm(AbstractAction loginAction) {
                SpringLayout layout = new SpringLayout();

                labUserName = new JLabel("User name");
                tfUserName = new JTextField(15);
                labPassword = new JLabel("Password");
                tfPassword = new JPasswordField(15);

                bnLogin = new JButton(loginAction);

                String command = (String) loginAction.getValue(Action.ACTION_COMMAND_KEY);

                if ((command == null) || (command = command.trim()).equals("")) {
                        command = LOGIN_COMMAND;



                layout.putConstraint(SpringLayout.WEST, labUserName, 10,
                        SpringLayout.WEST, this);
                layout.putConstraint(SpringLayout.NORTH, labUserName, 25,
                        SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.NORTH, tfUserName, 25,
                        SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.WEST, tfUserName, 20,
                        SpringLayout.EAST, labUserName);

                layout.putConstraint(SpringLayout.WEST, labPassword, 10,
                        SpringLayout.WEST, this);
                layout.putConstraint(SpringLayout.NORTH, labPassword, 50,
                        SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.NORTH, tfPassword, 50,
                        SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.WEST, tfPassword, 20,
                        SpringLayout.EAST, labUserName);

                layout.putConstraint(SpringLayout.NORTH, bnLogin, 75,
                        SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.WEST, bnLogin, 0, SpringLayout.WEST,
                // Set constraints for Container itself
                layout.putConstraint(SpringLayout.SOUTH, this, 20, SpringLayout.SOUTH,
                layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST,

         * Get the contents of the user name field
         * @return The value of the user name field
        public String getUserName() {
                return tfUserName.getText();

         * Get the password from the password field
         * @return The password as char[]
        public char[] getPassword() {
                return tfPassword.getPassword();