Questions tagged [tablemodel]

Table model is attached to Java Swing JTable, providing content to the table, and accepting the cell editing events from the table.

TableModel is an interface included in javax.swing.table package, which defines a contract to work with JTable component. This interface provides methods to handle table's content, accepting cell editing events and data event support through TableModelListener interface.

There are two known implementations: AbstractTableModel and DefaultTableModel. First one is an abstract implementation which provides full event handling and several default implementation for methods related to table's content. Second one is a full implementation based on AbstractTableModel.

Developers has the ability to create their own implementation of TableModel interface, either by extending AbstractTableModel or by implementing the whole interface from the scratch. An example is shown in Creating a Table Model section of How to Use Tables official tutorial.

Here is an example of implementation by extending AbstractTableModel, intended to be used with user-defined POJO objects.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * Abstract base class which extends from {@code AbstractTableModel} and 
 * provides an API to work with user-defined POJO's as table rows. Sub-classes 
 * extending from {@code DataObjectTableModel} must implement 
 * {@code getValueAt(row, column)} method. 
 * 
 * By default cells are not editable. If those are intended to be editable then 
 * sub-classes should override both {@code isCellEditable(row, column)} and 
 * {@code setValueAt(row, column)} methods.
 * 
 * Finally, it is not mandatory but highly recommended to override 
 * {@code getColumnClass(column)} method, in order to return the appropriate 
 * column class: default implementation always returns {@code Object.class}.
 * 
 * @param <T> The data object's class handled by this TableModel.
 */
public abstract class DataObjectTableModel<T> extends AbstractTableModel {

    private final List<String> columnNames;
    private final List<T> data;

    public DataObjectTableModel() {
        this.data = new ArrayList<>();
        this.columnNames = new ArrayList<>();
    }

    public DataObjectTableModel(List<String> columnIdentifiers) {
        this();
        if (columnIdentifiers != null) {
            this.columnNames.addAll(columnIdentifiers);
        }
    }

    @Override
    public int getRowCount() {
        return this.data.size();
    }

    @Override
    public int getColumnCount() {
        return this.columnNames.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return this.columnNames.get(columnIndex);
    }

    public void setColumnNames(List<String> columnNames) {
        if (columnNames != null) {
            this.columnNames.clear();
            this.columnNames.addAll(columnNames);
            fireTableStructureChanged();
        }
    }

    public List<String> getColumnNames() {
        return Collections.unmodifiableList(this.columnNames);
    }

    public void addRow(T dataObject) {
        int rowIndex = this.data.size();
        this.data.add(dataObject);
        fireTableRowsInserted(rowIndex, rowIndex);
    }

    public void addRows(List<T> dataObjects) {
        if (!dataObjects.isEmpty()) {
            int firstRow = data.size();
            this.data.addAll(dataObjects);
            int lastRow = data.size() - 1;
            fireTableRowsInserted(firstRow, lastRow);
        }
    }

    public void insertRow(T dataObject, int rowIndex) {
        this.data.add(rowIndex, dataObject);
        fireTableRowsInserted(rowIndex, rowIndex);
    }

    public void deleteRow(int rowIndex) {
        if (this.data.remove(this.data.get(rowIndex))) {
            fireTableRowsDeleted(rowIndex, rowIndex);
        }
    }

    public T getDataObject(int rowIndex) {
        return this.data.get(rowIndex);
    }

    public List<T> getDataObjects(int firstRow, int lastRow) {
        List<T> subList = this.data.subList(firstRow, lastRow);
        return Collections.unmodifiableList(subList);
    }

    public List<T> getDataObjects() {
        return Collections.unmodifiableList(this.data);
    }

    public void clearTableModelData() {
        if (!this.data.isEmpty()) {
            int lastRow = data.size() - 1;
            this.data.clear();
            fireTableRowsDeleted(0, lastRow);
        }
    }
}
404 questions
2
votes
1 answer

Can't Add TableRowSorter to JTable Produced By SwingWorker

Thank You Hovercraft Full Of Eels for making note of the fact that my question was full of a jumbled mess of code that was unlikely to be solved. Since then, I have created a "minimal" Test Program to display the issue: The Issue What I am looking…
HyperCell
  • 60
  • 1
  • 8
2
votes
2 answers

TableModel fire methods expensive if not visible

