Results 1 to 8 of 8
  1. #1
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default Is there any real advantage of using only one listener

    hello,

    Is there any advantage (performance, memory etc.) of using only one static listener(mouse, action whatever) over new listener for each object....

    What do guys think....

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    More than one issue here. One is performance and another is maintainability. The small benifit for performance will be offset by the larger cost in mixing logic and threads etc in one method and class vs isolating them into separate classes and then trying to figure out how to find a problem or where to add new features.
    For a small app, perhaps things will be simple enough that there will be no confusion. In a larger app, keeping things separate may save time and trouble figuring out what went wrong.

  3. #3
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    For a small app, perhaps things will be simple enough that there will be no confusion. In a larger app, keeping things separate may save time and trouble figuring out what went wrong.
    Yes, I agree.

    I don't know how to put it...
    I recently saw the sun's tutorial example of TabbedPane custom comp header ( new feature in Java 6), and make my own test app (without the static list.) where one can open insane number of tabs, then in a low memory system I get "Low Virtual Memory" warning from OS, and start to think about only one static listener...... but I do agree with you for large code this brings up the maintainability issues as you pointed out.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Why static listener?
    One method for all listeners vs one listener per component is described as: spagetti vs ravioli

  5. #5
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    umm! are you talking about something like:

    button1.addMouseListener(this);
    button2.addMouseListener(this);
    button3.addMouseListener(this);

    vs.

    button1.addMouseListener(new MouseAdapter(){......});
    button2.addMouseListener(new MouseAdapter(){......});
    button3.addMouseListener(new MouseAdapter(){......});

    I'm NOT talking about them...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    I'm NOT talking about them...
    What is this a guessing game?

  7. #7
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    :D

    ok, let's look at an example at here :

    Tabs With Custom Components
    How to Use Tabbed Panes (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    Java Code:
    private final static MouseListener buttonMouseListener = new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                Component component = e.getComponent();
                if (component instanceof AbstractButton) {
                    AbstractButton button = (AbstractButton) component;
                    button.setBorderPainted(true);
                }
            }
    
            public void mouseExited(MouseEvent e) {
                Component component = e.getComponent();
                if (component instanceof AbstractButton) {
                    AbstractButton button = (AbstractButton) component;
                    button.setBorderPainted(false);
                }
            }
        };
    Vs.

    Non-static listeners....

    only one (static) MouseListener for every tab you create
    vs.
    For each tab a new MouseListener...
    Last edited by playwin2; 09-16-2008 at 06:20 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    static vs new object?

    Try writing a large program and see what happens to the design.

    Seems like there'd be problems communicating the events to the controlling classes.
    The standard events only pass components and from them containers. How would you get to the constructing/controlling class that needs to know about the event?
    Last edited by Norm; 09-16-2008 at 07:22 PM.

Similar Threads

  1. Replies: 0
    Last Post: 08-13-2008, 11:46 PM
  2. Replies: 0
    Last Post: 08-01-2008, 12:59 PM
  3. Replies: 0
    Last Post: 07-31-2008, 12:33 PM
  4. real world java
    By Zosden in forum Forum Lobby
    Replies: 6
    Last Post: 06-25-2008, 06:39 AM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-21-2008, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •