Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
S.T - 20 May 2007 19:31 GMT
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) {}
}
the_edge123.nospam@club-internet.fr - 21 May 2007 08:28 GMT
> 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.
This link should be interesting for a beginner
http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html
Tom Hawtin - 21 May 2007 09:13 GMT
> 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-
[quoted text clipped - 4 lines]
> 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
cyprian - 21 May 2007 10:18 GMT
> > 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-
[quoted text clipped - 24 lines]
>
> 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.
S.T - 21 May 2007 17:32 GMT
Thank you all for your accurate answers! It helped really a lot.

Sincerly
S.T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.