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

Discussion in 'Java' started by S.T, May 20, 2007.

  1. S.T

    S.T Guest

    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) {}
    }
     
    S.T, May 20, 2007
    #1
    1. Advertisements

  2. S.T

    Guest Guest

    Guest, May 21, 2007
    #2
    1. Advertisements

  3. S.T

    Tom Hawtin Guest

    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
     
    Tom Hawtin, May 21, 2007
    #3
  4. S.T

    cyprian Guest

    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.
     
    cyprian, May 21, 2007
    #4
  5. S.T

    S.T Guest

    Thank you all for your accurate answers! It helped really a lot.

    Sincerly
    S.T.
     
    S.T, May 21, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.