| Java application login form |
|
|
|
| Written by Charles | |||||
| Wednesday, 13 January 2010 22:45 | |||||
|
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. 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(); setLayout(layout); labUserName = new JLabel("User name"); tfUserName = new JTextField(15); labPassword = new JLabel("Password"); tfPassword = new JPasswordField(15); bnLogin = new JButton(loginAction); bnLogin.setDefaultCapable(true); String command = (String) loginAction.getValue(Action.ACTION_COMMAND_KEY); if ((command == null) || (command = command.trim()).equals("")) { command = LOGIN_COMMAND; } bnLogin.setText(command); add(labUserName); add(tfUserName); add(labPassword); add(tfPassword); add(bnLogin); 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, labUserName); // Set constraints for Container itself layout.putConstraint(SpringLayout.SOUTH, this, 20, SpringLayout.SOUTH, bnLogin); layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, tfPassword); } /** * 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(); } }
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Wednesday, 13 January 2010 23:03 ) |



