File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Copying Object (such as Button, Label, and personal Objects) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Copying Object (such as Button, Label, and personal Objects) ?" Watch "Copying Object (such as Button, Label, and personal Objects) ?" New topic
Author

Copying Object (such as Button, Label, and personal Objects) ?

Nicolas Viollin
Greenhorn

Joined: Apr 13, 2001
Posts: 26
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
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
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();
}

private class MyButton extends Button implements Cloneable{
public Object clone(){
Object bTemp = null;
try{
bTemp = super.clone();
}
catch(Exception e){System.out.println("Erreur");}
return bTemp;
}
}
}

it take a huge time to display all the objects....

re thnx for any help ! ;-)
Nicolas Viollin
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Ouups !! here is the SpyTimer class :
public class SpyTimer {
private long startTime, endTime;
private final int MAX = 1000;
private String[] msg = new String[MAX];
private int idxMsg = 0;
public void start() {
startTime = System.currentTimeMillis();
idxMsg = 0;
}
public void printTime(String s) {
endTime = System.currentTimeMillis();
if (idxMsg < MAX)
msg[idxMsg++] = s + " @ " + Long.toString(endTime - startTime);
}
public void printFinal() {
try {
for (int j = 0; j < idxMsg; j++) {
System.out.println(msg[j]);
}
}
catch (Exception e) {}
}
}
...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copying Object (such as Button, Label, and personal Objects) ?
 
Similar Threads
diagrams and objects - study sheet
object
Another garbage collection question
Design Pattern
Method Resolution