简体   繁体   English

Java jTable 数据来自 ArrayList 对象和从其他方法更新

[英]Java jTable data from ArrayList objects and update from other methods

I'm new to Java GUI and trying to work with jTable's in SWING.我是 Java GUI 的新手,并尝试在 SWING 中使用 jTable。 I currently have one which I made by following the Oracle tutorial and it gets the table data by using:我目前有一个我按照 Oracle 教程制作的,它使用以下方法获取表数据:

Object[][] data = {
    {"Kathy", "Smith",
     "Snowboarding", new Integer(5), new Boolean(false)},
    {"John", "Doe",
     "Rowing", new Integer(3), new Boolean(true)},
    {"Sue", "Black",
     "Knitting", new Integer(2), new Boolean(false)},
    {"Jane", "White",
     "Speed reading", new Integer(20), new Boolean(true)},
    {"Joe", "Brown",
     "Pool", new Integer(10), new Boolean(false)}
};

I have an object called Orders which has an ArrayList (productsInOrder) which contains an unlimited number of Project objects.我有一个名为 Orders 的 object,它有一个 ArrayList (productsInOrder),其中包含无限数量的项目对象。 I am trying to get the table to display each of the following for each object in the ArrayList.我试图让表格显示 ArrayList 中每个 object 的以下各项。 productsInOrder.getPrice() productsInOrder.getSKU() productsInOrder.getName() productsInOrder.getPrice() productsInOrder.getSKU() productsInOrder.getName()

Can anyone point me in the right direction?谁能指出我正确的方向? Or link me to a tutorial that can help as I've looked all over the internet and can not figure this out.或者将我链接到一个可以提供帮助的教程,因为我已经查看了整个互联网并且无法弄清楚这一点。

Finally, I have elsewhere I have an action listener which adds more Product objects to the ArrayList when the user performs certain actions.最后,我在其他地方有一个动作监听器,当用户执行某些动作时,它会向 ArrayList 添加更多产品对象。 How would I update the jTable?我将如何更新 jTable? Simply by reloading the whole jTable in the action listener or is there a way to simply add another row to the table?只需在动作侦听器中重新加载整个 jTable 或者有没有办法简单地将另一行添加到表中?

Or link me to a tutorial that can help或将我链接到可以提供帮助的教程

The tutorial you've read is the tutorial that should help you.您阅读的教程是应该对您有所帮助的教程。

The basic solution is to create a custom TableModel.基本的解决方案是创建一个自定义的 TableModel。 In the example they store the data in a 2D array.在示例中,它们将数据存储在二维数组中。 In your case the data will be stored in an ArrayList.在您的情况下,数据将存储在 ArrayList 中。 Therefore you will need to modify the getVAlueAt() method to access the ArrayList instead of the 2D array.因此,您需要修改 getVALueAt() 方法以访问 ArrayList 而不是二维数组。

Finally, I have elsewhere I have an action listener which adds more Product objects to the ArrayList when the user performs certain actions.最后,我在其他地方有一个动作监听器,当用户执行某些动作时,它会向 ArrayList 添加更多产品对象。

Your code should update the TableModel, not the ArrayList.您的代码应该更新 TableModel,而不是 ArrayList。 Then the table model will notify the table.然后表 model 会通知表。 This means you need to implement an addRow(...) method in your custom TableModel.这意味着您需要在自定义 TableModel 中实现 addRow(...) 方法。

For a more complex solution you can create a generic table model which can be used.对于更复杂的解决方案,您可以创建一个可以使用的通用表 model。 See the Bean Table Model for an example of this approach.有关此方法的示例,请参阅Bean 表 Model The JButtonTableModel example code shows how you can further customize the BeanTableModel for only the properties you are interested in. JButtonTableModel 示例代码展示了如何进一步为您感兴趣的属性自定义 BeanTableModel。

The Definitive Guide to Swing by John Zukowski is an excellent resource on Swing. John Zukowski 撰写的Swing 权威指南是 Swing的优秀资源。 It addresses everything very methodically with relevant examples.它通过相关示例非常有条理地解决了所有问题。 Highly recommend it.强烈推荐它。

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

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