JFrame that waits for User Input [duplicate] - java

In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.
How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?

Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed
see http://mindprod.com/jgloss/modal.html for explanation
and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example
If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..

I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:
How do I make one JFrame wait for another JFrame?
I would say the easiest way to acheive this is by registering and firing event listeners.
In your "child" frame, register the "main" frame as an event listener.
In your "main" frame,
implement your choice of listener, e.g. ActionListener
in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.
One can easily implement this to a ny number of situations, including the login scenario described in the question.

Use JModalFrame instead of JFrame.

Related

Figure out when a button is pressed in another window

I run my main window in the main method like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NoteSystem MainWindow = new NoteSystem();
MainWindow.initUI();
}
});
And then when a button is pressed on this form, I create a window by instantiating a class I made. I'm having trouble detecting when the second form is closed and what the textboxes/other controls contained.
What is the proper way to:
a) Fire an event in NoteSystem when the second window is closed
b) allow NoteSystem to check all the components/controls in the second window
I considered using a JOptionPane, but I'd like to create the window entirely in my own class. The idea of having the main window frozen while waiting for response from the second window works for my application though, so if I could use JOptionPane with my own class, that would be ideal.
Thanks
The best way is to use a modal dialog, a window like a JFrame, but that halts program flow in the calling code until it is no longer visible. This way, the calling code will know exactly when the dialog window has been dealt with, since its code flow will resume once again, and so often the calling code will extract information from the dialog window code at that point. A JOptionPane is one type of these, and so is a modeal JDialog (of which a JOptionPane is a sub-type). Either of these can display as complex a GUI as any that is displayed within a JFrame, so don't sell them short. You'll notice that the second parameter of most JOptionPane methods is of type Object, meaning anything can go in there, but most often you'll pass in either a String for a simple JOptionPane, or a JPanel that can be chock full of components and other nested JPanels, and in this way the JOptionPane can display the complex GUI if need be.
For examples, please see:
Passing values between JFrames
action listener to JDialog for clicked button
trouble obtaining variable info from another JFrame
How do you return a value from a java swing window closes from a button?

Blocking frame switch with Java

I would like to block the frame switch request of the user in my java Application; Example:
I have the main frame with full size and the setup frame with a smaller size (400,400 for example). While the setup frame is opened I wouldnt like to let the user to acess the Main Frame, he can do this only if he closes the setup frame.
That might one duplicated question and I'm sorry for that, but I couldnt find the specific term to research what I want, I was try something like "Window focus on java" but I think I was researching in the wrong direction..
Thanks in advance for the help
The best solution is to use a modal dialog. If this is not an option, you will have to create a handler yourself that fires whenever a frame gets focus and checks if it is the right frame. If not, the handler must focus on the important frame.
set child jframe or whatever swing component as modal window

Exiting out of one of two JFrame exits out of both

I have two seperate JFrames but when i click the X in the topright of one, it will exit out of the other also. I have an "exit" button near the bottom to do setVisible(false), but i still have the tendency to use the x button. How would i make it so that it doesnt cancel out of the entire project?
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
Don't give your GUI two JFrames. The GUI ideally should have only one GUI. If a separate window is required, then make it a dialog such as a JDialog, and this won't happen.
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
You are perfectly describing the behavior of a modal JDialog or JOptionPane. Just use 'em.
Later we'll chat about using CardLayouts to swap views in a single GUI.
Edit, you state:
Im using Netbeans form editor to create them faster but I only see JFrame and JPanel. Can I edit them in Netbeans? I'd rather not do them through scratch Java
You've touched on another zealous belief of mine, that this is yet another reason not to use a code generator when learning a library as one can get too tied into the code generator, that it prevents one from learning the library. I strongly advise you to put aside your code-generation tool and create by hand, referring to the tutorials and API. Then later when you get more familiar with the library, sure use the tool. By the way, an answer to your direct question here is to gear your GUI's to create JPanels, and then use these JPanels where and how you want them -- in JFrames, or JDialogs, or JOptionPanes, or swapped in CardLayouts, or JTabbedPanes or nested in other JPanels,... etc...
You should be using a modal JDialog, not a second JFrame, because JDialogs provide certain functionality such as not adding another window bar to the taskbar, and automatically setting focus when the parent JFrame receives focus. Modal JDialogs prevent user input to the JFrame while it's open, useful for an "Are you sure you want to exit?" dialog, for example.
As for one JFrame exiting the other, you probably have their default close operation set to EXIT_ON_CLOSE. If you do this:
jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
handleUserWantsToCloseWindow();
}
});
Then you can control what happens when the user wants to close, such as popping up a "Save Changes?" modal JDialog or "Are you sure you want to quit?" modal JDialog. Note that you have to manually dispose of the JFrame if you use this method.

How to restrict the focus of JFrame in Swing?

I have a parent JFrame it contain a JButton. functionality of that button is to open another window.
I want to restrict the focus of window, means after closing the second window's focus should come into first window(parent). And focus should not come to first window if second window is open.
You should make your second window modal. That said, you'd probably want to make it a JDialog.
yes its possible but workaround for two or more JFrames, but for full funcionalities is needed lots of code,
standard would be one JFrame and another TopLayoutContainers could be JDialog then you can easily play with parent and modalities, toFront , setAlwaysOnTop
As shinoku stated, you can use a modal. However if you have to use a JFrame, you have to implement a WindowListener for the new JFrame. In that implementation for the windowClosing() method you can say originalJFrame.requestFocus(). Of course, your constructor of the WindowListener must be supplied with a reference to the original frame as well.

Java swing. How to wait for other Jframes

In a JFrame, when I click on 'login', I pop up another Jframe which is the login window.
How do I make my main Jframe wait for my login Jframe to exit, before doing anything else?
Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed
see http://mindprod.com/jgloss/modal.html for explanation
and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example
If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit..
I agree that a modal dialog would be the best option here, but if I were to answer this question in it's more general form, say:
How do I make one JFrame wait for another JFrame?
I would say the easiest way to acheive this is by registering and firing event listeners.
In your "child" frame, register the "main" frame as an event listener.
In your "main" frame,
implement your choice of listener, e.g. ActionListener
in the method called by the listener, e.g. actionPerformed, code the logic that handles what happens upon each of the actions it can respond to in the "child" frame.
One can easily implement this to a ny number of situations, including the login scenario described in the question.
Use JModalFrame instead of JFrame.

Categories