Blocking frame switch with Java - 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

Related

JFrame that waits for User Input [duplicate]

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.

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.

Link between two JFrames

Link between two JFrame using JMenu
I have an application where I have to move to another frame by clicking a menu. for example, on a file menu, I click on add which will bring out a new frame where operations can be carried out. what code(s) can i use in Netbeans?
I used it to open another JFrame but when I exit the new JFrame both frames are closed ....
there is something wrong in ma code plz help me in that prob....
You have to set the defaultCloseOperation to something else then EXIT_ON_CLOSE. Please refer to the documentation for JFrame for more information. I suspect that the second JFrame should only have HIDE_ON_CLOSE though.
do you need to use jframes? why not jdialogs? (the dialog window (which can pop up) does not have to be modal.
if you have two different frames, that can make parentage (which window belongs to which frame) something you need to keep track of more assiduously.
please tell us in more detail what you are trying to accomplish; that will help in picking the correct widgets to be used. if things are hard to do/code in swing, then there's probably an easier way with other widgets/components, imho.
good luck!

how do i close a frame yet open a new frame?

how do i close a frame yet open a new frame?
i have a frame, (help)
when i click on my menu item
i want to open (mainForm)
exit from help.
new mainForm().setVisible(true);
System.exit(0);
i know this closes the whole program however how do i get it to only close the current frame
thanks
If you no longer want to use the frame you could use frame.dispose()
If you just want to hide it use frame.setVisible(false).
If you extended a Frame and are trying to close it from within use this.dispose or this.setVisible(false).
You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really bad for usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!
Let's say you created your frame as so:
JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame
mainframe.dispose();
I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.
In short, the semantics of setVisible has nothing to do with starting / stopping the application.
If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.
try setting the default close operation for the JFrame like so.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then implement a WindowListener that performs the actions you want when a window closing event is fired.

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