• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking to confirm listener behavior  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a JFrame, added a Window listener via addWindowListener and implemented the following code.

public void windowClosing(java.awt.event.WindowEvent evt)
{
System.out.println("Inside Listener 1");
}


I have my own class that extends JTable and added a procedure to my class to get the parent frame via parentFrame = (JFrame)SwingUtilities.windowForComponent(this);
The return value "parentFrame" is stored within a variable defined within the my class which is then used to execute addWindowListener to add a new listener.
The same method (windowClosing) is implemented.

public void windowClosing(java.awt.event.WindowEvent evt)
{
System.out.println("Inside Listener 2");
}

When it is compiled and executed both listeners are processed.
Both System.out.println commands are executed as the following two lines are sent to the screen.
Inside Listener 1
Inside Listener 2

I was actually expecting the addition of the second listener to overwrite the first without having to execute the removeWindowListener, but it appears you can add many different listeners and all will execute.
Is this the expected behavior?
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't to extends JFrame, create this Object as local variable, why to casting, searching in component three is useless, because is accesible to/from this Object
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears you can add many different listeners and all will execute. Is this the expected behavior?


Yes.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
It appears you can add many different listeners and all will execute. Is this the expected behavior?


Yes.


Thanks for confirming. I assumed it would simply replace one object for the other. I didn't see where the docs hinted that you could have multiple listeners of the same type.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:I assumed it would simply replace one object for the other. I didn't see where the docs hinted that you could have multiple listeners of the same type.

The key word is 'add'. If the method name were 'setxxListener' then you should expect there to be just one listener at a time. But when the method name is 'addxxListener' and the API says "Adds the specified xx listener to receive xx events" then you should think of a collection. Usually when the API is talking about something singular (such as in a set method) the description will use the internal value as the point of reference rather than the parameter: "Specifies the xx listener to receive xx events" or "Sets the xx listener to the specified object".
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
The key word is 'add'. If the method name were 'setxxListener' then you should expect there to be just one listener at a time. But when the method name is 'addxxListener' and the API says "Adds the specified xx listener to receive xx events" then you should think of a collection. Usually when the API is talking about something singular (such as in a set method) the description will use the internal value as the point of reference rather than the parameter: "Specifies the xx listener to receive xx events" or "Sets the xx listener to the specified object".


Thanks, that is a great rule of thumb to follow and easy to remember.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also getXXXListeners methods, which return an XXXListener[].
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There are also getXXXListeners methods, which return an XXXListener[].


Just wondering if you want to remove all the Listeners and execute getXXXListener how would you know if there are is only one or many Listeners?
Do you have to loop through every time a getXXXListener is done and check when null to determine if you have retrieved them all?
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use all the usual suspects for arrays. Start with myArray.length. I do not know whether any of the elements can be null, but it is possible to get a zero‑length array. Have you ever tried addXXXListener(null)?
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely it always returns all the Listeners as an array?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!