• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding All New Components  RSS feed

 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way I can be notified anytime a new Swing component is created? For example, if some code I launch builds a GUI that has 4 buttons on it, I would receive 4 notifications - one for each instantiation of each GUI button.

Thanks,
Jeff
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ComponentListener interface has a componentShown event that is invoked when the component has been made visible.

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/ComponentListener.html
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,

Thanks for the reply. I think I didn't explain the complexity of my problem well (as I re-read it, it looks like a simple problem).

I would like to add listeners at run-time from another Java class. Assume Class A builds some GUI and depending on the actions performed on the GUI, new GUI components are created. Whenever a new component is created, a listener of type L is added to the new component (assuming it supports that type of listener). I was thinking this may have to be some AOP based solution, but I'd like to avoid that if possible.

Thanks,
Jeff
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "created"? I think that is what is causing my confusion of your problem.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instantiated using the new keyword.

So, imagine clicking a button instantiates 3 new components. I want to add listeners to those newly instantiated components.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There aren't any Swing events that tell you when a JButton (for example) is instantiated. What would be sending those events? But there's no point in adding a listener to a component that isn't in the GUI component hierarchy anyway, so what's wrong with finding out when it's added based on (say) a ContainerEvent?
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a ContainerEvent would work just fine in this case. As long as I start with the top-level container, I can find when components are added to the container at run-time. Thanks!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, it seems odd to me that you need to do something quite so dynamically. Somewhere in your code you've written something like:



Give or take some additional code there. Maybe you are looping to add more than one button at a time. Whatever the case, why not just add a listener to the button when your code creates said button?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever the case, why not just add a listener to the button when your code creates said button?


I am guessing because the button is created in some piece of code to which the source is not available, e.g. a 3rd-party library.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, you are correct. Thanks for the help.
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can replace the standard ButtonUI with your own. You won't catch every JButton instantiated, but you will catch every JButton that ever gets a UI delegate, which is probably good enough.



For non-button Swing components you can do the same kind of thing, but this approach won't work for AWT components.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!