0

Is there a way to insert always an empty row if the lowest row gets edited?
I always want to have a free row on the bottom and if someone edits the lowest row, there should be automatically inserted a new one.

I use that AbstractTableModel:

public class NativeTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private ArrayList<Spieler> myObjects = null;
private String[] columnNames = { "Vorname", "Nachname", "Trikotnummer" };

public NativeTableModel() {
    myObjects = new ArrayList<Spieler>(JNIResultSet.getMeineSpieler());
}

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

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

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

@Override
public boolean isCellEditable(int row, int col) {
    boolean retValue = false;
    if (col >= 1) {
        retValue = true;
    }
    return retValue;
}

@Override
public void setValueAt(Object value, int row, int col) {
    switch (col) {
    case 0:
        if (value instanceof String) {
            //
        }
        break;
    case 1:
        if (value instanceof Integer) {
            //
        }
        break;
    }
    fireTableCellUpdated(row, col);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Object value = "";
    Spieler s = this.myObjects.get(rowIndex);
    switch (columnIndex) {
    case 0:
        value = s.getVorname();
        break;
    case 1:
        value = s.getNachname();
        break;
    case 2:
        value = s.getTrikotnummer();
        break;
    }
    return value;
}

}

How can I check if the lowest row is empty or not and if it's empty insert a new row?

mafioso
  • 1,630
  • 4
  • 25
  • 45
  • First of all, why are you using a rigid 2D array for your model and not an `ArrayList` where RowClass is a class that holds a row's worth of information? – Hovercraft Full Of Eels Oct 12 '16 at 20:59
  • Since you're using a hard-coded array, and since your rowCount is dependent on this array's length, the only way to increase size is to create a new and larger array, basically to create your own ArrayList functionality, which is why I'm asking the question above. – Hovercraft Full Of Eels Oct 12 '16 at 21:03
  • I haven't used the `ArrayList` before and that example is working – mafioso Oct 12 '16 at 21:04
  • OK, good luck then. – Hovercraft Full Of Eels Oct 12 '16 at 21:04
  • ... but on the other hand, if you want code that will solve this new problem,... then your example simply won't work. – Hovercraft Full Of Eels Oct 12 '16 at 21:17
  • If you still need help, consider creating and posting a [mcve] or [sscce](http://sscce.org). The links will explain all. – Hovercraft Full Of Eels Oct 12 '16 at 21:17
  • Look, I've updated the question. How should my setValueAt method look like because I've never done that before? – mafioso Oct 12 '16 at 21:23
  • I've answered as best I can, but if you need more detail, then I ask that you first supply us with more detail as per my links above. – Hovercraft Full Of Eels Oct 12 '16 at 21:23
  • 2
    `How should my setValueAt method look like because I've never done that before?` - Check out [Row Table Model](https://tips4java.wordpress.com/2008/11/21/row-table-model/) for a simple breakdown on how to create a custom model using an ArrayList to hold the data. You can then create your own (which would be good practice) or use the provided generic `RowTableModel`. – camickr Oct 13 '16 at 01:26

1 Answers1

2

Suggestions:

  • Change your table model to use an ArrayList as its data nucleus, not an inflexible 2D array.
  • The generic type of the ArrayList should be a class that holds the data of a single row of your JTable.
  • It should have an empty default state that signifies a row devoid of data.
  • When data is changed in your table model's methods, check the last row of the model to see if the object held by the list here is default or holds data.
  • If it holds data, add a new object to the list and call the appropriate fireXxx notification method.
  • In setValue, you can check the rowCount, and you can compare it with the row parameter passed into the method. If they match, then add a new row. But you have to be careful of circular references if you do this, since adding the new row will likely call setValue as well, and so you'll have to have logic present to prevent a stackoverflow. Again, we can help better if we can work with your SSCCE.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • How should the `setValueAt` method look like? – mafioso Oct 12 '16 at 21:26
  • Ok, but I still don't know how to change the value in the arraylist – mafioso Oct 12 '16 at 21:33
  • @mafioso: If you don't know how to get an object from an ArrayList yet or how to modify objects by using getters and setters, then you will want to review the appropriate tutorials for this, tutorials that are easily found online. This site is great for answering specific code problems, but we don't function well as a tutorial for the most basic of Java concepts. – Hovercraft Full Of Eels Oct 12 '16 at 21:44
  • Yeah, I've fixed that.. the only issue still remaining is inserting an empty row if the lowest row isn't empty – mafioso Oct 12 '16 at 21:59