0

I am currently in the process of making a slots game.

I have a history feature implemented as such. In my class I have the following static variable static String[][] figureHistoryArray = new String [1000][4];

Everytime a user presses a "spin" button in the GUI a method called slotSpin() is activated.

Within this method I have got

figureHistoryArray[turnCounter][0]= rollOne.getFigureName();
figureHistoryArray[turnCounter][1]= rollTwo.getFigureName();
figureHistoryArray[turnCounter][2]= rollThree.getFigureName();

In which each slot spin gets saved.

After that I display the data in the GUI

    private JTable historyTable;

And in the method:

    historyPanel = new JPanel();
    historyPanel.setLayout( new BorderLayout() );
    getContentPane().add(historyPanel );

    // Create columns names
    String columnNames[] = { "Slot 1", "Slot 2", "Slot 3", "Win/Loss" };

    String[][] dataValues= TheBigA.figureHistoryArray;

    // Create a new historyTable instance
    historyTable = new JTable( dataValues, columnNames );

    // Add the historyTable to a scrolling pane
    this.scrollPanel = new JScrollPane( historyTable );
    historyPanel.add(this.scrollPanel, BorderLayout.CENTER );
            add(historyPanel); 

Now my issue is whenever the user presses "New Game" the history from the previous game is still being displayed. How would I go about fixing this

1 Answers1

4

Now my issue is whenever the user presses "New Game" the history from the previous game is still being displayed. How would I go about fixing this

Don't create any new Swing components.

Instead, when you start a new game you load the table with an empty TableModel:

table.setModel( new DefaultTableModel(columnNames, 0) );

Edit:

so how would I update my table model to refer to this new array?

So then you do something like:

historyArray = new String[1000, 4];
table.setModel( new DefaultTableModel(historyArray, columnNames);

However, you should NOT really do this. The data should be stored in the TableModel. You can dynamically add data to the model using

model.addRow(...);

When you want to save the data you can iterate through the TableModel to save the data.

Or if you read the JTable API, it suggests you use an XMLEncoder to save data. Check out this posting for a generic solution that you can use to save/load the data: How to write a JTable state with data in xml file using XMLEndcoder in java

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Then it won't show the new game history anymore, it work resetting the table, but it remains empty afterwards – J. Dohne Oct 16 '15 at 23:23
  • You said you don't want the history to be displayed. – camickr Oct 16 '15 at 23:24
  • Well no, I don't want the history from the old game being displayed. Only the new game. – J. Dohne Oct 16 '15 at 23:25
  • I don't understand your problem. The new game won't have any history yet because the game just started. The point is your start with an empty TableModel and then you add data to the model when the game starts. Only you know how this works in your application. – camickr Oct 16 '15 at 23:26
  • The issue is that the Array historyArray itself remains filled. And that is always the data source that is used. – J. Dohne Oct 16 '15 at 23:32
  • by doing figureHistoryArray = new String [1000][4]; ?? – J. Dohne Oct 16 '15 at 23:34
  • So then clear it. Something like:: historyArray = new String[100, 100]; The question is why do you have a historyArray? You should be adding data directly to the TableModel maybe by using the addRow(...) method. – camickr Oct 16 '15 at 23:35
  • I think the issue then becomes that there is a new memory reference to the new array, so how would I update my table model to refer to this new array? – J. Dohne Oct 16 '15 at 23:35
  • I have a history array because I also have a save game feature. I save the array as an object. – J. Dohne Oct 16 '15 at 23:36
  • This is why you don't keep the data in two places. Keep the data in the model. Then your "Save Game Feature", saves the data in the TableModel. Using your current approach you hardcoded an maximum of 1000 entries for your game. There is no need for limitations when you design the code properly as suggested above. – camickr Oct 16 '15 at 23:37
  • Hmm interesting I did not realise that TableModel stores the actual data, thank you I will give it a go now – J. Dohne Oct 16 '15 at 23:41
  • 1
    @J.Dohne, `I did not realise that TableModel stores the actual data,` - The is the purpose of the "model" in a Model-View-Controller design, which Swing (sort of) follows. In Swing the View-Controller logic is combined into the View. See my edit for another way to save the data in the model. – camickr Oct 16 '15 at 23:47
  • @J.Dohne, don't forget to "accept" the answer when it solves your problem (by clicking on the check mark) so others know the problem has been solved. – camickr Oct 17 '15 at 15:19