• Post Reply Bookmark Topic Watch Topic
  • New Topic

how use "clone"!!

 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
555555555555!!!-_-!
I create a "JLabelX",it extended JLabel.
code:
------------------------------------------------------------
public class JLabelX extends JLabel implements java.lang.Cloneable{
public JLabelX(){
super();

this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
System.out.println(((JLabel)e.getComponent()).getToolTipText());
}
});
}
public Object clone(){
try {
return (JLabelX)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
...............
JLabelX jLab = new JLabelX();
jLab.setName("Label1");
jLabel.setToolTipText("Label1"); //

.....................
//clone
JLabelX cloneLab = null;
cloneLab = (JLabelX)jLab.clone();
cloneLab.setName("cloneLabel");
cloneLab.setToolTipText("cloneLabel");
......................
System.out.println(((JLabel)e.getComponent()).getToolTipText());
the all printed "cloneLabel". I reset cloneLab's "width" and "height" property,and the jLab's "width""height" are changed.same to cloneLabel.but their name is not same. I think this problem take a week,but i can't save problem. please!!please!!please!! help me!!!

thank!!!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the API for JLabel (or any other Swing component): http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JLabel.html

Notice that clone is being inherited from Object. In other words, no attempt has been make to override clone to do deeper copying in Swing component types. This is a strong hint that you shouldn't use clone and that overriding clone isn't likely to do what you hope it will -- it's too likely that object components will end up being shared. You've discovered that's the case with the component's tool tip.

What to do? Why do you want to clone? I've never seen a need to clone Swing components, so the next step for you is to explain why you think you need to do this. I suspect there is an easier solution.
 
Kamesh Loganathan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It is true that in general there is no need to do cloning in swings but there might be wierd cases for instance in trees we might want to do a copy paste action. In such cases we have to clone the user object associated with the node or create a new one. Since the user object might be a complex data structure written by us, we might want to define a clone method in it to save time.

There are a set of rules that have to be followed while performing cloning. We shouldnt jusr over ride the method clone and create a new object inside it and set the values in the old one to the new one. Please refer to the java spec before overriding this method.

All the best!!!
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we might want to do a copy paste action
--------------------------------
yes,i want. the new component keep same value with old component.when I changing the new component's value ,and the old not changed. its copy paste action.
do you have any method?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you'll have to make your own descendant of the component and have it implement Clonable. You'll also need to override the clone() method to make a deep copy of the component.

Or just find another way to implement your copy/paste action.
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in class,its implemented cloneable and deep clone.but their "getToolTipText()" method return same.and "Width","height" too.
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not any way. I created a new component and set it's properties same to old one.
thanks eveyone.thanks..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!