JTable刷新不显示 [英] JTable refresh are not displayed

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

问题描述

我极力试图说服我的JTable在更改其数据时刷新.数据存储在我正在使用的全局单例TreeSet中.每当TreeSet的数据被更改时,都会触发一个事件来刷新TableModel.出于测试目的,我用一个简单的Timer触发事件代替了它.

I'm desperately trying to convince my JTable to refresh when I change its data. The data are stored in a global singleton, TreeSet, that I'm working with. Whenever the TreeSets data is altered, an event is fired to refresh the TableModel. For test purposes, I replaced this by a simple Timer firing events.

每次触发事件时,都会删除TreeSet中的元素以模拟数据中的更改.事件触发,TableClass接收并按预期方式对其进行处理,但是在刷新时什么也没有发生.每当发生事件时,我都尝试创建一个新的TableModel并将其设置为全局表.对单例TreeSet进行了更改,但JTable没有任何反应.

Each time an event is fired, an element from the TreeSet is removed to simulate changes in the data. The event fires and the TableClass receives it and processes it as expected, but when it comes to refreshing nothing happens. I tried to create a new TableModel each time an event occurs and set it to the global table. The changes to the singleton TreeSet are made but nothing happens to the JTable.

public class TreeTableObj implements ActionListener{
public JTable table ;

public TreeTableObj(){
    MyTableModel myModel = new MyTableModel(getValuesOfTreeSet(), getTreeSetRows());

    table = new JTable( myModel){ //some rendering };

    table.setAutoCreateRowSorter(true);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames;
        private Object[][] data ;


        public MyTableModel(Object[][] data, String[] columnNames){
            this.data = data;
            this.columnNames = columnNames;
            System.out.println("Model created");
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public void setData(Object[][] data){
            this.data = data;
            fireTableDataChanged();
        }


        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }


        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }


        public void setValueAt(Object value, int row, int col) {
            if (true) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (true) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    public void refreshTableModel(){
        FlightsTreeSet.getInstance().remove(FlightsTreeSet.getInstance().first());
        table.setModel(new MyTableModel(getValuesOfTreeSet(), getTreeSetRows()));
        }

    public void actionPerformed(ActionEvent arg0) {
        refreshTableModel();    
    }
}

任何帮助将不胜感激!

我越来越接近核心问题: 我发现JFrame中显示的表包含另一个引用,作为我要更改的那个.我使所有更改可见,并将它们应用于模型.

I am getting closer to the problems Core: I found out that the table which is displayed in the JFrame holds another reference as the one that I change. I made all changes visible and they apply to the Model.

推荐答案

我发现了问题.并留下答案供其他人找到... 意识到显示的JTable和我正在编辑的JTable具有不同的引用(请参见)后,我发现我正在创建JTable的两个不同的对象.一个要显示,另一个在main()中显示为ActionListener.侦听器收到事件并进行了更改,但是另一位则从未注意到发生了任何事情.

I found the problem. And leaving the answer here to be found by others... After realizing that the displayed JTable and the one that I was editing hold different references (see ) I found out that I was creating two different Objects of the JTable. One to be displayed and the other one in main() to become an ActionListener. The Listener received the Events and did the changes but the other one never noticed that anything was happening.

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

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