0

I have a class with several member variables, one of which is a DefaultTableModel. My constructor for the class uses a MySQL ResultSet to populate the DefaultTableModel. I would like to generate an exact copy of the DefaultTableModel and store it as another member variable within my class, so I can edit a copy of the variable within my class while maintaining a copy of the original variable.

Below is an outline of my class.

public class MyClass() {

    int i;
    Boolean b;
    DefaultTableModel model;
    DefaultTableModel model2;

    MyClass(ResultSet myRS) {
        //code to initialize i,b,model;
        //code to clone model and save as model2;
        //code to modify model2;
    }

    public DefaultTableModel getModels(String s) {
        //code that returns model or model2;
    }

}

I have been doing some reading but haven't figured out how to make an exact copy of a variable within a class. I know I can't just do model2 = model; since this just copies the references of the variable. I made this mistake and found out that editing model2 would also edit model. Everything I have read about clone makes it seem that it is just used to make a new and identical instance of an entire class. Any advice on how to clone a single variable within a class would be greatly appreciated.

Riggster
  • 107
  • 3
  • 15

2 Answers2

1

use clone() method thats create exact copy of object what you want to clone. To allow cloning your class must implement interface Cloneable. Then you must override clone method from interface which you implements.

Little example:

public class TableClass implements Cloneable {


    private DefaultTableModel model;


    @Override
    public Object clone() {
        DefaultTableModel cloned = null;
        try {
            cloned = (DefaultTableModel) super.clone();
        }
        catch (CloneNotSupportedException ex) {
            Logger.getLogger(TableClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cloned;
    }

Because of clone() method returns instance of Object, you must cast to type of your object: cloned = (DefaultTableModel) super.clone()

So this is how to work clone(). Now you can implement your own tableMode.

Basic example:

public class YourTableModel extends AbstractTableModel implements Cloneable {


    private static final String[] columnNames = {"URL", "Progress", "Size", "Status"};
    private static final Class[] columnClasses = {String.class, String.class, String.class, String.class};

    private List<T> yourData;

    @Override
        public Object clone() {
            YourTableModel cloned = null;
            try {
                cloned = (YourTableModel) super.clone();
            }
            catch (CloneNotSupportedException ex) {
                Logger.getLogger(TableClass.class.getName()).log(Level.SEVERE, null, ex);
            }
            return cloned;
        }

    @Override
    public Object getValueAt(int row, int col) {
        switch (col) {
            case 0:
                return yourData.getName();
            case 1:
                return yourData.getAge();
            case 2:
                return yourData.getAddress();
            case 3:
                return yourData.getSomethingElse();
        }
        return "";
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return downloadList.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Class getClassName(int col) {
        return columnClasses[col];
    }
}

This way your implement your own model also clone() method and then you can cloning this objects. But there is also other approaches like wrote @vizier

Hope it helps!

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • I really appreciate your help. I am having a hard time understanding exactly what you are saying, though. Would you mind inserting your code where it should be in the sample class I put in my original question? Thanks. – Riggster May 18 '12 at 16:33
  • You want to create copy of DefaultTableModel, in the way of reference like you wrote (model2 = model) this is only one object but you want to create two exact copies of one object and then you can do whatever want to do. this is sample how to work clone() method. – Simon Dorociak May 18 '12 at 17:09
0

Normally if it was your own TableModel subclass rather than DefaultTableModel you could achive this by making your TableModel implement Clonable and get a copy by calling the clone() method.

As you are working with DefaultTableModel instances, you have to go the other way around and do this:

Instead of creating a single Object[][] or Vector instance for passing data to the constructor of DefaultTableModel, create two copies of Object[][] or Vector. With these you can create both DefaultTableModel instances with the same content.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • Thanks for the help. I was hoping to do something a little more elegant using clone, but that did the job. – Riggster May 18 '12 at 17:20