jscrollpane表格中的新一行数据不滚动?

时间:2014-06-21 02:33:54

标签: java jtable jpanel jscrollpane

我使用以下代码显示表中的统计信息。每次更改hashmap中的数据时,我都会再次运行以下代码。虽然新添加的行将显示,但它无法滚动(如此enter image description here)。所以谁知道为什么?感谢。

 HashMap<String, Commodity> duMap = Light.getLightMap();

            String[] column = {"Name", "Type", "Amount", "Default in price", "Default out price", "Last in price", "Last out price"};
            String[][] row = new String[duMap.size()][];
            int i = 0;
            for (String string : duMap.keySet()) {
                duLight = duMap.get(string);
                row[i] = new String[]{duLight.getName(), duLight.getModel(), ""+duLight.getAmount(), ""+duLight.getDefaultIP(), ""+duLight.getDefaultOP(), ""+duLight.getLastIP(), ""+duLight.getLastOP()};
                i ++;
            }
            JTable statistics = new JTable(row, column);
            statistics.setEnabled(false);
            statistics.setFont(new Font("DIALOG", 2, 20));
            for (int j = 0; j < 7; j++) {
                statistics.getColumnModel().getColumn(j).setPreferredWidth(120);
            }
            statistics.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            statistics.setRowHeight(40);

            statistics.setDefaultRenderer(Object.class, dtc);

            //back is a static panel
            back.add(new JScrollpane(statistics), BorderLayout.CENTER);
            back.revalidate();

1 个答案:

答案 0 :(得分:0)

而不是一直尝试创建新的JTable和JScrollPane:

  1. 只需使用setDataVector(...)方法更新现有的DefaultTableModel。
  2. 或者,您创建一个新的DefaultTableModel,然后使用setModel(...)方法更新JTable。
相关问题