单击按钮更新JTable [英] Updating JTable on button click

查看:165
本文介绍了单击按钮更新JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JTable 找到自定义表模型此处。我已使用该帖子中提供的建议更新了我的代码并遇到了新问题。我对我的代码所做的更改是将 ArrayList< CompletedPlayer> 注入我的 JTable 以避免线程出现问题。执行此操作后,通过按下按钮更新我的表的代码已停止工作。

I am working with a JTable with a Custom Table Model found here. I have updated my code with the suggestions provided in that post and have run into a new problem. The change I have made to my code was to inject an ArrayList<CompletedPlayer> into my JTable to avoid issues with threads. After doing that, the code to update my table by pressing a button has stopped working.

用于初始化 JTable的代码如下:

TableModel model = new PlayerTableModel(FileHandler.getCompletedPlayers());
JTable table = new JTable(model);

我用来更新 JTable的代码如下:

JButton btnRefreshAllPlayers = new JButton("Refresh");

btnRefreshAllPlayers.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        PlayerTableModel model = (PlayerTableModel) table.getModel();
        model.fireTableDataChanged();

    }

});

我也尝试过使用 repaint()但这也不起作用。截至目前,获取 JTable 更新的唯一方法是关闭并重新打开该程序。 FileHandler ArrayList 我用于 JTable 随着用户添加更多玩家,尺寸会增加。

I have also tried using repaint() but this does not work either. As of right now, the only way to get the JTable to update is to close and reopen the program. FileHandler has the ArrayList I am using for the JTable which increases in size as the user adds more players.

为什么 fireTableDataChanged()检测到任何变化?

Why doesn't fireTableDataChanged() detect any changes?

推荐答案


我搜索了stackoverflow,有几个人说过要用这种方法。

I have searched on stackoverflow and a couple of people have said to use that method.

不,你不应该在上下文之外调用任何 fireTableXxx 方法 TableModel 本身,人们建议否则是完全错误的,它将来会引起你的问题。从代码的外观来看,没有任何改变。如果您根据上一个问题中提供的答案更新了 TableModel ,则模型中的数据与外部源无关。您需要手动从外部源重新加载数据,创建一个新的 TableModel 并将其应用到表中

No, you should not call any fireTableXxx methods outside of the context of the TableModel itself, people suggesting otherwise are simply wrong and it will cause you issues in the future. From the looks of your code, nothing has changed. If you've updated the TableModel according to the answer provided in your previous question, then there is no relationship with the data in the model to the external source. You need to manually reload the data from the external source, create a new TableModel and apply it to the table

例如......

JButton btnRefreshAllPlayers = new JButton("Refresh");

btnRefreshAllPlayers.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        TableModel model = new PlayerTableModel(FileHandler.getCompletedPlayers());
        table.setModel(model);

    }

});




我还尝试使用更新的ArrayList设置新模型工作但没有保留我之前设置的表格行宽度。

I have also tried setting a new model with the updated ArrayList and it worked but did not keep the table row widths I previously set.

这对表来说是合理的,因为它不知道新模型是否具有与之相同的属性/列旧的,所以它会重置它们。

This is a reasonable thing for the table to do, because it has no idea if the new model has the same properties/columns as the old, so it resets them.

你可以走 ColumnModel ,将列宽保存在列出 Map ,然后再应用模型并重新应用宽度

You could walk the ColumnModel, storing the column widths in a List or Map before you apply the model and reapply the widths


是否有更新JTable的正确方法?

Is there a proper way to update the JTable?

您可以提供 TableModel 使用刷新方法,可以加载数据本身并触发 tableDataChanged 事件

You could provide your TableModel with a refresh method, which could load the data itself and trigger a tableDataChanged event

public class PlayerTableModel extends AbstractTableModel {
    private final List<PlayerSummary.Player> summaries;

    public PlayerTableModel(List<PlayerSummary.Player> summaries) {
        this.summaries = new ArrayList<PlayerSummary.Player>(summaries);
    }
    // Other TabelModel methods...

    public void refresh() {
        summaries = new ArrayList<>(FileHandler.getCompletedPlayers());
        fireTableDataChanged();
    }
}

然后你需要在你的 ActionListener ...

Then you would need to call this method in your ActionListener...

PlayerTableModel model = (PlayerTableModel)table.getMode();
model.refresh();

这篇关于单击按钮更新JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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