Cascading JFrames to open multiple Windows - java

How many JFrames can be visible at a time simultaneously....?
I wrote a GUI program that opens another window when a button "Sign in" is clicked.That new window has its own button which opens another window.After the 3rd window no new window is opening.My program had maximum five windows.

You can have as much as JFrame you want .
There isn't any limitations as I know.
But if you are facing any tgere should be a solution.
You should just close current JFrame before opening other one.

Related

java: add an option "re-size window" in the contextual menu which appears when we right click on the restored window in the tool bar

First I apologize if my English is not very good. it's my 4th language :P:P.
I have a java program (which I run using NetBeans) that open a graphic window (application), when I restore this window then I do a right click on its button in the toolbar it shows a menu that contains a button to close the window (like it does for every window XD), I want to add another button or text to re-size the window to [30,30],(example: we add text "send window to [30,30]" and when we click on it it resize the window to 30,30) cause when I do a remote connection from home I can't see the window its big and it appears in the 2nd screen. I don't know how to write this in Java. I'll be grateful for any help. If I didn't explain the problem well please don't hesitate to ask me and i'll explain it other way. :)
Thank you and have a lovely day everyone :):)

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.

Minimize and Maximize JFrame on custom events in Java Swing

If I have two JFrames where one is the main JFrame, and the other one pops up when I click something. Now what I want is that when the new frame pops up, the main one should be minimized. Also when I click on this popup frame to close it, the main should be restored back.
Essentially I want to know, how can we maximize and minimize a JFrame apart from the default click operation. Is there any function for doing this on custom click for example?
1) setDefaultCloseOperation to NOTHING_ON_CLOSE
2) addWindowListener to JFrame
3) overrive windowsClosing() method with proper Action for iconify ...
4) don't forget to set to the JMenuItem/JButton System.exit(1), because in this form current JVM instence never gone from PC RAM or swap area until PC restarted or switch off
5) better would be to change 2nd. JFrame to the JDialog because in most cases is too hard manage lots of methods betweens two JFrames
setPatent
setModal
setModalityTypes

Do JMenuItem Accelerators function properly when in a frame opened by another frame?

I have one frame that opens up another frame (editor). Most of the menu accelerators don't work when opened this way, but when I run the editor frame standalone, they do work. Should it work?
It turns out that when you open one frame from another in java you have to start another thread or the keybindings don't work right - they all get caught by the first frame. This makes sense.

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.

Categories