Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jscrollpane don';中表中的一行新数据;卷轴?_Java_Jtable_Jpanel_Jscrollpane - Fatal编程技术网

Java jscrollpane don';中表中的一行新数据;卷轴?

Java jscrollpane don';中表中的一行新数据;卷轴?,java,jtable,jpanel,jscrollpane,Java,Jtable,Jpanel,Jscrollpane,我使用以下代码在表中显示统计信息。每次更改hashmap中的数据时,我都会再次运行以下代码。虽然新添加的行将显示,但它无法滚动(如下所示)。有人知道为什么吗?谢谢 HashMap<String, Commodity> duMap = Light.getLightMap(); String[] column = {"Name", "Type", "Amount", "Default in price", "Default out price", "Last

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

 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();
HashMap duMap=Light.getLightMap();
String[]列={“名称”、“类型”、“金额”、“默认价格”、“默认价格”、“最后价格”、“最后价格”};
字符串[][]行=新字符串[duMap.size()][];
int i=0;
for(字符串:duMap.keySet()){
duLight=duMap.get(字符串);
行[i]=新字符串[]{duLight.getName(),duLight.getModel(),“”+duLight.getAmount(),“”+duLight.getDefaultIP(),“”+duLight.getDefaultOP(),“”+duLight.getLastIP(),“”+duLight.getLastIP(),“”+duLight.getLastOP();
i++;
}
JTable statistics=新的JTable(行、列);
statistics.setEnabled(false);
setFont(新字体(“DIALOG”,2,20));
对于(int j=0;j<7;j++){
statistics.getColumnModel().getColumn(j).setPreferredWidth(120);
}
statistics.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
统计数据:设置行高(40);
statistics.setDefaultRenderer(Object.class,dtc);
//背面是一个静态面板
添加(新的JScrollpane(统计),BorderLayout.CENTER);
back.revalidate();

您可以尝试以下操作,而不是一直创建新的JTable和JScrollPane:

  • 只需使用
    setDataVector(…)
    方法更新现有的DefaultTableModel
  • 或者,创建一个新的DefaultTableModel,然后使用
    setModel(…)
    方法更新JTable

  • 看起来您没有删除旧的滚动窗格/表格,尽管如此,您的方法效率非常低。更好的解决方案是设计一个TableModel,它可以包装您的HashMap,然后提供一个简单的方法(在表模型中)来告诉何时添加了新行,并允许它触发update@MadProgrammer有什么例子吗?我是新手。你能给我看一些例子吗?我是新手,不知道你的意思。建议2,你可以阅读DefaultTableModelAPI。它有一个构造函数,您可以在其中提供列名和数据。创建DefaultTableModel后,使用setModel()方法将其添加到现有JTable中。再次阅读jtableapi以获取有关该方法的信息。无需创建新表或滚动窗格。代码所做的只是更新现有表中的数据。您可以在论坛中搜索使用setModel()方法的示例。谢谢您的建议。我试试看。