• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using an ActionListener to Close a Frame  RSS feed

 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JButton registered for an ActionEvent, what I would like to do is have it get the String from a textbox and then close the frame, any ideas on how to close the JFrame using the action event?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, how are you implementing your frame?

you can either use two classes, or just make

the main method an inner method of the JPanel class..

if it's just one class go this route:



hope this helps a lil bit..

Justin
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Just use dispose and the hide

JFrame: MyFrame
JTextField: MyText



You might want to set the JFrame.setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mathias Nilsson:
Hi!

Just use dispose and the hide

JFrame: MyFrame
JTextField: MyText



You might want to set the JFrame.setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );


Calling hide() on a Window that has been disposed is rather pointless. Furthermore, you shouldn't use hide to begin with as it's deprecated and replaced with setVisible(boolean). Using DO_NOTHING_ON_CLOSE isn't very intuitive to the user unless you properly register a WindowListener and handle the event as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!