Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying Components

 
Ted Newholm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael.

That's very helpful. Much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic