Reputation: 305
I have this problem to solve where you have a JPanel
and JLabel
and you have to clone JLabel
with drag and drop and create a JLabel
clone on the JPanel
where JLabel
was dropped. First thing I'd like to ask is if it's possible to implement Cloneable
interface to JLabel
in anonymous class like listeners, so I don't have to write my own class that extends JLabel
and implements Cloneable
. I know how to drag and drop JLabel
to JTextField
by transfering "text" property, but I have no idea how to clone a JLabel
to JPanel
.
Upvotes: 1
Views: 449
Reputation: 205775
Implementing the Cloneable
interface is unlikely to achieve any beneficial effect in this context. In Effective Java, Second Edition, the author outlines the vagaries of cloning objects in Item 11: Override clone judiciously.
Drag and Drop transfers the data, not the container. Because String
is immutable, there's no reason to clone a String
at all. For mutable data, a copy constructor or factory method makes more sense.
Because JLabel
is not user editable, it may be an unexpected target for DnD, although it is possible. Alternatively, you can add a suitable component at run time using the container's add()
method, followed by validate()
and repaint()
.
Upvotes: 1