• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create close buttons  RSS feed

 
Marcelo Sena
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a very simple gui application:

There is a Run Class that run the application an start the MainScreen class that calls the initialize method from the constructor creating a button called "aButton" when the user presses aButton, the MainScreen closes.

So this is how i have implemented the aButton mouseClicked method:

"me" in this case is a variable from MainScreen class that is equal to the current instance of MainScreen.

I know this is probably the wrong way of doing this, so, what I want to know is how to create a close button that has access to the frame without creating a new variable that points to the current frame.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MainScreen.this.dispose();

or do you really want
System.exit(0);
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a job for the setDefaultCloseOperation method of JFrame. You have (I think) 4 options:
  • Do nothing.
  • hide (which I think is the default behaviour)
  • dispose
  • exit (equivalent to System.exit(0)
  • I think this topic is more appropriate for the Swing forum, so I shall move it.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!