Results 1 to 15 of 15
  1. #1
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default Question on Multiple Listeners implementation

    I've been wondering about this. Lets say you have a GUI designed that has a whole bunch of GUI Fields, such as a whole bunch of Buttons or a GUI which listens for property changes on a bunch of different objects. Is it better to use anonymous inner classes to listen on each object or just have the GUI implement the Listening interface and handle it accordingly?


    Example:

    Bunch of inner classes:
    Java Code:
    	public void initConnections() {
    		getButton1().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button1Clicked();
    			}
    		});
    		getButton2().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button2Clicked();
    			}
    		});
    		getButton3().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button3Clicked();
    			}
    		});
    		getButton4().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button4Clicked();
    			}
    		});
    		getButton5().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button5Clicked();
    			}
    		});
    		getButton6().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button6Clicked();
    			}
    		});
    		getButton7().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button7Clicked();
    			}
    		});
    		getButton8().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button8Clicked();
    			}
    		});
    		getButton9().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button9Clicked();
    			}
    		});
    		getButton10().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button10Clicked();
    			}
    		});
    	}
    Or a GUI which implements the ActionListener
    Java Code:
    	public void initConnections() {
    		getButton1().addActionListener(this);
    		getButton2().addActionListener(this);
    		getButton3().addActionListener(this);
    		getButton4().addActionListener(this);
    		getButton5().addActionListener(this);
    		getButton6().addActionListener(this);
    		getButton7().addActionListener(this);
    		getButton8().addActionListener(this);
    		getButton9().addActionListener(this);
    		getButton10().addActionListener(this);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == getButton1()) {
    			button1Clicked();
    		} else if (e.getSource() == getButton2()) {
    			button2Clicked();
    		} else if (e.getSource() == getButton3()) {
    			button3Clicked();
    		} else if (e.getSource() == getButton4()) {
    			button4Clicked();
    		} else if (e.getSource() == getButton5()) {
    			button5Clicked();
    		} else if (e.getSource() == getButton6()) {
    			button6Clicked();
    		} else if (e.getSource() == getButton7()) {
    			button7Clicked();
    		} else if (e.getSource() == getButton8()) {
    			button8Clicked();
    		} else if (e.getSource() == getButton9()) {
    			button9Clicked();
    		} else if (e.getSource() == getButton10()) {
    			button10Clicked();
    		}
    	}

    Is this just down to personal taste? Or is there a valid reason to select one over the other such as better performance or uses less space?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    I'd select the first alternative; one of the less important reasons is speed: if it's the last component in the row that fired the event if has to pass all those (failing) if-else tests before it gets handled.

    A more important reason is smaller methods that are independent on the other gui objects. It is not a 'god-handler' that manages all the events.

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A good example, I think, is a calculator application. I would have all the number buttons and the period use the same ActionListener, the memory buttons share another, the clear/clear all buttons share another, and the operations (+, -, *, /, =) buttons share another listener. So you would have many buttons and perhaps 4 listeners with buttons that can be grouped into similar actions sharing the same listener. And you could argue for all the non-numeric buttons having their own listener even.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm using the first selection too. One of the reason I've seen is that code is more clear, easy to read. Specially with a dry-run it's easy to find-out what the next step would be.

    Second reason is that as Jos pointed, avoid passing all if-else conditions to find the exact one on action fired. But at the same time I don't think there is much difference with less number of events like in the code.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Well....

    It is all depends not only of your code style but of a code structure you want to use ;)

    Case 1:

    Emm... It is good one but it is also quite limited thing if you want to use it with your method's params ;)
    Just imagine

    Java Code:
    void hoho([B]String a[/B],int b)
    {
    
    getButton1().addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button1Clicked();
                                    ((JButton)e.getSource()).setText(a);
    			}
    		});
    }
    Case 2:

    Quite long one, yes... But you can make it lighter by checking not objects themselves but their names ;)

    Java Code:
    public void actionPerformed(ActionEvent e) {
    
    String name=((JButton)e.getSource()).getName();
    
    if("NAME".equals(name)){...}
    
    }
    So as you can see many ways to think it ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Quote Originally Posted by Webuser View Post
    Case 2:

    Quite long one, yes... But you can make it lighter by checking not objects themselves but their names ;)

    Java Code:
    public void actionPerformed(ActionEvent e) {
    
    String name=((JButton)e.getSource()).getName();
    
    if("NAME".equals(name)){...}
    
    }
    So as you can see many ways to think it ;)
    What exactly do you mean by lighter? Is it that the actual if/else clause takes a shorter amount of time? I've seen this in a couple of applications I've had to debug through and I was always wondering why someone would do it this way.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Since we're talking GUI code where the time it takes the user to click a button is on the order of seconds, and the time to zip through an if block less than a ms, time is not the issue here, maintainability is.

  8. #8
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I agree but I find using the Names in the Listener is less maintainable as GUI objects may be updated, and not have the name set correctly. In fact, I never set the names of my GUI objects, is this a mistake? And if so why? The only reason I would see the need for this is if you don't keep track of the GUI elements(JButtons, JTextFields).

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

    Default

    The problem with using names as shown in the code where it uses literals is that anytime there is a change to one of the names, you MUST change it in TWO places and they changes must match. Better to use a final String and use its value in the two places.

    Another point: name.equals(value) has got to be more expensive than comparing the addresses of two objects: obj1 == aComponent
    Object obj1 = e.getSource(); // get pointer to the component with the action

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Oh my...

    I don't saying you MUST always do like this :) I just want to MARK that there is no plot as for prog structure and you should come to terms with a current situation.

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Wink

    Quote Originally Posted by StormyWaters View Post
    What exactly do you mean by lighter? Is it that the actual if/else clause takes a shorter amount of time? I've seen this in a couple of applications I've had to debug through and I was always wondering why someone would do it this way.
    Just imagine the weight of JMenu and the weight of word like a "menu1" ;)
    If to come down the Java inner things you can remember a term as bytes, heap memoty etc...

    How many bytes does JPanel or String objects contain? It is quite interesting how many if/else st there are but it is more interesting how long each if/else st would take? :rolleyes:
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Lol,

    Sorry if I am probing too hard, I'm just looking for the best way to continue designing and I like to know the reasoning behind it.

    From all the responses so far, using the anonymous inner classes seems to be the best solution.

    I have another question though,
    Fubarable described a calculator design with 4 maybe 5 listeners(Reply #3), but would it be better for each JButton to have its own anonymous listener which would call the corresponding method and have the Calculator class(assuming here) handle everything? Why is there a need to use the 4 to 5 Listeners?

  13. #13
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Quote Originally Posted by Webuser View Post
    Just imagine the weight of JMenu and the weight of word like a "menu1" ;)
    If to come down the Java inner things you can remember a term as bytes, heap memoty etc...

    How many bytes does JPanel or String objects contain? It is quite interesting how many if/else st there are but it is more interesting how long each if/else st would take? :rolleyes:
    So what you are saying that using the if/else statements with the names would be quicker?

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    Just imagine the weight of JMenu and the weight of word like a "menu1" ;)
    Only in your dreams would those components weigh something. Using String comparisons to find out with which component you're dealing is a detour and should be advised against.

    kind regards,

    Jos

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by StormyWaters View Post
    Fubarable described a calculator design with 4 maybe 5 listeners(Reply #3), but would it be better for each JButton to have its own anonymous listener which would call the corresponding method and have the Calculator class(assuming here) handle everything? Why is there a need to use the 4 to 5 Listeners?
    There is no dogma w.r.t one Listener versus multiple Listeners. If a calculator numeric button can produce it's 'value' (0, 1, 2 ... 9) a single listener would be fine; i.e. it can cast the source of the event to a 'NumberButton', retrieve the numerical value and go on with the show. Calculator keys with a clear different functionality should (imho) have different Listeners; there is no need to have one big listener test for the source of the event (which button was pressed) and most certainly there is no need for String comparisons to figure out the source of the event.

    kind regards,

    Jos

Similar Threads

  1. Question about linked implementation
    By bdario1 in forum New To Java
    Replies: 5
    Last Post: 04-23-2010, 12:00 PM
  2. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 08:21 PM
  3. array-based implementation question
    By intelli in forum New To Java
    Replies: 1
    Last Post: 04-18-2009, 06:18 PM
  4. Replies: 5
    Last Post: 07-18-2008, 05:30 PM
  5. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 11:06 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
  •