Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Frank Navarro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3397
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Frank Navarro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3397
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3397
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic