开发者

Populate JTable using one object per row

开发者 https://www.devze.com 2023-02-14 23:56 出处:网络
I\'m sure this is possible but I am having trouble find开发者_StackOverflowing the appropriate documentation or a simple example for this situation.

I'm sure this is possible but I am having trouble find开发者_StackOverflowing the appropriate documentation or a simple example for this situation.

For example's sake, lets say I have an object such as this:

public class Person {
    private final String name;
    private final int age;
    private final String address;

    // Constructor, Getters and Setters
}

Now I would like to be able to do something like this:

myTableModel.addColumn("name");
myTableModel.addColumn("age");
myTableModel.addColumn("address");
myJTable.setModel(myTableModel);
for (Person person : people) {
    myTableModel.addRow(person);
}

And then each column would use the row's Person object to "get" its appropriate information to display.


Create a custom TableModel. Here is a sample I wrote some time ago:

https://sourceforge.net/p/puces-samples/code/HEAD/tree/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java

Instead of the NbBundle you can also use a ResourceBundle, if you're not building your application on top of the NetBeans Platform (though I recommend to use a RCP).

0

精彩评论

暂无评论...
验证码 换一张
取 消