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
1
vote
3 answers

Is there a generic TableModel we can use in JTables?

I'm now looking into JTables and have a bunch of business objects that I retrieve from the DB with Hibernate + Spring Data JPA. I love that Spring Data JPA handles all the cumbersome implementation of the DAL and was wondering if there's something…
Joao Coelho
  • 2,838
  • 4
  • 30
  • 36
1
vote
3 answers

Remove AbstractAction from jtable

Ok. I have a large project where a particular jtable is created at startup and never gets rebuilt. The table model is refreshed and the table redrawn based on various user actions. I've added a custom TableCellListener class to react to cell changes…
jeff
  • 87
  • 13
1
vote
1 answer

JTable - why doesn't it keep the TableColumns after adding a row?

I'm using a JTable : public class MyPanel extends JPanel { public MyPanel () { init() } init () { DataModel model = new DataModel(); JTable table = new JTable (model); TableColumnModel tcm = agentsTable.getColumnModel(); …
Adi Mor
  • 2,145
  • 5
  • 25
  • 44
1
vote
0 answers

Displaying a LinkedHashMap in a JTable

I have an Order object which among other things stores a LinkedHashList (Product : amount). I use a LinkedHashList because I specifically want to keep the insertion order. public class Order { private Map productMap; …
PrOF
  • 93
  • 1
  • 6
1
vote
1 answer

How to create a scatterplot GUI linked to change in a tablemodel(dataframe)

I have a GUI, which consist of a Qtableview and dataframe scatter plot widget. Plot draws the X, Y value from the table and has colormap with Z value. Here's the point. When I modify Y value in Qtableview, then press the refresh button on the bottom…
Brian Kim
  • 33
  • 4
1
vote
2 answers

JTable how to hide column in view but keep it in model for use it in background

I have a JTable and am passing a data array into the TableModel. I wish to retain all columns in the data model as I need them all for background data processing, but I wish to show only some of the columns in the JTable. How do I achieve this --…
Umesh K
  • 13,436
  • 25
  • 87
  • 129
1
vote
0 answers

Update changes In JTable to database?

It's not working. Using the select syntax put table to JTable. Then I want update the changes in JTable to the Database. It's a reasonable idea, if there is an better idea to Update GUI changes to database, please let me know. The following is my…
jian
  • 4,119
  • 1
  • 17
  • 32
1
vote
2 answers

How to add rows to TableModel with JopenDocument

I am working on a java application that reads a file template.ods, and fills it in with an array of Objects using JopenDocument. Based on the JopenDocument documentation, I should get the TableModel from the Sheet, and then use the method…
Zakaria Marrah
  • 755
  • 6
  • 15
  • 28
1
vote
2 answers

Getting JTable from its TableModel

I have a function which triggers with: public void tableChanged(TableModelEvent e){...} I got the TableModel from the TableModelEvent with: TableModel model = (TableModel)e.getSource(); But I need the JTable to use it in a TablecellBalloonTip…
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
1
vote
2 answers

Multiple instances of model components in Java Swing?

Until now I had different model classes for the appropriate Java Swing component, for instance I have several TableModel for several JTable. Every JTable has its own TableModel. The TableModel is based on one object (Model), giving all the required…
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
1
vote
1 answer

Are row and column indices that TableCellEditor#getTableCellEditorComponent() receives view coordinates?

I'm writing a TableCellEditor implementation which needs to fetch some data from underlying TableModel. In order to do that, I need row and column indices for TableModel. What I'd like to know is whether the indices getTableCellEditorComponent()…
Kohei Nozaki
  • 1,154
  • 1
  • 13
  • 36
1
vote
1 answer

Java: serverside Persistent storage solution for TableModel and TreeModel?

Scenario: client would logon, create their own TableModel and TreeModels and be able to access it at a later time. TableModel and TreeModel which comprises of Java class objects as well as strings. How would I be able to store this on couchDB? (is…
KJW
  • 15,035
  • 47
  • 137
  • 243
1
vote
1 answer

How to define the JTable's Column's data type the same as MySql database?

I have created a graphical interface in Netbeans where I inserted a JTable inside a FORM JFrame. I have a MySql database and it's columns are: Id: Integer Name: String Active: Boolean However, when I use: jTable.setModel…
Arroiz
  • 13
  • 2
1
vote
0 answers

Dividing Jtable for printing prints blank table

To make this short, I want to print a table that is divided into sections (Columns 1 - 16 and Columns 17 up) and this is the code I have thought of. However, with this code, only the first part of the table will be printed with the other part being…
Darrius
  • 167
  • 1
  • 1
  • 11
1
vote
1 answer

Saving JTable Values in an ArrayList

I'm currently programming Yahtzee and I'm in the process of changing the player names. For this I use an extra form in which there is a JTable and a JButton. Depending on the variable number of players in the table, an entry will be created where…
iTz_ElKay
  • 41
  • 7