Moving from one JFrame to another - java

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.

Related

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?

Closing a JFrame from a JPanel

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

How are GUIs usually manipulated?

I am attempting to learn Java (at this point GUI programming in Swing and as a concept in general) and I have managed to create a basic login page. What I want to do however is to have it change what is displayed somehow to a 'home' page, as it were. I can think of only two ways of doing this, the first being opening a new window and closing the old one and the second being somehow changing the frame the login page is in to whatever I want to display. How is this usually done in real-world applications?
There are a number of ways that it can be done including the two that you mentioned. It really depends on what you are trying to achieve. Something like a login form could be done with a JDialog that pops up over the main window such that you start the main window and the main window simply pops open a JDialog for credentials etc.
Sometimes you create multi document interfaces using JInternalFrame. Where the various windows that you need all appear to be within a bigger main window.
So if you were creating a simple UI then simply switching out the content of the JFrame as you suggested would suffice. You can use an apprpriate LayoutManager to assist such as a CardLayout.
If you have a login frame, a better way is to make it a modal dialog, which is:
1) more user-friendly.
2) making your coding job easier.

How to wait for a JFrame to close before continuing?

My program consists of 3 main 'sections'. Main function, Login form and App form. The main function should do something like: Open Login form, wait for it to close, then open App form. I can't get the waiting part to work, or rather, I don't know how I would go around doing that.
I was told by someone to use a JDialog instead and use setModal(true), but with that approach the Login form wouldn't appear on the taskbar, which is terrible in my opinion.
Another thing I considered was to open the App from inside the Login after it closes, but that feels like bad design since that'd make the Login form non-reusable.
So, please, what would you suggest?
Why must the login appear on the task bar, since the main app will be there, and you don't want more than one task bar item for an individual program. Your best option may be to use a modal JDialog.
Another option is to use CardLayout to swap "views".
A third option is to use a JFrame if you must but attach a listener to it, a WindowListener I believe, to respond to its close event.
Regardless of which route you go, your login gui should be a JPanel so that you can place it anywhere you wish and then change your mind later.

How to use setVisible in JFrames?

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.

Categories

Resources