Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nicolas Viollin
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {}
}
}
...
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic