Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: implement TableModel or extend AbstractTableModel?

When should I rather implement TableModel and when should I extend AbstractTableModel?

like image 411
HansDampf Avatar asked Feb 28 '23 02:02

HansDampf


2 Answers

When should I rather implement TableModel

When you need a complete clean sheet implementation of the table model and/or you have to provide something that by the nature of your model doesn't exists yet or doesn't fit.

and when should I extend AbstractTableModel?

When you can reuse the existing structure and methods provided by the abstract class and/or it would easier for you to implement it.

The idea is, if you can re-use it, do so. If you can't, implement from scratch.

By inheriting AbstractTableModel you'll be imlementing the TableModel interface anyway, it just will be easier.

like image 81
OscarRyz Avatar answered Mar 05 '23 18:03

OscarRyz


AbstractTableModel has implementation for the handling of TableModelListeners, including the firing of TableModelEvents. If you want to handle that yourself, then there really is no reason to extend. Outside of that code, the other code doesnt add any benefit other than the stubbing out of methods declared in the interface.

like image 45
akf Avatar answered Mar 05 '23 18:03

akf