• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to create close buttons

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MainScreen.this.dispose();

or do you really want
System.exit(0);
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic