简体   繁体   English

更新JTable的内容

[英]Updating the content of a JTable

I have a JTable which displays an Object[][] of data. 我有一个JTable,它显示数据的Object [] []。 There is also a form on the same screen that lets the user add an item to the object to the list. 在同一屏幕上还有一个表单,允许用户将项目添加到列表中的对象。 Although for the life of me I can not get the list displayed in the table to update when the user presses the "Add Item" button. 尽管我一生都无法在用户按下“添加项目”按钮时更新表中显示的列表。

(it gets appended to the array fine, and I can print it onto the screen, just can't get the table to change.) (它可以很好地附加到数组中,并且我可以将其打印到屏幕上,只是无法更改表。)

Here is crating of the table 这是桌子的板条箱

    Object[] tableHeadings = {"Item Name","Qty/Weight","Price"};
    ShoppingApplication shoppingApplication = new ShoppingApplication();
    Object[][] tableData = shoppingApplication.generatePurchaseTableData();
    final JTable tblBill = new JTable(tableData, tableHeadings);

Here is the table data being generated: 这是正在生成的表数据:

    /**
     * Generates data in the correct format to go into the table 
     * from the purchase list
     * @return 
     */
     public Object[][] generatePurchaseTableData(){ 

     Object[][] results = new Object[listOfPurchases.size()][2];
     for (int i = 0; i<listOfPurchases.size(); i++){
          Purchase purchase = listOfPurchases.get(i);
          Object[] innerObject = 
                   {purchase.getProductName(),
                    purchase.getPricePerUnit(),
                    purchase.getTotalPrice()};
                 results[i] = innerObject;
         }
     System.out.println(results.length);
     return results;
         }
     }

Here's the action listener 这是动作监听器

/* Add Action Listeners */
cmdAddItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){

        AddPurchase addPurchase = new AddPurchase(
           (radWeight.isSelected()) ? 'w' :(radQty.isSelected()) ? 'q' : 'u', 
               txtNameOfItem.getText(), 
               txtAmount.getText(), 
               txtUnits.getText(), 
               txtPricePerUnit.getText());

         ShoppingApplication sa = new ShoppingApplication();
         Object[][] newData = sa.generatePurchaseTableData();
         //TODO make tblBill updata it's contents
                }
 }); 

From the code above, it does look like I haven't made much effort, but I've actually been trying to get this to work for hours now, tried using different methods, data structures and been getting no where to went right back to the start, and that is what the above code it. 从上面的代码看来,我似乎并没有花太多力气,但实际上我已经尝试将其工作数小时,尝试使用不同的方法,数据结构,并且一直无处可寻。开始,这就是上面的代码。

I've Googled this a bit, and can't seem to find anything, can't think why no one else has seemed to get stuck on this. 我已经在Google上搜索了一下,似乎找不到任何东西,无法思考为什么没有其他人似乎对此卡住了。 (maybe I'm just being thick) but hopefully this post will help others too. (也许我只是很胖),但希望这篇文章也能对其他人有所帮助。

Thanks in advance :) 提前致谢 :)

The JTable has a data model ( TableModel ), which either holds the values, or is an adapter to the actual data. JTable有一个数据模型( TableModel ),该模型可以保存值,或者是实际数据的适配器。

Data in a table can be update by either changing the data in the table model, or by setting a new table model. 可以通过更改表模型中的数据或设置新的表模型来更新表中的数据。

In your case, you could use the javax.swing.table.DefaultTableModel : 在您的情况下,可以使用javax.swing.table.DefaultTableModel

TableModel tableModel=new DefaultTableModel(newData, tableHeadings);
tblBill.setModel(tableModel);

update when the user presses the "Add Item" button. 当用户按下“添加项目”按钮时更新。

Why do recreate the entire table when you only add a single item? 当您仅添加单个项目时,为什么要重新创建整个表? That is not very efficient. 那不是很有效。

Instead must add a new row to the table. 而是必须在表中添加新行。 This code (like the above code) assumes you are using the DefaultTableModel: 此代码(与上面的代码类似)假定您正在使用DefaultTableModel:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM