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
4
votes
1 answer

Can't seem to get the TableModelListener to work

I am creating an UI class in which everything will run (a different class will work as the runner). In this class I have a table and the table is supposed to create TableModeEvents when changed, but it doesn't seem to do so. The console is supposed…
Nacht
  • 10,488
  • 8
  • 31
  • 39
4
votes
3 answers

Java Swing: implement TableModel or extend AbstractTableModel?

When should I rather implement TableModel and when should I extend AbstractTableModel?
HansDampf
  • 161
  • 2
  • 11
4
votes
6 answers

How to refresh data in JTable I am using TableModel

Hi, I have created my TableModel and want to refresh JTable once I added a new row. What should be added to the listener to "refresh" JTable? public class MyTableModel implements TableModel { private Set listeners = new…
devger
  • 703
  • 4
  • 12
  • 26
4
votes
1 answer

Why do we have to use a TableModel for a JTable?

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.…
nightfox79
  • 2,077
  • 3
  • 27
  • 40
4
votes
3 answers

Calling Previous Model Behavior of JTable.setModel()

Have one JTable on my swing screen. While loading screen, I am setting one table model which is created for this table only. And at run time if there is any data change I am recreating the same model and again set it using…
Navnath
  • 1,064
  • 4
  • 18
  • 34
3
votes
2 answers

Cell won't update after an action is called in the cell editor

I'm using a table with a cell renderer which allows me to place a button "x" in each cell with a value so that i can delete the value of the cell by pressing the button. The cell editor assigns the action listener to the button and the proper…
user1149359
3
votes
4 answers

Making JTable cells uneditable

I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table model class, extend DefaultTableModel and then override method…
3
votes
4 answers

Visualizing a Set with JTable in Java Swing

I would like to visualize a Set of Threads, something like: Set. I choose Set, as every Thread in the JVM is unique. With the choice of displaying it with the component JTable in Java Swing I am facing some issues. I need to implement a…
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
3
votes
2 answers

AbstractTableModel setValueAt not firing upon Jbutton Click

I have a custom TableModel that extends the AbstractTableModel. public class IndicatorPropertyTableModel extends AbstractTableModel The setValueAt function I wrote fires appropriately in cases when you type a new value into the editable cell and…
Judicator
  • 133
  • 1
  • 8
3
votes
2 answers

JTable, TableModel and TableColumnModel - Weird thing going on

I am developing a costum JTable for a client of mine. I had just finished the column model when I started on the table model. Most of the functions that are related to columns in the table model are actually aliases for the functions in the column…
3
votes
1 answer

Add Checkbox to CodenameOne TableModel in Table component

Can you tell me if I should be doing this differently? I need to make the last cell on my data rows a checkbox that is tied back to an object I will remove form the list when a delete button is clicked. When I manually create the TableModel in code…
Java-K
  • 497
  • 2
  • 11
3
votes
3 answers

JTable's and DefaultTableModel's row indexes lose their synchronization after I sort JTable

JAVA NETBEANS // resultsTable, myModel JTable resultsTable; DefaultTableModel myModel; //javax.swing.table.DefaultTableModel myModel = (DefaultTableModel) resultsTable.getModel(); // event of clicking on item of table String value = (String)…
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
3
votes
1 answer

JTable with row Filter - how to iterate over filtered rows only

I have JTable where rows are filtered by RowFilter (the row B is hidden). How can I iterate over visible rows only (not the hidden B row)? The example bellow unfortunately prints also the B row which I would like to skip. import…
Radim Burget
  • 1,456
  • 2
  • 20
  • 39
3
votes
2 answers

Working with several custom table models avoiding repetitive code

I'm working in a project in which we have several domain classes to model business data. Those classes are simple POJO's and I have to display several tables using them. For example, consider this class: public class Customer { private Long…
dic19
  • 17,821
  • 6
  • 40
  • 69
3
votes
2 answers

How to update jtable model correctly?

I have been thinking a lot to solve this mistake. But unfortunately I came to final cnclusion that I need help of professionals. Please, copy,paste this code to see issue: public class DateFormatDemo extends JFrame { private JTable…
Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83
1
2
3
26 27