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