I am making an application where I need to have a Preferences JFrame which can open another JFrame for further preferences of a certain type. Though I do not want the further preferences JFrame to be able to have more than one instance open at the same time.
I have tried using the .isActive() method but that always no matter what happens reports that the variable used to make the new instance of the JFrame is equal to null, which it has been initially set to when it was initialised.
So what is the best way of actually checking if a certain JFrame already has an instance in existence? Is it to get it to write to a variable when it launches and when it quits? If so, how would you get it to write to a variable when someone pressed the x button, and what if it crashed and closed not through the standard means?
Or is there another way of doing it like getting the isActive() method to work or another method that would do better?
What you should do is to make the JFrame a modal JDialog. Instead of opening a JFrame, you open a JDialog. You pass the other settings window as the parent and pass true for modal. This will prevent any action in the other settings window as long as this modal dialog is open.
Thats what a lot/almost every program uses. Have you ever noticed that, when you open a settings window and try to click on the main window, the settings window pops back on top and blinks for a second? Thats what the modal dialog does.
Why don't you just avoid creating a new JFrame at all? Just make one earlier in your execution and make it visible when you need it and hide it when you temporarily don't need it anymore. Trying to make an already visible JFrame visible or hide a hidden JFrame shouldn't do anything.
Related
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?
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'm trying to create a multi-windowed interface, ala GIMP. One of them allows the user to load an image, to be displayed in the frame. So, when the program loads, all windows (two for now but I plan to have three) are shown in the application bar. However, when the second* window invokes JFileChooser, it disappears in the application bar (but does not close). But if I <Super>+<Tab> or <Alt>+<Tab> it still appears there. It also reappears in the app bar when I click the "Show/Hide All Windows" button.
All JFrames are invoked from a single Runnable. Anyone else encountered this issue? How do I work around this one (i.e., make all windows visible in the application bar at all times)?
Thanks!
*I called it the second window since it is the second one that is setVisibled to true.
A JFrame will appear on the task bar. A JWindow or JDialog do not appear on the task bar.
A JFileChooser uses a JDialog to display the date so it will not appear on the task bar. You need to specify a JFrame as the owner of the file chooser. The file choose will still not appear on the task bar, but when you click on the icon representing the owner frame it will become visible along with the frame.
I have a modeless dialog box being generated which prompts users to open a new window. The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.
When I make the call directly via the file menu the dialog box comes up with focus exactly as I want. But when I have the framework indirectly open the dialog box it does not have focus as it should.
There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened. In both cases the frame which generates the dialog box is realized at the time the box is generated. I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.
Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?
in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus
When you create the dialog, try setting the main GUI as parent of the dialog.
In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.
So make sure when you create the dialog, you are setting the main GUI/ window as parent always.
It should help most times.
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.