I have done some basic desktop applications using Java Swing. I however want to use my skills to develop a more complex application that involves user authentication.
I want to have an opening window that will contain JTextField
s to accept the user's username and password. On successful authentication (checking the user details in the database), I want a new window displayed containing some information about the user.
How can I achieve this multi-window effect using Swing?
Thanks!
Asked
Active
Viewed 2,818 times
-1

Akinkunle Allen
- 1,299
- 12
- 19
-
1What did you try and what problem do you encounter? – Raphaël Feb 21 '14 at 16:43
-
1Seems like you need to create a custom dialog to perform what you want. JDialog should be useful, or may use JInternalFrame within a JDesktopPane. They are also good solutions. – Marcelo Tataje Feb 21 '14 at 16:45
-
1Use Dialog boxes for username and password with a button at the bottom to submit them. – Shrey Feb 21 '14 at 16:46
-
I second Marcelo and @Scarecrow's recommendation. Note that a JOptionPane can function well in this capacity. The object parameter can accept a JPanel with your small sign-in GUI including JLabels, JTextFields and JPasswordFields. – Hovercraft Full Of Eels Feb 21 '14 at 16:48
-
For example, please have a look at [this answer](http://stackoverflow.com/a/10150308/522444) for how to use JOptionPanes to create complex modal dialogs. – Hovercraft Full Of Eels Feb 21 '14 at 17:05
2 Answers
2
- Create a
JDialog
that prompts for user details. - Show the dialog.
- Get the credentials from the dialog*.
- Authenticate.
- Create a
JFrame
for your application. - Show the frame.
*Don't authenticate from the dialog, just keep your UI to doing UI things.

Qwerky
- 18,217
- 6
- 44
- 80
2
To quote Blue Peter, "Here's one I made earlier". I've implemented it as a JPanel
and then embed it within a JDialog
in the main
method. Note that there's a hook to cause the password field to gain focus when the dialog is first displayed.
class LoginPanel extends JPanel {
private final JTextField userNameTxtFld;
private final JPasswordField passwordFld;
public LoginPanel() {
super(new GridBagLayout()); // GridBagLayout: Not everyone's bag.
this.userNameTxtFld = new JTextField(12);
this.passwordFld = new JPasswordField(12);
userNameTxtFld.setText(System.getProperty("user.name"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = INSETS;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridx = 0;
gbc.gridy = 0;
int row = 0;
addLabelledComponent("User Name:", userNameTxtFld, this, gbc, 0, row++);
//noinspection UnusedAssignment
addLabelledComponent("Password:", passwordFld, this, gbc, 0, row++);
}
private boolean gainedFocusBefore;
void gainedFocus() {
if (!gainedFocusBefore) {
gainedFocusBefore = true;
passwordFld.requestFocusInWindow();
}
}
String getUserName() {
return userNameTxtFld.getText();
}
String getPassword() {
return new String(passwordFld.getPassword());
}
void reset() {
passwordFld.setText("");
}
public static void main(String[] args) {
final LoginPanel pnl = new LoginPanel();
JOptionPane op = new JOptionPane(pnl, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog dlg = op.createDialog("Login");
dlg.addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowGainedFocus(WindowEvent e) {
pnl.gainedFocus();
}
@Override
public void windowLostFocus(WindowEvent e) {
}
});
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dlg.setVisible(true);
System.exit(0);
}
}

Adamski
- 54,009
- 15
- 113
- 152