Alptugay
Alptugay

Reputation: 1686

Adding an object to and retrieving an from a table

Let's assume that I have a class like this:

public Class FooBar {
  String foo;
  String bar;
  String unvisibleField;
  String id;
}

Then I have created a table using DefaultTableModel and adding elements to it like this (I am not showing all attributes of the class to the user):

for(int i=0;i<fooBarList.size();i++){
  model.addRow(new String[]{fooBarList.get(i).getFoo(), fooBarList.get(i).getBar()});
}

But I want to retrieve the FooBar class object from the table. Something like this:

model.getRow() will return a FooBar object So probably I will also need something like

model.addRow(FooBar item)

Upvotes: 1

Views: 52

Answers (2)

mKorbel
mKorbel

Reputation: 109813

Upvotes: 1

Pierre Arlaud
Pierre Arlaud

Reputation: 4133

If you're absolutely sure you'll only be returning FooBar object you just have to cast the objectsof the model into FooBar

Upvotes: 1

Related Questions