Closing a JFrame from a JPanel - java

I have a JFrame with a login(JPanel) which can be selected from a menubar. The menubar also shows options depending on users roles. When a user first logs in an instance of the JFrame is created, my problem comes when i select login on the menubar and instanciate en new JFrame since i obviously have 2 JFrames open and i can't find a way to hide or close the previous JFrame from the JPanel. Is there any way to hide/dispose all open windows right before i instanciate a new JFrame or any other possible solution? Thanks in advance for your time!

Start by taking a look at The Use of Multiple JFrames, Good/Bad Practice?.
Instead of using a second JFrame, using a JDialog of some kind to show the login window. A modal dialog will block the caller when the dialog is made visible, allowing the code to continue once it's closed. This is very helpful for gain information from the user...
For example, use this to gather the credentials (and possibly authenticate) the user and allow the caller to extract the results when the dialog is closed.
Have a look at How to Make Dialogs for more details

Related

Adding JPanels to a JFrame

I'm trying to make a simple program with Java, Swing, that shows you a Window and by clicking the button "Start" it should show you a new panel with a login interface (enter username, password, login).
I've read a question asked here that says that it's better to use only one JFrame and it makes sense because I don't want to start a new window between clicking "start" button and going to the login interface. But, how should I do this? I've created two JPanel containers named loginPanel and startPanel, but I don't know how to set visible loginPanel when the application runs, and then by clicking a button (in startPanel) set visible loginPanel (and obviously stop showing startPanel).
I'm using the graphical Swing interface that comes with NetBeans (because the IDE have greyed the zone to edit the code by yourself, IDK why).
Could anyone give me a example of what I'd like to do?

GUI - Switching between screens

I got my program, that can connect to a Database working! ( Hooray )
But now, I ran into a new problem. I read that using multiple JFrame windows (and closing the old one) is not user friendly, and a bad learning habit.
So now I am wondering, is there a way to switch between Panels, or something similar?
Example:
JFrame with Login & Password. -- Users logs in, goes to the next 'screen' where he or she can see the Database info, cause he or she logged in!
What should I use, any good methods out there?
you may want to check CardLayout
The simple answer is yes you can. The idea is to have a localized class that contains a method that outside classes can call passing a jPanel then simply add that panel to your jFrames content pane (which in turn will remove the other panel). There are many ways to go about this and I hope you find one that works and I hope this answer helps you as well.
Here is the procedure I generally follow. I create and open a new Frame and make the parent frame invisible. Again when child frame is closed I make the parent frame visible. I am using this procedure for a long time and not facing any problem.
This is the piece of the code executed when login button is clicked.
...
setVisible(false); //Hide the login page
DBPage page=new DBPage(this, value1); // DBPage is another JFrame
page.setVisible(true);
I feel this much of code is enough to understand.
JLayeredPane might work.
http://docs.oracle.com/javase/8/docs/api/javax/swing/JLayeredPane.html
You could have several layers on top of each other, the login screen, etc. and show the layer that is most relevant at the time.
A previous question may prove useful:
Java Swing - how to show a panel on top of another panel?

Best procedure for making a dialog box with a large number of buttons?

It seems that making a new JFrame to act as a dialog box isn't the best way to do this as it seems tricky to grab the specific value from the class which creates it.
I basically want to make a dialog box with a large number of buttons, which once a user clicks on one, the main class will grab that value. If I make a frame to do this, getting that value is tricky (but it's much easier to design in Windowbuilder Pro) but it also means the user can still interact with the main frame while this frame is presented.
What's the best way to create such a dialog box?
Use a JDialog or JOptionPane instead. If you want the user to be able to interact with the frame while it is open, it should be non-modal, but then you have the problem of knowing when it is closed in order to get the values.
See also:
The Use of Multiple JFrames, Good/Bad Practice?
How to Make Dialogs
How to Use Modality in Dialogs

Make JFrame display other JFrame without opening new window

I'm not sure if this has been answered or the correct way of phrasing it, but have had no luck in my search. I have 4 JFrame guis all in their own classes: a main gui and 3 others. I want to know if it is possible to display the other guis inside the same window without opening a new window and setting the first window to a false visibility? I'm able to call the other JFrames and make them display through a series of actionlisteners, but they open another window, making me have to setVisible(false) the gui window. I want to be able to have all the guis display in the same window without opening/closing windows. Thanks
You should not be creating separate frames. Just create separate panels and swap the panels.
See the Swing tutorial on How to Use Card Layout for more information.
Also, if you ever do need more than one window, you should be using a JDialog for the child windows. An application should only have a single JFrame.

Moving from one JFrame to another

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..
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
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,
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);
Try this...
The approach you used to hide and un-hide is fine, but will be better if dispose is used.
Try applying the Singleton pattern on the classes which govern these JFrames.
And yes they both will be on the same Process.

Categories

Resources