I have a small test program to try and copy a new label from an existing one, (not sure if this is even possible). The new label copies but it has no border. If I substitute lbl2 = lbl1 for lbl2.equals(lbl1) I get a single bordered lbl2. I think this is something to do with By Reference and Buy Value??
Also, for some reason, lbl1 is not accepting the background color.
a simple way to have components borders/colors/whatever the same, without having to repeat all the changes, is to roll-your-own
Joined: Dec 27, 2011
Thanks for replies. I checked out clone() and it seems to get a lot of bad press. I really didn't understand all the concerns but it seemed that the clone method may be unreliable under certain circumstances and result in an error. In fact there didn't appear to be any sure fire way of duplicating components.
Regarding Michael's reply, if I understand what you've done, it means that the any attributes placed in the MyLabel class would apply to all labels. Have I got that right? If so, how would I create labels that had the attributes of existing labels (where the existing label attributes differed from each other).
On a related matter. I have another program where I have added a large number of TextAreas to a GridBagLayout, (it looks a bit like a spreadsheet). Is there any way to identify a component in a particular gridbaglayout "cell" through the use of gridx and gridy, e.g. pseudocode: name = textArea.getName(gridx, gridy).
Thanks for your help.
Joined: Jun 09, 2003
> it means that the any attributes placed in the MyLabel class would apply to all labels. Have I got that right?
yes, to all labels created as MyLabels().
> how would I create labels that had the attributes of existing labels (where the existing label attributes differed from each other).
probably easiest way is to setup your own clone method/class. Most 'attributes' have setter and corresponding getter methods,
so it's just a matter of 'setting' the attributes of the new label to 'getting' attributes of the existing label
here's an example based on your posted code, it only has a few setters/getters based on what's been set for lbl1.
there are lots more that can be added the same way. Note that if using something similar for Swing components other than
labels, not all have the same setter/getter pairing e.g. JComboBox has no setText/getText, whereas labels, textfields etc do.
> Is there any way to identify a component in a particular gridbaglayout "cell" through the use of gridx and gridy
of not seen that anywhere, perhaps (if using the mouse to id gridx/y) [container].getComponentAt(Point p) might be worth looking into