in the java swing tablemodel, we are able to fire table changed, added, deleted etc. I am wondering if these method calls are expensive if the component is NOT in visibility? For instance, another window is covering it. Or its in an non-active tab.
delita
  • 1,571
  • 1
  • 19
  • 25
2
votes
1 answer

How to shuffle in Jtable

I have a JTable which contains TableModel(all my data). The JTable has multiple rows and columns. I need to shuffle the rows randomly. I understand I can do that with Collections.shuffle(some list from TableModel); But I dont know how to get the…
user1631306
  • 4,350
  • 8
  • 39
  • 74
2
votes
1 answer

Where to put SQL queries that join on multiple tables?

I am using an MVC framework and have a photo SQL table and an album SQL table. Queries for the photo table go in the photoTableModel class, and queries for album table go in the albumTableModel class. Where do I put queries that use both tables? For…
Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43
2
votes
1 answer

How to get JTable in TableModelEvent

I have created Swing GUI in Eclipse, where i have two JTables and method tableChanged. In this method i need read data from table, when i edit some cell in table and send it to logical layer of my project. Problem is how to identify table ,which…
1000Bugy
  • 105
  • 1
  • 8
2
votes
1 answer

Custom Table Model not working

I wrote a custom table model. The goal of it is to bind values to a list. This list is used throughout the application. If a change is made in the table, anywhere else in the application that change is also reflected. My problem is, my table JTable…
user489041
  • 27,916
  • 55
  • 135
  • 204
2
votes
2 answers

How to make one mySQL's table column invisible

I am running a query on ID column but I don't want it to be visible in my frame/pane. How can I achieve this? Shall I make another table, is there a function in sql/mysql which allows to hide columns? I tried to google it but havent found anything…
Paul
  • 169
  • 13
2
votes
1 answer

The right approach to update complex JTables, TableModel and others

My GUI shows the vehicles in my park, and vehicles that I want to set availables in two different VehicleTables (classes that extend JTable). For availables I intend that these vehicles can be observed from an agent (third-part software). Both the…
Gioce90
  • 554
  • 2
  • 10
  • 31
2
votes
3 answers

"key-value" pairs collection maintaining order and retrieving by key and by pair index?

I'd would like to use a collection of "key - value" pairs: Which is the base for a JTable data model (TableModel implementation), so I need to access elements by their index/position (e.g. to implement Object getValueAt(int rowIndex, int…
mins
  • 6,478
  • 12
  • 56
  • 75
2
votes
1 answer

Java: JTable reordering rows and refresh

I have a JTable (extended) and an implementation of TableModel, in which I have the methods ... moveEntryUp(int rowIdx) moveEntryDown(int rowIdx) ... within the table model implementation. I have verified that these work properly. However, I am…
bguiz
  • 27,371
  • 47
  • 154
  • 243
2
votes
1 answer

JDBC TableModel for a JTable in Java?

I want to display a database table as a JTable. I have never used JTable before so I googled JTable and TableModel. With that googling, I am able to write my own custom TableModel which show data stored in Object[][] data; Now, I want to show my…
Amit
  • 33,847
  • 91
  • 226
  • 299
2
votes
2 answers

Accessing hidden JTable model columns after user sort a column

I have a JTable called transactionList, each row represents a Transaction where the first column holds the actual Transaction object and each subsequent column displays some data about the Transaction. I don't want the Transaction object appearing…
Ron
  • 1,450
  • 15
  • 27
2
votes
6 answers

implement AbstractTableModel for a Java collection

I'm trying to implement an AbstractTableModel for a collection named "clients" but I keep receiving the error "required variable found value" for the "add" method. Here is my code: I'm sorry for the confusion created. The add method is meant to add…
bluesony
  • 459
  • 1
  • 5
  • 28
2
votes
2 answers

Create JTable from ArrayList of Objects - Java

How do I display my "Click" objects in a JTable? ArrayList myClicks = new ArrayList(); Click click = new Click(620, 1028); Click click2 = new Click(480, 230); myClicks.add(click); myClicks.add(click2); It should look something like…
Connor
  • 670
  • 3
  • 9
  • 29
2
votes
1 answer

Why doesn't fireTableStructureChanged() update my table?

I probably do not understand how the method fireTableStructureChanged() works. I am assuming that if I call the method, the implemented methods getColumnCount(), getRows() and getValueAt(int row, int col) are called so that my Table with the…
F. Hall
  • 33
  • 1
  • 4