1

I am making a JTable that uses an arrayList for data population. I have two sets of data in the arrayList. one for one type of table and another for another type of table.

What I am hoping to do is overload the getValueAt method on the abstractTableModel so that it takes in an argument to which set of data I want.

Is this possible or should I think about this a different way?

Robert
  • 4,306
  • 11
  • 45
  • 95

3 Answers3

2

Nobody stops you from overloading the method. It's just that the JTable code won't call your new method. You will have to overwrite the regular public Object getValueAt(int rowIndex, int columnIndex) and call the other method from inside it, based on your business logic.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

you can do so, however the problem is that whoever in Swing components is calling this method currently would not call the oveloaded one :)

It would not be too much usefull.

From my point of view, you have basically 2 options, as you need to present the specific data in 2 different table types:

  • either I'd go for 2 different table models and keep all the data separated
  • or the other approach might be to have some specific flag (new field) on model, that would indicate the table you use it in. This one could be set by setter or directly in constructor if you know which type you'd go for.

However the 1.st one would be a recommended way from my point of view.

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
0

There's nothing wrong with the accepted answer, but also consider a second TableModel that shares a reference to the given List with the first. A single JTable could display either model by simply invoking setModel().

In this exmaple, EnvDataModel obtains a its data via System.getenv(). A PropDataModel might obtain a its data via System.getProperties(). Both share access to System.

TableModel first = new EnvDataModel();
TableModel second = new PropDataModel();
JTable table = new JTable(fisrt);
...
table.setModel(second);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045