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

Have a TableModel. Need to sort based on two different columns simultaneously

My tableModel contains multiple columns (for ex: Columns A - F), of which when I first display, I should display the table sorted by firstly Column B and later by D. Now, the issue that am facing is: Col. D is timestamp. Col. B contains identifiers…
user2406106
  • 23
  • 1
  • 3
2
votes
1 answer

JTable not updating when model updates

Ok, I have read up on this, but I'm still very confused. I have a JTable with a custom Table Model that stores data in an ArrayList. It displays just fine. However, when I want to add a row, I add an object to the ArrayList, then call…
Chris Chambers
  • 1,367
  • 21
  • 39
2
votes
2 answers

JTable cell renderer skips Boolean column

Below is a picture of my JTable. My problem is that the blue background every other row isn't present in the 4th column. My table model and cell renderer are also below. Table model: public class MyTableModel extends DefaultTableModel { public…
Joel Christophel
  • 2,604
  • 4
  • 30
  • 49
2
votes
2 answers

JTable + TableModel cache fetch event for lazy instantiation?

Scenario: you are using a JTable with a custom TableModel to view the contents of some collection located in a database or on the network or whatever. The brute force way to make this work is to load the whole collection at once. Let's say that…
Jason S
  • 184,598
  • 164
  • 608
  • 970
2
votes
2 answers

Two JXTables with same custom model - How to make cells editable in one table but non-editable in the second?

I have a custom table model whose data I want to edit in one JXTable, but view-only in a second JXTable. Can this be done without having two separate models? Is there some way of overriding model.isCellEditable for the view-only table? import…
shanesolo
  • 141
  • 1
  • 1
  • 6
2
votes
1 answer

Swing dependent components and event system

I have to build a complex GUI in JAVA with Swing (for the moment I have near 80 classes). The graphic partv of the application is split as follows: a first series of tabs (eg "Management", "Administration", "Configuration"), then a second level (for…
GlinesMome
  • 1,549
  • 1
  • 22
  • 35
2
votes
2 answers

TableModel - modify external objects upon setting a value

I have a separate class that implements a TableModel interface used for JTable. I have one Boolean column, presented as a column of checkboxes and I was wondering how I can inform an external object that certain values need to be updated? If I had…
Bober02
  • 15,034
  • 31
  • 92
  • 178
2
votes
5 answers

Clearing data from one JTable is also deleting the another JTable

I am doing an application in Java using Swing. I have two tables and I have to copy contents from one table to another (Replication.) The problem is if I clear the destination Table rows then my source table rows are also getting deleted. If I…
Amarnath
  • 8,736
  • 10
  • 54
  • 81
2
votes
2 answers

Having trouble in finding out what ArrayIndexOutOfBoundsException: 6 > 1 means? when sorting jtable

i am having trouble finding out problem in my jtable sorting mechanism ,when ever i implement sorting inside the search code it gives me array index out of bounds , the populate table code works fine at intial stage but after the search happens it…
Clux R
  • 23
  • 1
  • 3
2
votes
1 answer

Refresh table model or GUI in Swing JAVA

In a program I have been working on, I need to refresh a table with new data. Currently, since I am new to GUI's, I was going to refresh the entire GUI with a new table. My program does this, but it ends up building underneath the previous GUI. …
user1093111
  • 1,091
  • 5
  • 21
  • 47
2
votes
1 answer

how can i use removeTableModelListener

I implemented "addTableModelListener" at run-time in my table-model, but I want to create one more control for removing it. I have searched Google, but I haven't found any suitable logic to implement removing the table model listener. Please help me…
2
votes
1 answer

JTable load values on change, display progressbar while loading

So i am working with a JTable, It has Columns A-K. with A and B being the only editable ones. If someone edits an empty row in A, I make an API call to get B then i make a DB call to get all rows where B exists.If someone edits an empty row in B, i…
dsymquen
  • 584
  • 1
  • 5
  • 23
1
vote
2 answers

Jtable - getvalueAt() issue

I want to know why my getvalueAt() is picking old data when Enter is pressed. I tried all update and table change modules, but I couldn't get it working. I am making an Excel sheet-like structure in JTable, in which one row updates on change to…
greatmajestics
  • 1,083
  • 4
  • 19
  • 41
1
vote
3 answers

Highlight a cell in JTable via custom table model

I have a JTable and a JTextField, I want to highlight the cell which corresponds to the text in the JTextField. I added Todo in the code but I don't know how to do it. How is it possible to do it within table model? Can anybody suggest a code…
itro
  • 7,006
  • 27
  • 78
  • 121
1
vote
1 answer

Java - TableModel and DefaultTableModel

I've spent quite a while trying to figure out a way of adding a new row to a JTable, initially by looking for methods on the following model: TableModel model = new DefaultTableModel(data, tabs); However, some quick searching lead me to find that…
mark
  • 2,841
  • 4
  • 25
  • 23