Java: Close Specific JFrame Window - java

I hope everyone is doing well.
I've built a hangman game with a swing gui and everything works well enough, HOWEVER I am trying to make a popup show up by constructing a new JFrame object when the user wins or loses with a "you lose" message or what have you. No problem, but I want a specific window to close when activating the button listener on the popup, or when the 'x' is clicked. Assume my program has 3 windows up, and I only want to close 2 of them with one click.
I tried stuff in the area of
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
But that specific one closes all the windows. If you want to see more specific sample code, I would happy to provide it, but it didn't seem necessary for this question.
Either way, I can't figure out how to do this. Is this possible using Swing?
Thank you so much in advance. You guys are always so helpful.

Related

When I dispose and reopen a JFrame, it doesn't show anything

I have a fairly simple Java Application I created with JFrames. There is more than one JFrame in my program. I have a menu launching at the beginning of the program. After going through the menu, I have the menu JFrame disposed.
The main JFrame has a menu button that should launch the exact same menu at a later time. However, when you launch the menu from inside of the ActionListener (when you press the menu button), the JFrame doesn't launch properly. None of the components show up and colors are off.
However, when the menu is launched from outside of the ActionListener, the JFrame shows up perfectly. I have tried multiple methods to fix this, but none of them have worked.
My full program code is available by clicking here.
The main class is "LetsMultiply5.java". This class also sets up the ActionListener.
The JFrame causing the problem is "MenuWindow.java".
"LetsMultiply5.java" calls the "Booter.java" class, which then calls the "MenuWindow.java".
"MainWindow.java" is the JFrame that has the "Menu" button.
For proof, "SpeedModer.java" calls the menu window after it has been disposed, and works.
================================EDIT================================
Also, I'd like to let you know that I realize my code is a little bit messy. I am not sure how else to write the code for this program.
I am forced to use Thread.sleep(x); because the Swing timers aren't what I am looking for. The Swing timers activate an ActionListener when the timer goes off. I need a system that will wait a second before continuing on with the code.
I realize that the while (repeater==0) loop with ActionListeners inside of it seems crazy, but that was the only way I could get it to work. If I put a single ActionListener and just had the while loop do no code inside of it, nothing happens when I press the button.
I would, as MadProgrammer mentioned:
Advice: Scrap your current approach and start again.
However, the way that I have my program currently coded is the only way that I know how to get what I need to do done. I read the tutorials, but still don't know how to improve the code in the way that you told me.
I thank everyone for trying to tell me to improve my bad "Java grammar", but as far as I am concerned, I am not going to continue this program for the next 20 years and make my millions off of it.
I have looked at the Swing timers before and I understand the whole new Timer(speed, this); concept, but I don't understand how this would make my code any better.
If anyone would like to show me how to fix my ActionListeners or Thread.sleep(x); lines, please tell me. Thank you.
You're blocking the Event Dispatching Thread with Thread.sleep(3000); - Don't do this, it will prevent the UI from been painted
See Concurrency in Swing for more details about the problem and How to use Swing Timers for a possible solution
You may also want to consider having a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using CardLayout or JTabbedPane
If you need to block the user temporarily (to display a error message or gather important details), consider using a modal JDialog. See How to Make Dialogs for more details

Java GUI Transparent overlay, which has a menu in it, by clicking a button

Hello lovely community,
I'm Leon and at developing a little jump n run.I'm sitting since rough 24 hours on the laptop to find a solution for my problem. So my GUI starts up with a little menu, in it buttons titled for example with "start game". By clicking this button the game starts. BUT it starts in a new JFrame respectively it starts in a new window.. by playing the puzzle you can press escape for launching the pause menu. BUT again, it opens up in a new window. I need desperately a solution because I cant go on with coding.. Hope anyone can help me. THX Sorry for my English. Leon ;)
Don't use jbuttons. Whenever I make swing based game I use 1 jpanel in which I override paint method and create my own buttons (you'll need to give them functionallity with mouse/key listener, but that is not hard to figure out. If you want to see example code, look up the way minecraft creator does it (google "notch ludum dare" - all games have source code attached)

Java Gui empty frame

I am currently developing a project with a genetic algorithm.
It works fine, I get the results I wants, so no problem on this side.
I displayed all the information in the console so I decided to create a GUI to improve the software.
I have two frames. One is a selection frame where you select your options for the run.
The other is a frame with a textArea. The purpose of this second frame is to display the information about the run in it. For both frame I used the MVC design pattern. It works fine, I have run some tests for the GUI and all is displayed normally.
Now the problem is when I try to integrate the two sides.
I launch my first frame in the main. Then it switch to the second frame and run the Genetic algorithm when the information are complete and the user clicked the OK button. I make verification of the information and let the button enabled false until it is correct ;).
In theory it is suppose to display the second frame empty then launch the GA then input text in the frame dunring the run with the MVC model.
The problem happens when I click the button, the second frame display partially. I mean I have the frame with the title and the red close button but there is nothing in it. I can move the frame with my mouse but if i click the red close button, nothing append, so I guess the frame is kind of freeze. The Ga run perfectly during this time. At the end of the
GA's run the rest of the frame appears with all the information that should have been input in it. So the Mvc model is working good. It is just the frame that don't display normally.
I use the following code :
Vue2 vrga = new VueLorsRunGa(); //create the 2nd frame
vue1.dispose(); //destroy the first one
//code to run the GA
I am not use with the GUI so maybe I made it wrong and so I don't free something needed.
If we take it sequentially, the vue2 is supposed to be totally launch before the ga code is executed. But i read in a article that the GUI's things are managed in a special thread. And so it not suppose to be freeze by the GA ... The GA got its specific threads but I haven't done any for the GUI.
So if anyone had a idea where I can be wrong or how to managed to suppress my bug, it would be great :)
Thank you for help
Scyn
You are probably running your code in the EDT thread (like directly from the actionPerformed method) which blocks other events being processed.
You should move long running operations out of the EDT thread

How to make a JOptionPane appear at an attempt to close a JFrame

I am currently working on a Java application using a JFrame. What I want, is to have it when someone tries to close the window, it will make a JOptionPane appear asking: "Are you sure you want to exit 3DWorld?" and then have two buttons at the bottom saying Yes and No. Any help would be great!
Thanks.
See Closing an Application for some basic information and some helper classes.

OfficeBean won't display after moving containing Swing Panel from one Container to another

I'm developing a Java 6 applet which allows users to view OO (v.3.2) documents (read only), and if they choose, click a button which launches a new JDialog window, with the document displayed in it which allows the user to and mark and redact it as they wish. Once they are done, they can close the JDialog, which saves the document to a server and redisplay the updated document (read-only again) in the original applet window
I guessed that I could do this with a single instance of an OfficeBean, embedded in a Swing Panel. However, I cannot seem to successfully move my Panel (containing the OfficeBean) from the applet to the JDialog when the "Redact" button is clicked. All I get is a blank area in the JDialog where the document should be. I get no errors.
I have currently managed to get round this by creating new instances of the OfficeBean every time I need to display the document (once when the applet is loaded, again when the user chooses to redact and it is opened in a JDialog, and finally when they click "Save" in the dialog and the redacted result is displayed in the applet again.) However this means three trips to and from the server where the documents originate. That seems mad to me.
I'm in no way a Swing expert and may well be making a silly mistake. However, I've done a lot of fiddling around, debugging and googling and can't seem to get this to work. Can anyone help me in this? Am I trying to do something which is fundamentally impossible? I hope not.
One rule in Swing is that a component can only be displayed / attached to one part of the gui "tree" at a time. When you "move" your component to the dialog, are you first removing it from the applet?

Categories