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.
Related
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.
I have a very simple JFrame window that contains one button: No.
In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.
I have two questions:
Do I really need to System.exit(0); in the above case?
If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
use CardLayout
if is there real reason for another popup container
use JDialog with parent to JFrame, with setModal / ModalityTypes
create only one JDialog and to reuse this one JDialog by getContentPane#removeAll()
use JOptionPane for simple users interaction
put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()
setVisible will cause slowdown
dispose will cause slowdown
System.exit will close entire JVM
Therefore, you should reuse a single JFrame or JDialog.
In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.
Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).
Note: The default option for JFrame is HIDE_ON_CLOSE.
You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.
I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it's not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this: getWindowAncestor(Component c). Then you could call dispose() on the window returned.
i.e.,
Window win = SwingUtilities.getWindowAncestor(myJPanel);
win.dispose();
Note that this is fine if you're using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.
The button is already on the frame (on the upper right, here). Just call JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) when constructing it and it will work as the user expects.
Other tip
Don't extend frame or panel, but instead just create and use them.
What is the difference between a JFrame and a JDialog?
Why can't we use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); for a JDialog?
JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).
But both inherit from Window, so they share much functionality.
Why we can't use setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); for JDialog?
Sure you can.
Post your SSCCE that demonstrates the problem you are having when using this value.
However you can't use EXIT_ON_CLOSE for a JDialog because that value is not supported which makes sense since a JDialog is a "child" or "helper" window for your application which is represented by a JFrame. Closing a dialog should not close the application.
There are some JDialog constructors with a owner parameter which can be a Frame, a Dialog or a Window. A non-null value also makes the JDialog stay above his owner. This is complementary of the modal behavior described by Fortran.
You can also use setModal(boolean t);
This only works on JDialog. User must operate on JDialog not other window. If they wanna operate owner windows, they must shut down this JDialog.
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.