Focussing in Java swing - java

I'm developing an application using java swing. When I click a button, I want another window to be opened. This works fine. But it is possible to alt+tab to the original window and then interact with it even after the new window is open. Is there any way not to let the user focus the original window after the new window appears? By window I'm referring to Jframe/Jdialog.

Assuming the instance of your main JFrame window is called mainWindow:, the following code will prevent switching the focus.
// the second parameter makes the dialog modal and will prevent
// switching the focus to the mainWindow
JDialog dialog = new JDialog(mainWindow, true);
...
dialog.setVisible(true);
Documentation on JDialog: http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html

You may try to use a JDialog instead of JFrame and pass the instance of JFrame to JDialog constructor
You may also try to check
frame.setAlwaysOnTop(true);
or may be like this:-
frame.toFront();
frame.setState(Frame.NORMAL);

Related

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 use JDialog in a Swing GUI?

I want to extend my JFrame Form ... with one JDialog form (I want to connect with frame, I want in the menu-bar when someone click to the HELP (from the menu) will show the new dialog box. I do not want to use JOptionPane for this use.
I made in to the program menu with Help menu item, now on the actionPerformed will something write.
new help(this, true, ).setVisible(true);
Something this but I don't remember - what is correct?
this from new help(this, true, ).setVisible(true); could be missinterpreted by another this in rest of your code
create local variable for JFrame and JDialog, use title and Modal/ModalityType if is required, e.g. new JDialog(myFrame, ....)
create only one instace of JDialog, change DefaultCloseOperations to HIDE_ON_CLOSE
(then only) call myDialog.setVisisble(true) delayed inside invokeLater() (from JMenuItems event)
Why not using javax.swing.JOptionPane? It provides you all you need to show dialogs and prompt the user for inputs.
JOptionPane p = new JOptionPane();
// init p
p.setVisible(true);

How to properly hide a JFrame

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.

Java: Dispose ActionEvent from different JFrame

I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.

Java: If a custom JDialog is hidden, is focus returned back to its parent?

I am creating a custom JDialog. I need to hide the JDialog (without removing it from memory) so that its parent can call a method on the JDialog (getResults()).
JDialog dialog = new JDialog(.....);
///Code WITHIN JDialog:
{
//JDialog opens and its actions are performed
this.setVisible(false); //Does this allow the parent to gain focus once more?
}
It depends: whether JDialog modaless is or not. And also if you extend JDialog then:
Yes.
If it will disable focusing other windows, it will release this constraint when the JDialog is hidden. If the JDialog is visible again, it will be impossible to focus the other windows again.

Categories