Moving from one JFrame to another

13,893

Solution 1

Is this the correct way?

Yes, this should be fine.

isn't it wrong if these 2 separated classes are hidden instead of being closed?

The ideal is dispose of your unused forms (such as the login form when not needed any more)

are these 1 process or 2 different processes?

These will run on the same process

Solution 2

In a package in Netbeans I created 2 JFrame Forms, first one is Login, second is, mainProgram, after the successful log in, I use the following way to "close" the Login frame and open the main program frame.

use CardLayout, after correct login you can to switch the GUI to next Card and/or with change for JFrame Dimmnsion on the screen too,

Solution 3

Try this...

  1. The approach you used to hide and un-hide is fine, but will be better if dispose is used.

  2. Try applying the Singleton pattern on the classes which govern these JFrames.

  3. And yes they both will be on the same Process.

Solution 4

in my opinion the more correct way is to use another class, like Launcher, which will have the entry point (main method). Make the login window as a modal JDialog, and set DISPOSE_ON_CLOSE as a value of default close operation. The class of dialog should contain a method to inform a user really logged in. After the login dialog is closed, show the main frame

loginDialog.setVisible(true);
if (loginDialog.isLoggedIn())
    mainFrame.setVisible(true);
Share:
13,893
Ali Bassam
Author by

Ali Bassam

Peaceful Lebanese Citizen, Loves Computers and Technology, Programming and the Web. You can follow me here alibassam.com here twitter.com/alibassam_ and here.. on StackOverflow!

Updated on June 29, 2022

Comments

  • Ali Bassam
    Ali Bassam almost 2 years

    In a package in Netbeans I created two JFrame Forms, first one is Login, second is, mainProgram, after the successful log in, I use the following way to "close" the Login frame and open the main program frame.

    mainProgram m=new mainProgram();
    m.setVisible(true);
    setVisible(false); //to hide the log in frame
    

    Is this the correct way? Isn't it wrong if these two separated classes are hidden instead of being closed? are these one process or two different processes? if there's a better way then what is it?

    thanks..

  • Ali Bassam
    Ali Bassam almost 12 years
    I used to use CardLayout, but CardLayout sets the JFrame size to the size of the biggest Card, means if the mainProgram frame is bigger than the Login frame (it has to be), the Login frame will look big and weird, then I decided to change the size when each frame loads, and it worked, but the change of size and card at the same time made it slow, switching using setVisible(true) was faster
  • mKorbel
    mKorbel almost 12 years
    setSize before setVisible, after correct logon to call JFrame.pack() with setLocationRelativeTo(null) (for example with centering to the monitor)