I've looked around online at how to open a new JFrame from an existing JFrame. I've found that apparently the best way to do this is dispose of the existing JFrame and open the new JFrame - however this is a problem.
I have a login form, one the users logs in, the login frame is disposed and the main frame is set visible.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class client {
public static void main(String[] args) {
initialize();
}
private static void initialize() {
JFrame loginFrame = new JFrame("Login");
loginFrame.setBounds(100, 100, 300, 300);
loginFrame.setResizable(false);
loginFrame.setLocationRelativeTo(null);
loginFrame.setDefaultCloseOperation(loginFrame.HIDE_ON_CLOSE);
loginFrame.getContentPane().setLayout(null);
JFrame mainFrame = new JFrame("Main");
mainFrame.setBounds(100, 100, 300, 197);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(null);
JButton loginButton = new JButton("Login");
loginButton.setBounds(102, 133, 89, 23);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButton.setEnabled(false);
loginFrame.dispose();
mainFrame.setVisible(true);
}
});
loginFrame.getContentPane().add(loginButton);
loginFrame.setVisible(true);
}
}
However if the user launches the client and then decides not to login and closes it, the process remains running in the background?
I feel like this is a really stupid question and I am sorry if so, but I've looked around and couldn't find any workarounds for this. Am I ok to not dispose of the login frame and just hide it and set them both to EXIT_ON_CLOSE?
Thanks in advance!
So, the primary issue is, you have two frames, while not visible, both have been "realised", meaning that until ALL the application windows are disposed of, the Event Dispatching Thread won't exit, which means the JVM won't exit.
So, I suggest a slight change in approach. Rather then using two frames this way, the login "window" should be based on a modal dialog and the application frame shouldn't be created until you need it.
A modal dialg will stop the execution of the code at the point it's made visible in away that won't block the Event Dispatching Thread (it's black magic), this means you can use it a loop to keep prompting the user for credentials until they are either authenticated or the close/cancel the dialog.
I would also strongly encourage the use of JPanels as the base component, allowing the window based classes to just be containers, this isolates responsibility, decouples the code and makes for a more re-usable solution overall.
You can have a look at How to Make Dialogs for more details
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
LoginPane loginPane = new LoginPane();
boolean authenticated = false;
boolean exit = false;
do {
int option = JOptionPane.showOptionDialog(null,
loginPane,
"login",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
new Object[]{"Login", "Cancel"},
"Login");
if (option == 0) {
// Authenticate
authenticated = true;
} else if (option == JOptionPane.CLOSED_OPTION || option == 1) {
exit = true;
}
} while (!authenticated && !exit);
if (authenticated) {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
});
}
public class LoginPane extends JPanel {
private JTextField userName;
private JPasswordField password;
public LoginPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("User name: "), gbc);
gbc.gridy++;
add(new JLabel("Password: "), gbc);
gbc.gridx++;
gbc.gridy = 0;
userName = new JTextField(10);
password = new JPasswordField(10);
add(userName, gbc);
gbc.gridy++;
add(password, gbc);
}
public String getName() {
return userName.getText();
}
public char[] getPassword() {
return password.getPassword();
}
}
public class MainPane extends JPanel {
public MainPane() {
setBorder(new EmptyBorder(50, 50, 50, 50));
add(new JLabel("Super awesome app"));
}
}
}
I would also encourage DISPOSE_ON_CLOSE instead of HIDE_ON_CLOSE, it will release the native peer and remove the window from the applications window cache
Now, if you're really up for a more challenging methodology, you could have a look at Java and GUI - Where do ActionListeners belong according to MVC pattern?, which presents a MVC based login implementation
Related
Hi im am working on my Java project which I have to create a login page for users to enter, and when login is successful, an alert is supposed to tell them that the login was ok. However, I am having some troubles as my application is not showing up at all, I think it is something to do with the code that I have written. Below is the code:
public class UserLoginPage implements ActionListener {
//Put all JLabels,Frames and buttons here etc
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel userLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
JTextField userText = new JTextField();
JTextField passwordText = new JTextField();
JButton loginButton = new JButton("Login");
//Label for successful login
JLabel success = new JLabel("Login Successful");
//Default Constructor to add the frames and panels etc
public UserLoginPage(){
panel.setLayout(null);
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
userText.setBounds(100,20,165,25);
panel.add(userText);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
loginButton.setBounds(10,80,80,25);
loginButton.addActionListener(new UserLoginPage());
panel.add(loginButton);
success.setBounds(10,110,300,25);
panel.add(success);
//success.setText();
frame.setSize(500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserLoginPage();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
I think the problem lies with loginButton.addActionListener(new UserLoginPage()); But I might be wrong, do let me know of how to solve the problem, thank you.
There are multiple issues in your program:
panel.setLayout(null); and .setBounds(...). You shouldn't be using null-layouts, as Swing has to deal with multiple PLAFs, OS, screen sizes and resolutions. You might end up with issues like this one. Pixel-perfect layouts might seem like the easiest way to create complex UIs but it's not, it'll just lead you to endless issues. Instead use layout managers or combinations of them.
loginButton.addActionListener(new UserLoginPage()); You're creating a new instance of your program every time, and on every instance of it you're creating a new object because all your code is inside the constructor. Just, don't! It's a recursive call that finally creates a java.lang.StackOverflowError, to solve this use this instead of new UserLoginPage()
frame.setVisible(true); this line should always be the last line in your program, after you've added everything to your JFrame, not before.
With the above recommendations, here's an updated version of your code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UserLoginPage implements ActionListener {
// Put all JLabels,Frames and buttons here etc
private JPanel panel;
private JFrame frame;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userText;
private JTextField passwordText;
private JButton loginButton;
// Label for successful login
private JLabel success;
// Default Constructor to add the frames and panels etc
public UserLoginPage() {
}
private void createAndShowGUI() {
frame = new JFrame(getClass().getSimpleName());
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
userLabel = new JLabel("Username");
passwordLabel = new JLabel("Password");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("Login");
success = new JLabel("Login Successful");
loginButton.addActionListener(this);
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
frame.add(panel);
frame.add(success, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserLoginPage().createAndShowGUI();;
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
}
You were correct. loginButton.addActionListener(new UserLoginPage()) causes a java.lang.StackOverflowError. The constructor is always calling itself without a base case to default to. You should be passing that very instance of the UserLoginPage as a parameter, not a new instance.
Use this code instead:
loginButton.addActionListener(this);
I want to put some texts in text-Field when the form is load which instruct to user and when user click on that text-filed the texts remove automatically.
txtEmailId = new JTextField();
txtEmailId.setText("Email ID");
i have wrote above code but it display the text and keep as it is when user click on that text button i want to remove it.
is there any way to do this task?
I use to override the text fields paint method, until I ended up with more custom text fields then I really wanted...
Then I found this prompt API which is simple to use and doesn't require you to extend any components. It also has a nice "buddy" API
This has now been included in the SwingLabs, SwingX library which makes it even eaiser to use...
For example (this uses SwingX-1.6.4)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.PromptSupport;
public class PromptExample {
public static void main(String[] args) {
new PromptExample();
}
public PromptExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField bunnies = new JTextField(10);
JTextField ponnies = new JTextField(10);
JTextField unicorns = new JTextField(10);
JTextField fairies = new JTextField(10);
PromptSupport.setPrompt("Bunnies", bunnies);
PromptSupport.setPrompt("Ponnies", ponnies);
PromptSupport.setPrompt("Unicorns", unicorns);
PromptSupport.setPrompt("Fairies", fairies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, bunnies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIGHLIGHT_PROMPT, ponnies);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, unicorns);
PromptSupport.setFontStyle(Font.BOLD, bunnies);
PromptSupport.setFontStyle(Font.ITALIC, ponnies);
PromptSupport.setFontStyle(Font.ITALIC | Font.BOLD, unicorns);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(bunnies, gbc);
frame.add(ponnies, gbc);
frame.add(unicorns, gbc);
frame.add(fairies, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
JTextField busqueda = new JTextField(20);
add(busqueda);
busqueda.setHorizontalAlignment(SwingConstants.CENTER);
if (busqueda.getText().length() == 0) {
busqueda.setText("Buscar");
busqueda.setForeground(new Color(150, 150, 150));
}
busqueda.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
busqueda.setText("");
busqueda.setForeground(new Color(50, 50, 50));
}
#Override
public void focusLost(FocusEvent e) {
if (busqueda.getText().length() == 0) {
busqueda.setText("Buscar");
busqueda.setForeground(new Color(150, 150, 150));
}
}
});
You can download this NetBeans plugin which you can use to create a placeholder with just one line.
I fixed up the code and updated, but now I do not know where to place the constructor:
private JPanel panel;
public RollButton(JPanel panel){
this.panel = panel;
}
I looked at some examples of where to place the constructor and it seems like it is placed around my RollButton class, but I have tried to put it in multiple times and in multiple places but I cannot seem to get it right.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
public class DiceRollGUI {
public static JLabel label;
public static JFrame frame;
public static JPanel panel;
private static JButton button;
private static JButton buttonRollDie;
private static JLabel diceRoll;
public static void main (String[] args) {
JFrame frame = new JFrame("Dice Roll GUI");
JPanel contentPanel = new JPanel(new GridLayout(0,2,5,10));
button = new JButton("Roll");;
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(750, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setActionCommand("Roll");
button.addActionListener(new RollButton());
panel.setPreferredSize(new Dimension(750, 500));
frame.setContentPane(panel);
frame.pack();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(button);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
}
private static class RollButton implements ActionListener {
public void actionPerformed(ActionEvent event){
int roll = (int) (Math.round((Math.random() * 5) + 1));
ImageIcon dice = null;
if(roll == 1){
dice = new ImageIcon("DiceRollGUI Pictures/dice_face_1.png");
}
else if(roll == 2){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
}
else if(roll == 3){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
}
else if(roll == 4){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.png");
}
else if(roll == 5){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
}
else if(roll == 6){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");;
}
JLabel diceRoll = new JLabel("",dice, JLabel.CENTER);
panel.add(diceRoll);
panel.revalidate();
}
}
}
Edit: It's a NullPointerException or as some of us say, an NPE.
Key here is to learn how to fix most NPE's when you get them:
Find the line that throws the NPE. The exceptions stacktrace will tell you, for example yours is telling you at DiceRollGUI$RollButton.actionPerformed(DiceRollGUI.java:40) meaning at line 40 in your DiceRollGUI.java class.
Check to see if you're calling any methods on variables on that line or if you are "dereferencing" a variable in some other way.
Check to see if any of the variables on that line are null (a debugger can help, or failing that, a couple of println's before the line of interest can help).
Then trace back in your program to see why that variable is null when you thought that it wasn't.
In your case your JLabel, label, is null because, well because you never initialize it. You need to assign it a new JLabel object before trying to use it.
Also, as I mentioned in my comment, in the future, if you ask questions about exceptions or errors, always post the full exception or error message, and indicate for us which line is causing the problem. The error message will usually tell you exactly which line it is, but you will have to translate the line numbers to the actual line in your program.
This is a simple NullPointerException, which should have being fixed by checking the nearest line to the cause of the exception within your source code and/or debugging the program
When I ran the program, I got the following exception...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dicerollgui.DiceRollGUI$RollButton.actionPerformed(DiceRollGUI.java:55)
Which pointed me to label.setText(" "); within the actionPerformed method...
if(rolldie.equals("Roll")) {
label.setText(" ");
This automatically suggested to me that label has not yet being initialised (and therefore is null).
Updated...
This...
JFrame frame = new JFrame();
frame.add(dice6);
Seems very weird to me. Basically on each dice roll, you are creating a new JLabel and a new JFrame but you're not showing the resulting frame...
Instead, you should have a single JLabel which maintains the value of the last roll and simply use either setText or setIcon to change it's state based on your requirements.
Updated with example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DiceRoll {
public static void main(String[] args) {
new DiceRoll();
}
public DiceRoll() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DicePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DicePane extends JPanel {
private JLabel diceRoll;
private JButton rollDice;
public DicePane() {
setLayout(new BorderLayout());
diceRoll = new JLabel();
Font font = diceRoll.getFont();
diceRoll.setFont(font.deriveFont(128f));
diceRoll.setHorizontalAlignment(JLabel.CENTER);
add(diceRoll);
rollDice = new JButton("Papper needs a new pair of shoes");
rollDice.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int roll = (int) (Math.round((Math.random() * 5) + 1));
diceRoll.setText(Integer.toString(roll));
}
});
add(rollDice, BorderLayout.SOUTH);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I would like to develop a form with some text field.
example:
Name
SecondName
the idea is that every text field have inside a text like:
Insert your name
Insert your second name
when you click on the first text field to write your name, the text "Insert your name" have to be deleted... the same have to happen for the second text field (SecondName).
The effect have to be this:
I think that i just need an Action on the text field that have to wake up when the user press on the mouse on the text field, it's possible?
Thank you
Take a look at PromptSupport in SwingLabs SwingX Library
For Example
When the fields have focus, the "prompt" will be hidden, but you can control this, making it shown until the user types something or highlight when focus is gained.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.BuddySupport;
import org.jdesktop.swingx.prompt.PromptSupport;
public class PromptSupportTest {
public static void main(String[] args) {
new PromptSupportTest();
}
public PromptSupportTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JTextField firstName = new JTextField(10);
PromptSupport.setPrompt("First Name", firstName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, firstName);
JTextField lastName = new JTextField(10);
PromptSupport.setPrompt("Last Name", lastName);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, lastName);
JTextField picture = new JTextField(10);
PromptSupport.setPrompt("Picture", picture);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, picture);
JButton browse = new JButton("...");
browse.setMargin(new Insets(0, 0, 0, 0));
browse.setContentAreaFilled(false);
browse.setFocusPainted(false);
browse.setFocusable(false);
browse.setOpaque(false);
// Add action listener to brose button to show JFileChooser...
BuddySupport.addRight(browse, picture);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
add(firstName, gbc);
add(lastName, gbc);
add(picture, gbc);
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Ok"), gbc);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I've also added an example of BuddySupport which is part of the same API, which allows you to "buddy" another component with a text component. Here I've done the classic "file browser" combination, but I do "search" style fields like this all the time...
Take a look at Text Prompt for a simple solution that allows you to control when the text is displayed/hidden as well as the font/color of the text.
It will work with regular text components. In its simplest form you only need one extra line of code:
JTextField firstName = new JTextField(10);
TextPrompt tp = new TextPrompt("First Name", firstName);
see this example
import java.awt.Color;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JTextField;
public class HintTextField extends JTextField {
Font gainFont = new Font("Tahoma", Font.PLAIN, 11);
Font lostFont = new Font("Tahoma", Font.ITALIC, 11);
public HintTextField(final String hint) {
setText(hint);
setFont(lostFont);
setForeground(Color.GRAY);
this.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
if (getText().equals(hint)) {
setText("");
setFont(gainFont);
} else {
setText(getText());
setFont(gainFont);
}
}
#Override
public void focusLost(FocusEvent e) {
if (getText().equals(hint)|| getText().length()==0) {
setText(hint);
setFont(lostFont);
setForeground(Color.GRAY);
} else {
setText(getText());
setFont(gainFont);
setForeground(Color.BLACK);
}
}
});
}
}
I currently have two frames, when you run the application the first JFrame that shows is a login, it has two input fields and a button. When the user logs in and is verified, I would like to close the frame and start up the second one.
So, the only thing I can think of doing is doing setVisible(false) for the login frame and setVisible(true) for the Main frame.
Is there a better way to do this, or is that the only way?
Personnally, I would start up your second JFrame immediately and replace your first frame with a modal JDialog which would be owned by the JFrame.
See also this answer to The Use of Multiple JFrames, Good/Bad Practice?
Here is a basic demo of what I suggest:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLogin {
private JFrame frame;
private boolean authenticated;
private JTextField login;
private JPasswordField password;
protected void initUI() {
frame = new JFrame(TestLogin.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
}
protected void showLoginDialog() {
authenticated = false;
final JDialog dialog = new JDialog(frame, "Please provide your credentials");
dialog.setModal(true);
JPanel panel = new JPanel(new GridBagLayout());
JPanel buttonPanel = new JPanel();
login = new JTextField(40);
password = new JPasswordField(20);
JLabel loginLabel = new JLabel("Login:");
JLabel passwordLabel = new JLabel("Password:");
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Here perform authentication and set authentication flag to appropriate value
authenticated = true;
if (authenticated) {
setUpFrame();
dialog.dispose();
}
}
});
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
if (!authenticated) {
System.exit(0);
}
}
});
dialog.getRootPane().setDefaultButton(ok);
buttonPanel.add(ok);
buttonPanel.add(cancel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(loginLabel, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
panel.add(login, gbc);
gbc.gridwidth = 1;
gbc.weightx = 0;
panel.add(passwordLabel, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
panel.add(password, gbc);
panel.add(buttonPanel, gbc);
dialog.add(panel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
while (!authenticated) {
dialog.setVisible(true);
}
}
protected void setUpFrame() {
frame.add(new JLabel("Successfully authenticated"));
frame.revalidate();
frame.repaint();
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TestLogin testLogin = new TestLogin();
testLogin.initUI();
testLogin.showLoginDialog();
}
});
}
}
There are several ways to do that.
E.g. you could reuse your 1st JFrame. Therefore remove the components you got on your 1st frame, add the ones for the 2nd and then repaint() the frame.
But I wouldn't consider that as good practice.
As Andrew Thompson suggested, you could also use a CardLayout to just initialize one JFrame, show your login-card and then switch to the fully initialized 2nd full-application card. This way you will get rid of those repaints.
You could also show your 2nd frame (your application first) and then use a modal JDialog to the let user log in.