javaswingtablemodel

Saving JTable Values in an ArrayList


I'm currently programming Yahtzee and I'm in the process of changing the player names. For this I use an extra form in which there is a JTable and a JButton.

Depending on the variable number of players in the table, an entry will be created where you can change the name. Only the second column should be editable - this also works.

However, I have no idea how to make it possible to add the contents from the second column to an ArrayList at the push of a button so that I can continue to use them.

Here is the implementation of my custom TableModel

public class SpielerBenennungTableModel implements TableModel {

    private int spielerAnzahl = 0;    
    private ArrayList<TableModelListener> Listener = new ArrayList<TableModelListener>();

    public SpielerBenennungTableModel(int spielerAnzahl){        
        this.spielerAnzahl = spielerAnzahl;
    }

    @Override
    public int getRowCount() {
        return spielerAnzahl;
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int arg0) {
        if(arg0 == 0){
            return "Spieler";
        }else{
            return "Eigener Name";
        }
    }

    @Override
    public Class<?> getColumnClass(int arg0) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int arg0, int arg1) {
        if(arg1 == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Object getValueAt(int arg0, int arg1) {
        if(arg1 == 0){
            return "Spieler: " + (arg0+1);
        }else{
            return rowData[arg0][arg1];    
        }
    }


    @Override
    public void setValueAt(Object arg0, int arg1, int arg2) {         
    }

    @Override
    public void addTableModelListener(TableModelListener arg0) {
        Listener.add(arg0);
    }

    @Override
    public void removeTableModelListener(TableModelListener arg0) {
         Listener.remove(arg0);
    }

}

Solution

  • Try this out:

    In your SpielerBenennungTableModel you need an object to hold the data you display. We will be using a List<String[]> that should look like this (I named it rows):

    [
      ["Spieler: 1", "Bob"],
      ["Spieler: 2", "John"]
    ]
    
    

    every time you change a value, the setValueAt method is called and will update the List<String[]> with the correct value.

    Then when you use the getValueAt method, it will read from this same List<String[]>

    class SpielerBenennungTableModel implements TableModel {
    
        private int spielerAnzahl = 0;
        private ArrayList<TableModelListener> Listener = new ArrayList<TableModelListener>();
    
        // this will hold the data for every rows
        private List<String[]> rows;
    
        public SpielerBenennungTableModel(int spielerAnzahl){
            this.spielerAnzahl = spielerAnzahl;
    
            // initialize the list so that all rows are
            // ["Spieler: n", ""]
            rows = new ArrayList<>();
            for(int i = 0; i<spielerAnzahl; i++) {
                this.rows.add(new String[] { "Spieler: " + (i+1), "" });
            }
        }
    
        @Override
        public int getRowCount() {
            return spielerAnzahl;
        }
    
        @Override
        public int getColumnCount() {
            return 2;
        }
    
        @Override
        public String getColumnName(int col) {
            return col == 0 ? "Spieler" : "Eigener Name";
        }
    
        @Override
        public Class<?> getColumnClass(int col) {
            return String.class;
        }
    
        @Override
        public boolean isCellEditable(int row, int col) {
            return col == 1;
        }
    
        @Override
        public Object getValueAt(int row, int col) {
            return rows.get(row)[col];
        }
    
        @Override
        public void setValueAt(Object value, int row, int col) {
            rows.get(row)[col] = value.toString();
        }
    
        @Override
        public void addTableModelListener(TableModelListener arg0) {
            Listener.add(arg0);
        }
    
        @Override
        public void removeTableModelListener(TableModelListener arg0) {
            Listener.remove(arg0);
        }
    }