1

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);
    }

}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
iTz_ElKay
  • 41
  • 7
  • 1
    Are you using Swing? Do you have an action listener for your button? Do you know how to read a value from a text box? – Bentaye Nov 13 '19 at 10:53
  • Yeah, I´m using a JTable. I know all of these things. The only thing I don´t know is how to get the text entries out of the table to safe them in an ArrayList. – iTz_ElKay Nov 13 '19 at 10:56
  • Use the table's [`getValueAt(row,column)`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#getValueAt-int-int-) method. – Kevin Anderson Nov 13 '19 at 11:03
  • This is where I'm struggeling. How can I create a loop which safes all column values in an ArrayList? – iTz_ElKay Nov 13 '19 at 11:07
  • Use table's [`getRowCount()`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#getRowCount--) method to know how many rows to loop over. – Kevin Anderson Nov 13 '19 at 11:12
  • When is the content in the row available? After it is typed? I don't update the Table, I just add the Strings into the rows – iTz_ElKay Nov 13 '19 at 11:16
  • getValueAt will return whatever is in the row/col at the time it is called. So if you can see the name in the field, then getValueAt will return it. – Bentaye Nov 13 '19 at 11:26
  • I will try it - thank you! – iTz_ElKay Nov 13 '19 at 11:27
  • Something like that. What should the GetValueAt method in the TableModel look like? I mean, the first column is fixed, but what do I return else? public Object getValueAt(int arg0, int arg1) { if(arg1 == 0){ return "Spieler: " + (arg0+1); }else{ return this.getValueAt(i, 1); } } – iTz_ElKay Nov 13 '19 at 11:45

1 Answers1

2

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);
    }
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45