0

I'm having difficulty understanding how to create a copy method for a custom modellist. My goal is this: I have an arraylist that displays how many objects are in it. I created another list for the purpose of serving a temporary action. Basically, I have two tables. The first table are all my available items. My second table will contain the objects that I add to it for that temporary action, in this case, I'm simulating a match. After the simulation is complete, I can either add or remove which ever object I would like. What I don't have as a result of having a custom model list are the methods for removing an index from that temporary list. I have added the following code and thank you for helping :)

addPlayerMatch.addActionListener(new ActionListener()
{

    public void actionPerformed(ActionEvent event)
    {

        ArrayList<Human> testDM = new ArrayList<Human>();


        try {

            testDM.addHuman((//selected Index from first list////.getModel(//this method don't work//));


            } 
        catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }   


            }

});

Here is my default model:

public class HumanListModel extends DefaultListModel implements TableModel{

   private ArrayList<Human> data;

public HumanListModel()
{
    super();
    data = new ArrayList<Human>();
}

public void addHuman(Human h)
{
   // add new human to the model
   data.add(h);

}

public void removeHuman(Human h)
{
    data.remove(h);
}


public int getColumnCount()
{
    // the number of columns you want to display
    return 1;
}


public int getRowCount()
{
    return data.size();
}


public Object getValueAt(int row, int col)
{
    return (row < data.size()) ? data.get(row) : null;
}


public String getColumnName(int col)
{
    return "Human";

}

public Class getColumnClass(int col)
{
    return Human.class;
}

public void addTableModelListener(TableModelListener arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean isCellEditable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void removeTableModelListener(TableModelListener arg0) {
    // TODO Auto-generated method stub

}

@Override
public void setValueAt(Object arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Iron
  • 49
  • 9
  • `testDM.addHuman(modelx.getModel(//*this method don't work*//));` Of course not, `modelx` isn't declared yet! –  Jun 02 '13 at 23:30
  • OH sorry that was just like a test I was running – Iron Jun 02 '13 at 23:32
  • By copy do you mean make a duplicate of it? –  Jun 02 '13 at 23:33
  • Yes, make a temporary duplicate is exactly right :) and also to remove from the list as I've been battling this one for days now :) – Iron Jun 02 '13 at 23:35

1 Answers1

1

Add a copy constructor to your class Human,

private static class Human {
    public Human() {}
    public Human(Human human) {}
}

and use it to create a clone of the List<Human> backing your TableModel

List<Human> list = new ArrayList<Human>();
Human human = new Human();
list.add(human);
List<Human> clone = new ArrayList<Human>();
clone.add(new Human(human));

To remove a row, just give your table model a public remove() method that forwards the request to the List implementation and fires the appropriate TableModelEvent.

I'm a little disturbed that you appear to be implementing the TableModel interface. Instead, extend AbstractTableModel which has the event machinery in place, as outlined here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you! I will definitely try and resolve it, will get try and get back to you to see if i got it working :) – Iron Jun 03 '13 at 00:57