This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Copying Object (such as Button, Label, and personal Objects) ?
Nicolas Viollin
Greenhorn
Joined: Apr 13, 2001
Posts: 26
posted
0
In order to increase Object creation, i'd like to know if it is possible to make copy of objects. I've tried with the clone() method, but it doesn't work well... thnx for any help !
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
You can only clone things that implement the Clonable Interface. Button and Label do not do that. For your personal objects you could create your own createCopy() method that creates a new instance and sets all the field values to match the current instance. Note that this is not exactly "cloning" because in cloning you are not supposed to call the constructor of the instance, just duplicate the instance.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Nicolas Viollin
Greenhorn
Joined: Apr 13, 2001
Posts: 26
posted
0
thnx... now i need a way to improve Displaying of Objects on a Frame or Panel. Because if i use this code : import java.util.*; import java.awt.*; public class Start extends Frame{ int max = 9999; MyButton b = new MyButton(); public Start(){ SpyTimer st = new SpyTimer(); setSize(640,480); Vector v = null; st.start(); v = createClone(); st.printTime("fin de creation des " + String.valueOf(max) + " bouttons"); for(int i=0;i<max;i++)> add((MyButton)v.get(i)); setLayout(new FlowLayout()); setVisible(true); st.printFinal(); } public Vector createNew(){ Vector v1 = new Vector(); for(int i=0;i<max;i++)> v1.addElement(new MyButton()); return v1; } public Vector createClone(){ Vector v = new Vector(); for(int i=0;i<max;i++)> v.addElement((MyButton)b.clone()); return v; } public static void main(String args[]){ new Start(); }