4

If I want to use a JTable in Java it seems to me for adding rows and doing alters from behind a button or so I always have to use a TableModel (this could be the default one or one created by your own) But my question is: Why do we have to use this. I can't find this in any of the posts I saw. Can someone explain how this works and why it is necessary? And why we can't just add rows to the JTable without a model.

It seems to me that if you want to just show a few records but at creation you don't know all the rows yet it would be easier to just do something like a table.add() to add the row. You can create the table with data inside without a model attached to it. So why not add data?

Or am I just wrong and can you add also data without a model?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nightfox79
  • 2,077
  • 3
  • 27
  • 40
  • and there is significant difference betweens DeafultTableModel and AbstractTableModeel – mKorbel Apr 16 '13 at 20:15
  • @Paul Vargas not theoretically all JComponents has model, – mKorbel Apr 16 '13 at 20:16
  • @PaulVargas I would also suggest it was some from the Swing team ;) – MadProgrammer Apr 16 '13 at 20:17
  • Thanks for the comments, but I already know how to use it. I just wanted to know why we have to use it. I think it is good if you work with resultsets and a database but if you just want to show some data I find it overload, although it might be only a few lines of code. So because of that I asked my question here. – nightfox79 Apr 16 '13 at 20:18
  • 3
    The basic design philosophy of Swing is based loosely on the MVC model. This requires all view components to be controlled in part by a separate data model. In separates responsibility and increases flexibility – MadProgrammer Apr 16 '13 at 20:18

1 Answers1

4

The TableModel interface defines the minimum methods needed by a JTable (view) to render its content (model). AbstractTableModel is an abstract implementation that provides the event plumbing and leaves just three methods that must be overridden. DefaultTableModel goes on to include an internal data model based on Vector and convenient methods to alter that internal model. See Creating a Table Model for a comparison and these contrasting examples.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Is the TableModel "controller", "view" or the "model" in MVC? – Koray Tugay May 12 '17 at 20:22
  • @KorayTugay: It is a _model_; more [here](http://stackoverflow.com/a/3072979/230513). – trashgod May 12 '17 at 22:59
  • I am not sure if I fully agree it is the "Model". I think it still falls in the "View". A TableModel does not implement and business logic or encapsulate any data, it seems to be more concerned with presentation to me. – Koray Tugay May 13 '17 at 05:20
  • @KorayTugay: I would argue that a `TableModel` encapsulates _all_ of the data displayed by the table and contains _no_ business logic. As comments are not suitable for extended discussion, your might want to pose a question on this topic. – trashgod May 13 '17 at 10:23