File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Closing a Window with a JButton? (+other newbie questions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Closing a Window with a JButton? (+other newbie questions)" Watch "Closing a Window with a JButton? (+other newbie questions)" New topic
Author

Closing a Window with a JButton? (+other newbie questions)

Frank Navarro
Greenhorn

Joined: Feb 11, 2013
Posts: 4
Hi I'm new to the forum, new to Java and in fact all programming. I started practicing with some beginner material recently just for fun, so I apologize in advance if my terminology is off and for the long and possibly ugly code I'm posting here. I would appreciate any critique/advice I can get.

So, for the first time I tried writing a program using only windows (nothing prints to the console). Here is my code, it consists of three classes, first the class with the main method (I'm using Eclipse btw):



^ This is just a simple dice rolling game that takes place in dialog boxes with JOptionPane. At one point it calls a method from the "Wait" class that pauses the program for one second:



I sincerely don't understand the point of handling exceptions, but I'm told I have to do this to make this part work.

At the end of the first class I call a method from the "RollAgain" class, which has the code for a JFrame I tried to make with WindowBuilder that gives the user the option of playing again . I did this by calling the main method again from the first class (so basically both classes end by executing the other class, creating a loop), I saw several advanced looking (for me) examples that I just didn't understand, so I tried doing it this way first...



...and it worked! Eclipse just forced me to put null in the parameter, I don't know why but as long as it's working I'm not going to complain.

The RollAgain Class (this was the most difficult part to get working and I still don't understand the structure as well as I would like, most of it was autogenerated from WindowBuilder and the design view):



The only problem I still have is the "Roll Again?" window refuses to close when I click the yes or no buttons, and a new window is created on each loop, what am I doing wrong? Also I can't seem to center anything in my window from design view.

Thanks to anyone who took the time to read all this
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Welcome to the Ranch.

When you say "closing the window" is this mean exiting the app entirely, or like in your code dispose or hide the jframe?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Frank Navarro
Greenhorn

Joined: Feb 11, 2013
Posts: 4
K. Tsang wrote:Welcome to the Ranch.

When you say "closing the window" is this mean exiting the app entirely, or like in your code dispose or hide the jframe?



Just to close the JFrame, specifically when the user clicks the "yes" button and it runs again, otherwise I end up with duplicate windows on each run. Although the whole program should end when the user clocks "no"
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Frank Navarro wrote:
Just to close the JFrame, specifically when the user clicks the "yes" button and it runs again, otherwise I end up with duplicate windows on each run. Although the whole program should end when the user clocks "no"


Then I suggest you NOT to extends JFrame in your class(es). So that you can get a reference to the "frame" variable in in your action listener. This way you would not need to have code like "new reset.main(null)" stuff, which is really awkward.
Frank Navarro
Greenhorn

Joined: Feb 11, 2013
Posts: 4
K. Tsang wrote:
Frank Navarro wrote:
Just to close the JFrame, specifically when the user clicks the "yes" button and it runs again, otherwise I end up with duplicate windows on each run. Although the whole program should end when the user clocks "no"


Then I suggest you NOT to extends JFrame in your class(es). So that you can get a reference to the "frame" variable in in your action listener. This way you would not need to have code like "new reset.main(null)" stuff, which is really awkward.


Ok, I'll try to rewrite the class, like I said I used WindowBuilder but I think maybe it would be simpler without the auto generated code anyway, that's where the "extends JFrame" came from.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

About auto-generated code, don't rely on the IDE to generate code. It's better to write your own code. Besides writing your own code will allow you to learn the API (Swing in this case) and tends to be more neat.
Frank Navarro
Greenhorn

Joined: Feb 11, 2013
Posts: 4
Finally got this to work! And it looks cleaner than the other code.

This is what I needed:

if (e.getActionCommand().equals("No")) {
dispose();
}

And apparently I needed to stop writing the frame.dispose();

Final code:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> if (e.getActionCommand().equals("No")) {

it cannot be anything else - that particular listener is added only to the 'no' button
same for the 'yes' button/listener
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Closing a Window with a JButton? (+other newbie questions)