As a followup to my question about the java.awt.Component.getName() property, I’m wondering if there is a way of squirreling an arbitrary object somewhere in a Component, similar to the tag property in .NET? This would be really handy for any code that does work to a component but doesn’t necessarily know what that component is.
For example, say I’m trying to implement an application-wide help system that knows to look at any component currently pointed to by the mouse, reach into that component and pull out it’s help text and display it in it’s own pane on the screen (no, I don’t want to use a tooltip). My answer currently is to use the Name (getName()/setName()) to store the help text, and this will work, but the Name has to be a string. If I wanted to get fancier and store anything other than a string, I think I’d be stuck.
I generally create a hash and put (component, cookie) whenever I add a component to the screen. When you need your cookie object back (in an event perhaps), the event always gets a copy of the component, and then you are just a get(component) away from your cookie.
In some extreme conditions, I’ve subclassed the control and just added a field. It’s a quick and dirty fix since subclassing the component is just like a few lines of code and can go in the same class the file where you are generating your screen. This is only useful if you just need to store your data connected to a single type of control.