Is it possible to overload the getValueAt method on the abstractTableModel?
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?
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.
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.
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);