I'm writing an app and I want to set my Log In Form to the top of Main Form and always on top of the Main Form. I mean lock it in front of Main Form until the Log In Form closed, so User can access Main Form after the Log In Form is Closed.
Asked
Active
Viewed 1,515 times
0
-
See [this answer](http://stackoverflow.com/a/20286447/2587435) for a simple login mechanism using a model `JDialog` – Paul Samsotha Jan 13 '14 at 03:16
-
See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) And I agree with the only answer so far offered, this needs a modal dialog, or easier, a `JOptionPane` (which is a modal dialog with some convenience methods). – Andrew Thompson Jan 14 '14 at 06:39
2 Answers
3
You can user JDialog
instead of a JFrame
for making a modal frame. Following two links can help you in getting started.

Andrew Thompson
- 168,117
- 40
- 217
- 433

Jabir
- 2,776
- 1
- 22
- 31
-
Can JDialog create an Log In from like JFrame? Can you give me an example to create a JDialog to be like a JFrame? – KaJasB Jan 13 '14 at 03:20
-
2@KaJasB: a JDialog can hold the exact same components as a JFrame, but it can also act as a modal dialog, just like what you need. Read the [API for the JDialog](http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html) to learn how to construct it so its modal. Do a little experimenting on your own with this, you won't regret this. 1+ to Jabir. – Hovercraft Full Of Eels Jan 13 '14 at 06:55
-
+1 - though I'd probably use the more specialized `JOptionPane` for this use-case.. – Andrew Thompson Jan 14 '14 at 07:48
0
This follows the general idea outlined by @Jabir, but uses a JOptionPane
(which uses a modal dialog as the display component) because it has a number of convenience methods that do the heavy lifting we would otherwise need to implement in a plain modal dialog.
A dialog on the other hand, is more versatile that an option pane. As soon as we start thinking "That's great, but I'd like to alter it to.." that is when to use a JDialog
directly. An option pane is harder to change beyond the (many) options offered by the existing API.
See How to Make Dialogs for more details of using option panes.
import java.awt.*;
import javax.swing.*;
class LoginForm {
private JPanel loginForm = new JPanel(new BorderLayout(5,5));
private JTextField userID = new JTextField(12);
private JPasswordField password = new JPasswordField(8);
LoginForm() {
initializeLoginForm();
}
/**
* Displays the log-in form inside a confirmation option pane.
* The result (OK/Cancel) of the option pane is returned for inspection.
*/
public final int displayLoginForm(Component parent) {
return JOptionPane.showConfirmDialog(
parent,
loginForm,
"Log In",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
}
private final void initializeLoginForm() {
JPanel labels = new JPanel(new GridLayout(0,1,5,5));
loginForm.add(labels, BorderLayout.LINE_START);
JPanel fields = new JPanel(new GridLayout(0,1,5,5));
loginForm.add(fields);
labels.add(new JLabel("User ID:", SwingConstants.TRAILING));
labels.add(new JLabel("Password:", SwingConstants.TRAILING));
JPanel userIdConstrain = new JPanel(
new FlowLayout(FlowLayout.LEADING));
userIdConstrain.add(userID);
fields.add(userIdConstrain);
JPanel passwordConstrain = new JPanel(
new FlowLayout(FlowLayout.LEADING));
passwordConstrain.add(password);
fields.add(passwordConstrain);
}
public final String getUserID() {
return userID.getText();
}
public final char[] getPasword() {
return password.getPassword();
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JLabel l = new JLabel("<html>"
+ "<body style='width: 300px; height: 175px;>",
SwingConstants.CENTER);
JFrame f = new JFrame("Secure Application");
f.add(l);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// ensures the minimum size is enforced.
f.setMinimumSize(f.getSize());
f.setVisible(true);
LoginForm lif = new LoginForm();
int result = lif.displayLoginForm(f);
// do the approrpaite action for this result
if (result==JOptionPane.OK_OPTION) {
l.setText("Welcome " + lif.getUserID());
} else {
l.setText("This application requires authentication!");
}
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}

Andrew Thompson
- 168,117
- 40
- 217
- 433