Event-Handling: Using one single class for different events?

S

S.T

Hi!

I am rather new to Java, so please, don't blame me for this question.

What I want is to seperate the GUI- from the Application code. Hence,
I have created three classes:

1. a Main class
2. a MainFrameCommand class, which parses the events
3. a MainFrameGUI class, that creates and paints the gui

(for a trivial example see end of this post)

Now, my question is this: Is it correct, to put all the various events
into one single command class (i.e, Focus-, Key-, Mouse-, and Windows-
Events) and then register the various event-listeners using this
single class?

It works perfectly, however, I wonder if this is a legal way to do so,
or is it necessary to create a seperate class for every different
event-listener-type?

Example:

------------------------------------------------------------
1. Main Class
------------------------------------------------------------
class Main {

public static void main(final String[] args) {
MainFrameCommand cmd = new MainFrameCommand();
MainFrameGUI gui = new MainFrameGUI(cmd);
}
}

------------------------------------------------------------
2. MainFrameCommand Class
------------------------------------------------------------



import java.awt.*;
import java.awt.event.*;

class MainFrameCommand
implements KeyListener, MouseMotionListener, WindowListener {

/* Key Listener */
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {}

/* MouseMotion Listener */
public void mouseMoved(MouseEvent event) {}
public void mouseDragged(MouseEvent event) {}

/* WindowListener */
public void windowClosed(WindowEvent event) {}
public void windowOpened(WindowEvent event) {}
public void windowClosing(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
}


------------------------------------------------------------
3. MainFrameGUI Class
------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

class MainFrameGUI extends Frame {
public MainFrameGUI(MainFrameCommand cmd) {
super("Window");
setSize(300, 300);
setVisible(true);

/* !!!!!!!!!!!!!!!!!!!!!!! HERE'S THE CRUCIAL QUESTION
!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!! Is it legal to add register all these
!!!!!!!!!!!!!!!!!!!!!!! event listeners using the same object?!?
*/
addKeyListener(cmd);
addWindowListener(cmd);
addMouseMotionListener(cmd);
}

public void paint(Graphics g) {}
}
 
T

Tom Hawtin

S.T said:
Now, my question is this: Is it correct, to put all the various events
into one single command class (i.e, Focus-, Key-, Mouse-, and Windows-
Events) and then register the various event-listeners using this
single class?

It works perfectly, however, I wonder if this is a legal way to do so,
or is it necessary to create a seperate class for every different
event-listener-type?

It's legal to have and use a class that implements multiple listeners,
and you often see tutorial code do it. However, it's not really a great
idea.

To keep your code as simple (though not necessarily as short) as
practicable, use a different class to implement each interface. For
listeners these will often be anonymous inner classes.

In the case of your Frame object, you probably don't want to override
the paint method. Instead add a component with an overriden paint. That
means you don't and shouldn't extend Frame.

Even though you are using AWT instead of Swing, I would suggest wrapping
the body of your main in the java.awt.EventQueue.invokeLater(new
Runnable() { public void run() { }}); boilerplate. AWT isn't as
thread-safe as it claims to be, and Swing would probably make a better
choice anyway.

Tom Hawtin
 
C

cyprian

It's legal to have and use a class that implements multiple listeners,
and you often see tutorial code do it. However, it's not really a great
idea.

To keep your code as simple (though not necessarily as short) as
practicable, use a different class to implement each interface. For
listeners these will often be anonymous inner classes.

In the case of your Frame object, you probably don't want to override
the paint method. Instead add a component with an overriden paint. That
means you don't and shouldn't extend Frame.

Even though you are using AWT instead of Swing, I would suggest wrapping
the body of your main in the java.awt.EventQueue.invokeLater(new
Runnable() { public void run() { }}); boilerplate. AWT isn't as
thread-safe as it claims to be, and Swing would probably make a better
choice anyway.

Tom Hawtin

i think you're in the right direction but using the interfaces for
events makes your code bloated. use built in adapters for the
interfaces, and declare instances of the adapters in your main class.
that way you implement only the code you want. in java UI delegating
of events and events capture, you'd probably be writing better if all
your events were handled in a single class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top