Java 更改数据后刷新 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19952870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:48:17  来源:igfitidea点击:

Refreshing JTable after changing data

javaswingnetbeansjtable

提问by Tilen Kav?i?

I want to refresh and display my JTableafter a user pressed a button. Before that the button generates an Object[][]in which the filtered data from the table is held. The filtered data is different only in the number of rows.

我想JTable在用户按下按钮后刷新并显示我的。在此之前,该按钮会生成一个Object[][]保存表格中过滤数据的文件。过滤后的数据仅在行数上有所不同。

I'm using the netbeans UI creator and the only way I could populate the table with data is by defining the abstractTableModel. Here is the code of my abstractTableModel:

我正在使用 netbeans UI 创建器,我可以用数据填充表的唯一方法是定义 abstractTableModel。这是我的 abstractTableModel 的代码:

class myTable extends AbstractTableModel{
    private String[] stolpci = {"Kategorija","Podkategorija","Opis","Cena","Datum","Boni"};
    private Object[][] data = PregledovalnikGUI.vrniTabelo(); /*PregledovalnikGUI.vrniTabelo() returns a value in form of Object[][] in which the data is held*/
    public int getColumnCount() {
        return stolpci.length;
    }
    public int getRowCount() {
        return vrstice.length;
    }
    public String getColumnName(int col) {
        return stolpci[col];
    }
    public Object getValueAt(int row, int col) {
        return vrstice[row][col];
    }
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    public void setValueAt(Object value, int row, int col) {
        vrstice[row][col] = value;
        fireTableCellUpdated(row, col);
    }

The jTable is set like:

jTable 设置如下:

Tabela.setModel(new myTable());

Which methods do I need to define to get the table to refresh at runtime?

我需要定义哪些方法才能在运行时刷新表?

采纳答案by Sionnach733

inside myTableyou could have a method called refresh()something like this

在里面myTable你可以有一个叫做refresh()这样的方法

public void refresh(Object[][] objects){
    //make the changes to the table, then call fireTableChanged
    fireTableChanged(null);
}

Then in the button listener, call the above method:

然后在按钮监听器中,调用上面的方法:

Tablea.refresh(objects);//objects stores your filtered data

回答by MadProgrammer

If you create a new TableModel, then nothing, the table will automatically update itself.

如果你创建一个新的TableModel,那么什么都没有,表会自动更新自己。

If the underlying data of the model is changed, then from within the model (seen as you extending from AbstractTableModl), you could call fireTableDataChanged, which lets the table know that the contents of the table have changed and it should redraw itself.

如果模型的基础数据发生更改,则从模型内部(从 扩展看AbstractTableModl),您可以调用fireTableDataChanged,它让表知道表的内容已更改,它应该重新绘制自身。

This may require that the model either have a refresh method of its own or that it has the capability to listen to changes from the data it is modelling

这可能要求模型要么有自己的刷新方法,要么有能力侦听正在建模的数据的变化