xdevel2000
xdevel2000

Reputation: 21364

Cloning a Swing component

I'm implementing a copy/paste example and when I set the clipboard like:

clipboard.setContents(new MyTransferable(image_label), null);

I want that image_label (is a JLabel) is a clone of the copied label

How can I do that?

Upvotes: 0

Views: 4427

Answers (2)

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17369

Since all Swing components are serializable, you can just serialize to memory/byte stream and deserialize back. This makes a good utility method for deep copying classes. If you want to speed it up a little bit, rewrite in/out streams to remove concurrency related code.

Upvotes: 6

Geoffrey Zheng
Geoffrey Zheng

Reputation: 6640

JComponents don't override clone, but they are beans so you can use something like BeanUtils to copy the properties from a label to a new one.

Upvotes: 0

Related Questions