You would need to register as a
MouseListener, and maybe also try dynamically casting to some other component types so you can register as other listener types. To do this for all components shouldn’t be too difficult, it just takes some recursion, then you need to add / remove yourself as a listener based on the call backs.
The tricky bit is playing back any recorded changes, if you need to play back the changes at a later date, say if you close and reopen the application then you will need some way to identify each component individually - either you will have to build up a hash code that uniquely identifies each component based on some properties about it or make sure every component is uniquely named. What will it do if a component is in a different position with a different size, or starts with different visibility? If you only need to playback while running that instance it will be easier but still there will be gotchas. For example I’m not sure what would be the best way to handle a component being deleted! How are you going to add it back again on a redo? But for…
movement, size change, visibility etc?
…it should be just fine.
What are you actually trying to achieve? if you want user interaction to be recorded and played back then I think you only really need to record mouse and keyboard events and play those back, then let the components control their size, position, visibility, etc. I recently started a project doing this with mouse events maybe I’ll finish it and put it up at some point, but I found myself wanting every component to be uniquely named so that when I played back mouse events, if components had moved I wanted the event to be played back relative to the new position of the component. I hadn’t got to keyboards events on that project, I suspect this will be harder but I might be wrong.
If you do want to listen to mouse events then adding yourself as a listener of all nested children is a little easier, just do something like
componentToWatch.addMouseListener (this, true);
Which reminds me, you should be able to record all mouse events except
mouseMagnify(). @jules is there any reason why
mouseMagnify() isn’t part of the