• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to create a JButton with an ActionListener that will allow me to quit JFrames .

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.

Hope this makes sense, I've tried to work it out but no luck as yet. So here's my question.
How do I go about calling a class who's function is to exit a JFrame from more than one JFrame?

In my code below I have a JFrame with 2 JButtons. One that opens another instance of a JFrame and a JButton that quits the application. I would be like to be able to place another JButton that also quits on the second JFrame using the class code below thereby avoiding having to write it twice. Is this possible.



again


I'm a complete noob so any hints etc much appreciated.
Thanks guys.
 
Bartender
Posts: 2907
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your indentations are not proper,

Lets analyze the issue, what does Quit class do when an action is performed:
It executes the function:



This is a call to stop your program and quit everything.
As i can guess, this is not the behavior you require.

There is a function called dispose() in Jframe that you might want to look at,

Also I see no purpose why Thing and Quit should be static classes.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
Welcome to the Ranch.

Having multiple base frames is a bad bad idea. Instead use a single JFrame and JDialog (check out JOptionPane for details on how to create and use modal dialogs)
Check out what JFrame#setVisible(boolean isVisible) does.

Swing related. Moving.
In future please post your UI questions in the Swing forum.
http://faq.javaranch.com/java/CarefullyChooseOneForum
 
Mark Toddd
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tips guys.

I'll be studying Java toward the end of the year at uni and am trying to get to grips with some of the basics.
I'll be sure to post in the right forum next time.

cheers
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic