Open New Window And Close Parent - java

When my program launches, a window (aka "StartWindow") opens that has 3 options: New Game/Load Game/Exit. When New Game or Load Game is clicked (and after some input), the game window (aka "GameWindow") will open so the user can play.
When the GameWindow opens, I would like to have the StartWindow close. And when the GameWindow closes, the StartWindow will open until the actual "EXIT" button is clicked.
Is there a way to do this? Right now, the only way I can achieve something similar to this is having a boolean called "gameRunning". When this is true, the buttons on StartWindow will have no action when clicked. Opposite when false.
For example purposes, suppose that each window has 3 buttons, a text field, and nothing else.

Use setVisible(false) method on parent before opening any child window. When child window closes call setVisible(true). It will solve your problem

Related

How would I be able to close this class and open a new class with a JButton and an ActionListener

I am trying to make a game menu where if you click a button it goes onto another window (which I made in another class). With my current code, the window does not close, rather it goes invisible.
I have tried using System.exit(0); and dispose();. However, System.exit(0) does not open the other window and dispose(); does not completely exit the window.
In the actionPerformed method right now I use
Class.main(new String[0]); to open the new class and I use setVisible(false); to close the current class. But this does not accomplish what I want.
The output I expect is when you click one of the JButton's it completely closes the PongGUI class and opens the corresponding class.

Closing one JFrame while other is up

I have two JFrames opened simultaneously (by clicking certain button in the main Frame I open second Frame). I can of course exit whole program by clicking X on the main frame. The thing is: is there a way to close my subFrame by clicking X while the Main Frame remain opened? In the subFrame there is going certain action and I want to shut it down by click X but not closing the Main Frame at a time.
Write your close function like this
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

Netbean GUI interface window closing

I'm working in the netbeans GUI interface, and I want to know how to close the window. I found the following code:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
However, I cannot find the name of the frame in the code.
How do I find the frame name, or am I going about this the wrong way? How do I close it?
Put this line in your form no need to use window event
setDefaultCloseOperation(EXIT_ON_CLOSE);
How do I close it?
When you create the frame you need code like:
Jframe frame = new JFrame(...);
frame.setDefaultCloseOperation(JFrame.EXIT__ON_CLOSE);
Now when you click on the "close" button the application will exit.
However you may also want to close the frame by using a menu item. In this case what you want to do is create an Action that you can add to your "Exit" JMenuItem.
Check out the Exit Action found in Closing an Application. The Exit Action shows how you can access the current frame in order to dispatch the windowClosing() event to the frame. So the "Exit" menu item will then function just like the user clicking on the "close" button.
You are using NetBeans IDE, go to JFrame Properties, the very first option is DefaultCloseOperation, use the drop down menu to toggle between options available or you can add custom codes.
Found an answer at https://netbeans.org/kb/articles/gui-functionality.html
Turns out all I needed was
System.exit(0);

Open java Applet or Frame inside a parent Applet method and wait for input

I have a Java applet (lets call it parentApplet) with a public method which must return information regarding the status of the performed actions (let's call it getUserInput()) . This method opens another Applet which needs user button input, by adding it as a child with add(childApplet), and afterwards adding itself (the parent) as an ActionListner of the buttons in the childApplet, being able to run other methods when the user clicks on the buttons in the childApplet.
My question is, how can I halt getUserInput() execution until the user has clicked the childApplet buttons?
I tried to have a static variable that tracks the return information, and spinning on a while(var == null) Thread.Sleep(1000); but it blocks the main thread, as it should.
PS: Having the childApplet as an applet can be changed to anything that could better fulfil the requirement of opening another panel on top of the parent applet.
Details on getUserInput()
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and OK/Clear/Cancel buttons. When the user presses OK, I need to get the BufferedImage drawn. Do you know if this can be accomplished by extending a JDialog?
You really need to restructure your app. You can't do it the way you want.
Try creating a JDialog set it up as you like with input fields and an OK/Cancel button.
Then to show the dialog do:
MyDialog dialog = new MyDialog(null, true); //true = modal
//dialog.setModalityType(ModalityType.DOCUMENT_MODAL); //or specify modal here
dialog.setVisible(true); //waits until dialog is closed
if (dialog.wasAccepted()) {
//grab values from dialog
dialog.getCanvas();
}
In the dialog you would have:
private boolean accepted = false;
public boolean wasAccepted() {return accepted;}
public Canvas getCanvas() {return canvas;}
public ? getWhateverElseYouWant() {return ...;}
The OK button would:
accepted=true;
dispose();
The Cancel button would:
accepted=false;
dispose();
The JDialog will pump events while it's visible. So the setVisible() function will halt execution until the dialog is closed.
That should work better, and then you can return many user input fields.
You can even change the JDialog constructor to pass default value(s) in.
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and ok/clear/cancel buttons. When the user presses OK, I need to get the BufferedImage drawn.
First of all, it should not be an applet but a JPanel (it is not impossible to do it as an applet, but also not trivial). Then you can show the JPanel in a one of three ways.
JOptionPane.showMessageDialog(..). The idea would be to use the ready made OK/Cancel buttons of the option pane to tell the main app. whether to actually use image drawn. I (as a user) would tend to expect the Clear button to not be in the group of buttons that dismisses the dialog. Put that button in the panel you pass to the option pane.
A modal JDialog. Creating a dialog is more work than using an option pane, but also more versatile. For instance, if the panel you put into the dialog has a 'set size of drawing' option, it is easier to resize a dialog than an option pane.
Another card of a CardLayout. The two previous suggestions have the dis/advantage that they will block the app. and the browser until dismissed. This is good in that you can simply query the state of the drawing immediately after it is shown, confident that the user has finished drawing. But it is bad in that the user might have the dialog or option pane sitting on screen for 30 minutes, and the rest of the browser will be inaccessible to them in that time. By instead flipping to a card that shows the drawing panel, the browser is not blocked, and the app. can query the state of the drawing as soon as the user makes one of the OK/Cancel selections.

Two JFrames in one Runnable. First JFrame disappears in the application bar after calling a JFileChooser

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.

Categories