• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

frame close operations (GUI)

 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm starting to play around with GUI's and i'm trying to write to a file. i've been using the following code in all of my GUI test programs:

chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

however, i want to be able to perform other operations on close, in particular, close the file i have written to...how do i do this?
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can go back to the older way of using a WindowListener:
 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, i made my class implement WindowListener, and wrote the function you mention, but i get this error:

myFunction is not abstract and does not override abstract method windowActivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener
public class myFunction implements ActionListener, WindowListener {
^

the only why i don't get this error is if i include:

public void windowActivated(WindowEvent e) {}

but i have to include one for each of the WindowListener methods. why do i HAVE to override all the other functions just because i want to override windowClosing?
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting started with event listeners can seem confusing because of the many options available. A good resource to help you in learning java is the Creating a GUI with JFC/Swing trail in the
java tutorial.

Some general observations:
When you implement an interface you are required to implement every method defined in the interface. Extending an adapter class allows you to implement only the methods you are interested in. This is a convenience. See General Information about Writing Event Listeners for more, especially the section Event Adapters.

Options:
1 - For simple programs you can have your class implement an EventListener interface, eg, ActionListener. And of course we must implement all the methods defined in the interface.

2 - If we need to keep a reference to a listener we can use either an outer class or a named inner class. There are two ways to do a named inner class:

3 - For a one–time use with no need to refer to the listener, ie, we don't need/want a reference to the listener, we have the anonymous inner class. This is what I used in the first reply above. And I used a WindowAdapter for the WindowListener. If I had specified a WindowListener instead I would have been required to implement the other methods within the listener.

For more on inner class listeners see Inner Classes and Anonymous Inner Classes section on the last (link above) page.
[ June 16, 2004: Message edited by: Craig Wood ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing...
 
Eric Snell
Greenhorn
Posts: 28
Java Mac MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Wood:
You can go back to the older way of using a WindowListener:


If you leave the default close operation as JFrame.EXIT_ON_CLOSE, you don't need the call to System.exit() in your windowClosing() method.
 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Craig, your explanation helped immensely...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic