Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are my add methods for arraylists conflicting, how can I edit my custom add method
    text
    copied!<p>I've figured out why my arraylist was always returning a size of 0 but I can't fix the problem. I have a custom model with an "addHuman(Human h) method that is intended to add to an array. Only problem is that it doesn't. Now, if I were to use the regular method, say model.add(index, object o) it would actually work and increment the size of my arraylist but doesn't display on my jtable. My question is how can I make this work for my custom addHuman method? Any help much appreciated!</p> <p>and the following is the main class that uses that method. When I click on the button to addIndividual, it's supposed to add the human to my HumanListModel:</p> <pre><code>addIndividual.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Human temp; try { temp = new Human(); modelx.addHuman(indexPoint, temp); ///the addHuman method does display on jtable but doesn't increment arraylist, meaning that the size is always 0 which creates many problems///// //modelx.add(indexPoint, temp); does indeed increment the arraysize but then it doesn't display the values on the jtable//// indexPoint++; System.out.println(modelx.size()); } catch (FileNotFoundException e) { e.printStackTrace(); } newbiex.revalidate(); ////is the jtable//// } }); </code></pre> <p>here is my custom HumanListModel:</p> <pre><code>public class HumanListModel extends DefaultListModel implements TableModel { private ArrayList&lt;Human&gt; data; public HumanListModel() { super(); data = new ArrayList&lt;Human&gt;(); } public void addHuman(int k, Human h) { data.add(k, h); fireIntervalAdded(this, data.size(), data.size()); } public Human getHuman(int o) { return data.get(o); } 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 &lt; 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) { } @Override public boolean isCellEditable(int arg0, int arg1) { return false; } public void removeTableModelListener(TableModelListener arg0) { } public void setValueAt(Object arg0, int arg1, int arg2) { } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload