I tried so far too much and was reading lots of things. Still couldn't find any working solution on my case. So, here is the deal. During the game, players money are changing. What I am trying to achieve is to display money instantly from the JTable
, so the player can always follow his money by simply looking at the table. But, as you can assume, I failed at changing the money when it is changed. It just stays at the amount of how it started.
What I got so far is a well-built JTable
which shows the initial money of player at beginning and a custom model for my JTable
.
Since I believe the solution relies on the model and setValuesAt(Object value, int row, int column)
, I was trying to figure a way out there. What I got there is a method called refresh. To be more specific, let me show you some fraction of my code.
So here is my rowData array in the constructor, which I'm using for displaying the initial money at the beginning of the game:
rowData = new Object[][]{
{GameFlow.getPlayer1().getName(), "Pink", GameFlow.getPlayer1().getMoney()},
{GameFlow.getPlayer2().getName(), "Blue", GameFlow.getPlayer2().getMoney()},
{GameFlow.getPlayer3().getName(), "Green", GameFlow.getPlayer3().getMoney()},
{GameFlow.getPlayer4().getName(), "Red", GameFlow.getPlayer4().getMoney()},
};
By saying that, I also need to show you these two following functions, setValueAt and refresh(refresh is a method I just wrote for simplicity for updating changes):
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
public void refresh(){
for (int i = 0; i < 4; i++) {
setValueAt(GameFlow.getPlayer1().getMoney(), i, 2);
}
}
Here, in the refresh, I am only trying to change the second (2nd) row because the first two are static and never changes. Also, the number 4 in the for loop is the number of players, so that 4 holds the number of rows.
You can find at below the whole my table model code:
import javax.swing.table.AbstractTableModel;
public class MonopolyTableModel extends AbstractTableModel {
private String[] columnNames = {
"Name",
"Color",
"Money",
};
private Object[][] rowData;
public MonopolyTableModel() {
rowData = new Object[][]{
{GameFlow.getPlayer1().getName(), "Pink", GameFlow.getPlayer1().getMoney()},
{GameFlow.getPlayer2().getName(), "Blue", GameFlow.getPlayer2().getMoney()},
{GameFlow.getPlayer3().getName(), "Green", GameFlow.getPlayer3().getMoney()},
{GameFlow.getPlayer4().getName(), "Red", GameFlow.getPlayer4().getMoney()},
};
}
@Override
public int getRowCount() {
return rowData.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowData[rowIndex][columnIndex];
}
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
public void refresh(){
for (int i = 0; i < 4; i++) {
setValueAt(GameFlow.getPlayer1().getMoney(), i, 2);
}
}
}
And that is how I use in the Gui class:
MonopolyTableModel monoModel = (MonopolyTableModel) dataTable.getModel();
monoModel.refresh();
Hope those are enough to show, if needed I can show more but since this is a school project I am avoiding to show much.