Hi

I just started learning on making gui on Java. I watched some youtube videos and i realise one of the them uses DefaultTableModel. At first i thought it was to allow access to the function of the table but i realise i can directly
write and get value from the table itself by directly calling the table name.

Need someone to explain what is the point or benefits of using DefaultTableModel?

Thanks
Cleve

Dani AI

Generated

Quick clarification for , building on : the JTable visible on screen is the view; the TableModel is the data backend. DefaultTableModel is a ready-made backend that stores the cells for you and sends the table the change events that make the UI update. Calling table.getValueAt(...) works because JTable forwards reads and writes to its model, but structural changes (adding/removing rows or changing columns) should be done through the model so notifications and editors/renderers stay consistent.

When to use DefaultTableModel vs a custom model: DefaultTableModel is great for learning, small tables and prototypes because of its convenience methods (addRow, removeRow, setValueAt). Its limits are that it stores everything as Object (no compile-time column types), keeps all data in memory, and is not ideal for lazy loading or very large datasets. For production code or when column types, performance, or custom behavior matter, implement AbstractTableModel so getColumnClass, getValueAt and change notifications can be tuned.

Practical example and pattern to follow:

DefaultTableModel model = new DefaultTableModel(new Object[]{"ID","Name","Qty"}, 0);
JTable table = new JTable(model);

// add data on the Event Dispatch Thread
SwingUtilities.invokeLater(() -> model.addRow(new Object[]{1, "Apple", 10}));

// read from the model (table delegates to model)
Object v = model.getValueAt(0, 1);

// custom model skeleton
class MyModel extends AbstractTableModel {
  public int getRowCount() { /* ... */ }
  public int getColumnCount() { /* ... */ }
  public Object getValueAt(int r, int c) { /* ... */ }
  public Class<?> getColumnClass(int c) { /* return real type */ }
}

Troubleshooting and best practices: always update models on the EDT; if changes do not appear, use the model API or fireTableXxx methods rather than mutating internal lists directly; use SwingWorker for DB or heavy loads; use TableRowSorter for sorting/filtering; switch to AbstractTableModel when type-safety or performance become important.

Recommended Answers

All 2 Replies

Tables are split into two main parts - the visible GUI stuff, and the data for the table. This allows you to chose how to store the data any way you like, without affecting the way the GUI display works (and vice-versa). The part that holds the data is called the TableModel.
For simple cases, where there's nothing special about the data storage, you use a DefaultTableModel that does it for you, so you don't have to write any extra code. Similarly, the GUI part of the table has methods to access the data in the TableModel for you, so you don't need to bother with that either unless you have a special requirement.

Oracle's tutorials are an excellent source of info. Maybe not the easiest because they are so comprehensive, but an ideal place to go when a simpler tutorial hasn't answered all your questions, eg
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

thanks alot!!! will read up on that link. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.