Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EventListener as parameter  RSS feed

 
James McGee
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still a bit new with event handling, but I am curious if the following is accepted practice, or if it is a newbie programmer testing the limits of what is practical...
I have two classes. One is an application, the other is an extension of JFrame. I want the application class to handle the event listening for the JFrame. I can implement ActionListener in the application, and pass an application object to the JFrame, where I can attach it as an action listener for a button in the frame.
I also want the application class to handle the window events for the JFrame, so I could make the application class extend the WindowAdapter class and pass an object of the application to the JFrame where I can register it as the Window Listener.
Basically my code looks like this...
In the application class...
MainFrame mf = new MainFrame(App);
In the MainFrame class constructor...
public MainFrame(EventListener evtList) {
...
button.addActionListener((ActionListener)evtList);
this.addWindowListener((WindowListener)evtList);
...
I am concerned that the parameter for the MainFrame constructor is too generic and doesn't really convey what needs to be passed to make things work.
Would it make more sense to have two parameters (WindowListener wList, ActionListener actList) and just pass the same application object for both?
Thanks in advance for the feedback.
- James
 
Thomas Suer
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I understood you right you have something similar to:
<code>public class App extends Xyz implements ActionListener, WindowListener {...}</code> and
<code>public class MainFrame extends JFrame</code>
1. Then one solution is to pass your App class itself to the MainFrame class, make it a member attribute of MainFrame and register it as an action listener for your desired button and as a window listener for your MainFrame class:
<code>public class MainFrame extends JFrame
{
private App mApp;
private JButton mOkButton;
public MainFrame(App app)
{
super();
mApp = app;
...
initialize();
...
}
private void initialize()
{
...
mOkButton = new JButton("Ok");
mOkButton.addActionListener(mApp);
...
this.addWindowListener(mApp);
}
...
}
</code>
What is important: because the App class implements the listener interfaces you don't have the need to explicitly cast these listeners when registering them.
2. You also could implement both, the window and action listener in the MainFrame class and make the App class a member attribute of the MainFrame class (the reference would be passed to a appropriate constructor of the MainFrame class as mentioned in 1.). In the implemented listener methods of the MainFrame class the appropriate methods of the App class that shall handle these events are to be invoked, if needed the occured event is to be passed.
The second approach would have the advantage that you could do any gui related things, like dis-/enabling components, for instance dependent on the state of processing operations.
Good luck
Tom
[This message has been edited by Thomas Suer (edited October 18, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!