Java Swing: implement TableModel or extend AbstractTableModel?

When should I use TableModel better and when should I extend AbstractTableModel?

+4
source share
3 answers

AbstractTableModel has an implementation for handling TableModelListeners , including firing TableModelEvent s. If you want to deal with it yourself, then there really is no reason to spread. Outside of this code, another code does not add any benefits other than that the process declared in the interface will be executed.

+3
source

When should I use TableModel

If you need a complete implementation of a clean table table and / or you must provide something that, by the nature of your model, does not yet exist or does not fit.

and when should extend AbstractTableModel?

If you can reuse the existing structure and methods provided by the abstract class, and / or it will be easier for you to implement it.

The idea is that if you can reuse it, do it. If you cannot, do it from scratch.

Inheriting AbstractTableModel, you will still use the TableModel interface, it will be easier.

+4
source

AbstractTableModel provides standard implementations for many of the methods defined in TableModel. If you don't need any custom behavior, you can simply extend AbstractTableModel. Also, if your model is already extending another base class, then it would be wise to implement TableModel.

+1
source

Source: https://habr.com/ru/post/1308913/


All Articles