aspose file tools*
The moose likes Beginning Java and the fly likes Copying Components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying Components" Watch "Copying Components" New topic
Author

Copying Components

Ted Newholm
Greenhorn

Joined: Dec 27, 2011
Posts: 10
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.

Thanks for any help.

The program is as follows:

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Ted Newholm wrote:


lbl2.equals does not create a copy,this method tests if lbl2 is equal to lbl1 and returns true if yes, or it returns false.

Try clone method instead:
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ireneusz Kordal wrote:Try clone method instead:

I thought of that, but nothing in the JLabel hierarchy implements Cloneable, so I suspect OP will get CloneNotSupportedException (or it won't compile).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a simple way to have components borders/colors/whatever the same, without having to repeat all the changes, is to roll-your-own

Ted Newholm
Greenhorn

Joined: Dec 27, 2011
Posts: 10
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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
Ted Newholm
Greenhorn

Joined: Dec 27, 2011
Posts: 10
Thanks Michael.

That's very helpful. Much appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copying Components