• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Events for addition/deletion of components from their parents

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I looking for a way to receive events when components are added/removed from their containers. Hopefully in the swing event will contain the component being added and their parent component.

This is going to be a 3rd party tool (monitoring application) and thus extending JPanel/JFrame/JDialog and overriding the add/delete/remove methods of those classes is not possible.

Thanks,
Jim
 
Marshal
Posts: 65107
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. I looked up ActionListener in the API and found its superinterface (EventListener). Looking through its subinterfaces I found some which might sort out your problem.

CR
 
Jim Harrison
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for the quick reply.

I've seen and used this event listener before. But I'm looking into using the -Xbootclasspath. I've taken the javax.swing.JFrame and edited that .java for the add methods and called back on a singleton in my implementation.

I believe what I am looking for is something a bit deeper in the Swing class possibly at the Manager level.

Thanks,
Jim
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to do anything as drastic as replace the Frame class...

 
Jim Harrison
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nate.

That is actually pretty interesting. However between

ContainerListener myContainerListener = ...

and

Frame[] allFrames = Frame.getFrames();

I believe I won't be receiving events for new frames added. As someone's application is running I want to receive when JFrames & JDialogs (and their children components) are made visible to the user as well as these frames/dialogs are removed/destroyed.

What is interesting is in the Frame.java class the implementation of the getFrames() method. It uses the concept of sun.awt.AppContext. I do not know if this is the deeper (or appropriate) class to use. Additionally, in the Frame.java were 2 other methods: addToFrameList() and removeFromFrameList().

Any thoughts on this?

Thanks,
Jim
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, you won't be able to detect new Frames/Dialogs being launched that way... Frame.getFrames() just returns the list of frames at the time it is called...

It took quite a bit of Googling and digging around in the JavaDocs, but I think I finally found a better solution than re-writing a common class.

Try this approach instead...



I made this example code use reflection so I wouldn't have to write a new app inside it to test it on You can run it on your own classes with:

"java <JDK options, like classpath> Monitor <your classname> <options for your class, if any>"

Replace the "globalListener" AWTListener with your appropriate implementation.

You may just need to listen for the CONTAINER_EVENT_MASK to get events when components are added and removed from containers (like you asked for originally). I added FRAME_EVENT_MASK to get events when new frames/dialogs were shown, and COMPONENT_EVENT_MASK to show events for all components.
[ March 20, 2007: Message edited by: Nathan Pruett ]
 
Jim Harrison
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate,

Thank you so much. This is what I was looking for.

Jim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!