如何清除一个数组列表,并显示空的JTable? [英] How to clear an array list and display empty JTable?

查看:206
本文介绍了如何清除一个数组列表,并显示空的JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在制作插槽游戏的过程。

我有这样实现的历史记录功能。在我的课,我有以下静态变量
静态的String [] [] = figureHistoryArray新的String [1000] [4];

每次一个用户$ P $在GUI激活一个称为slotSpin()方法psses一旋转按钮。

在这个方法我有

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

在每个插槽的自旋被保存。

在我显示在GUI中的数据

 私人的JTable historyTable;

和在该方法中:

  historyPanel =新JPanel();
    historyPanel.setLayout(新的BorderLayout());
    的getContentPane()加(historyPanel)。    //创建列名
    字符串COLUMNNAMES [] = {插槽1,插槽2,3槽,赢/输};    的String [] [] = dataValues​​ TheBigA.figureHistoryArray;    //创建一个新的实例historyTable
    historyTable =新的JTable(dataValues​​,COLUMNNAMES);    //将historyTable添加到滚动窗格
    this.scrollPanel =新JScrollPane的(historyTable);
    historyPanel.add(this.scrollPanel,BorderLayout.CENTER);
            加(historyPanel);

现在我的问题是每当仍显示用户presses新游戏,从previous游戏的历史。我怎么会去解决这个


解决方案

  

现在我的问题是每当仍显示用户presses新游戏,从previous游戏的历史。我怎么会去解决这个


不要创建任何新的Swing组件。

相反,当你开始一个新的游戏,你有一个空的TableModel装载表:

  table.setModel(新的DefaultTableModel(COLUMNNAMES,0));

编辑:


  

所以我将如何更新我的表模型引用这个新的数组?


这样,那么你做这样的事情:

  historyArray =新的String [1000,4];
table.setModel(新的DefaultTableModel(historyArray,COLUMNNAMES);

不过,你真的不应该这样做。该数据应存储在TableModel。您可以使用数据动态地添加到模型

  model.addRow(...);

当要保存可以通过TableModel的迭代数据来保存数据。

或者,如果你读JTable的API,它建议您使用XMLEn codeR保存数据。看看这个帖子的通用解决方案,您可以使用保存/载入数据:<一href=\"http://stackoverflow.com/questions/26250939/how-to-write-a-jtable-state-with-data-in-xml-file-using-xmlend$c$cr-in-java/30715923#30715923\">How写使用XMLEnd codeR在Java中的XML文件中的数据JTable中状态

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

解决方案

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

这篇关于如何清除一个数组列表,并显示空的JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