tags:

views:

239

answers:

2
+2  Q: 

multiple JFrames

im creating a java application with netbeans. i have two jframes for login and the main application. what i want to do is to load the login jframe at runtime, then load the main application jframe when user authentication is correct. the instance of the login jframe must be destroyed after the main application jframe has already loaded. also, i want the user information from the login jframe to be passed to the main application jframe. how do i acheive this?

+1  A: 

Extend JFrame to create the Main Frame. Add a constructor in this to accept the user information.

From login screen, when authentication succeeds, create an instance of the Main frame by passing the login information. Invoke dispose() on the login frame and invoke setVisible(true) on the main frame.

MainFrame mainFrame = new MainFrame(userInput.getText());
this.dispose();
mainFrame.setVisible(true);
Chandru
wouldn't this.dispose() also destroy mainFrame?
mixm
That piece of code must reside inside your login Frame.
Chandru
yes but the instance of mainframe is found in the login frame
mixm
That doesn't matter.
Chandru
+2  A: 

I suggest the following simple approach, whereby you create classes to represent your Login panel and main application frame. In the example I've created a login panel rather than a frame to allow it to be embedded in a modal dialog.

// Login panel which allows user to enter credentials and provides
// accessor methods for returning them.
public class LoginPanel extends JPanel {
  public String getUserName() { ... }

  public String getPassword() { ... }
}

// Main applicaiton frame, initialised with login credentials.
public class MainFrame extends JFrame {
  /**
   * Constructor that takes login credentials as arguments.
   */
  public MainFrame(String userName, String password) { ...}
}

// "Bootstrap" code typically added to your main() method.
SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    LoginPanel loginPnl = new LoginPanel();

    // Show modal login dialog.  The code following this will
    // only be executed when the dialog is dismissed.
    JOptionPane.showMessageDialog(null, loginPnl);

    // Construct and show MainFrame using login credentials.
    MainFrame frm = new MainFrame(loginPnl.getUserName(), loginPnl.getPassword());
    frm.setVisible(true);
  }
});
Adamski