I'm writing a visual app using Swing; I've got a JMenuBar in the main
frame, on of the JMenuItems in one of the JMenus is an "exit" option -
obviously when this option is chosen, I'd like the app to close. I've
been implementing this so far by making the class of the application
implement ActionListener, and adding an actionPerformed function:
...
class myGUIApp implements ActionListener{
...
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
...
public void actionPerformed(ActionEvent e){
myMainJFrame.dispose();
}
I'd like for all ActionListener to be an external class; this of itself
is not a problem, however, I don't know how to pass the dispose()
message to the main JFrame of the app if the ActionListening is handled
externally - anybody have any pointers for me?
Arnaud Berger - 20 Apr 2005 12:35 GMT
Hi,
What about :
class ExternalListener implements ActionListener{
private JFrame frame;
public ExternalListener (JFrame _frame){
frame=_frame;
}
public void actionPerformed(ActionEvent e){
frame.dispose();
}
and addActionListener(new ExternalListener(this));
Regards,
Arnaud
> I'm writing a visual app using Swing; I've got a JMenuBar in the main
> frame, on of the JMenuItems in one of the JMenus is an "exit" option -
[quoted text clipped - 16 lines]
> message to the main JFrame of the app if the ActionListening is handled
> externally - anybody have any pointers for me?
Kurt Underhay - 20 Apr 2005 14:20 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> and addActionListener(new ExternalListener(this));
Works like a charm - thanks for the suggestion.
Thomas Weidenfeller - 20 Apr 2005 13:05 GMT
> I'd like for all ActionListener to be an external class; this of itself
> is not a problem, however, I don't know how to pass the dispose()
> message to the main JFrame of the app if the ActionListening is handled
> externally - anybody have any pointers for me?
Some info from
http://groups.google.com/groups?selm=d2gn1a%24f7k%241%40newstree.wise.edt.ericsson.se
should be applicable in a similar way.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq