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

Is it possible to separate the jtable model to 5 different models?

I create my table model and now i want to seperate it and show it in different JTables. What is the best way to do that? Here is what i tried : public List getProcedures(JPanel panel) { Object[] data = new…
Aris
  • 984
  • 8
  • 22
1
vote
1 answer

getDataVector gives various data types from TJable

I have a JTable and when I use jTable1.getModel()).getDataVector() objects from different columns have different types (they should be all Strings for my case) My table has five columns: number (it's String actually, but I have no problem with…
Edheene
  • 455
  • 2
  • 7
  • 20
1
vote
1 answer

How to Show Null Values in JTable?

I have a table that fetches data from a database. But in my database, sometimes some fields can be null values and JTable does not show these null values. I want to show null values in my table as NA. How can I do that? public void…
Gamerr89
  • 41
  • 5
1
vote
1 answer

how color the minimum value cell on JTable?

I am developing a small application on Java. I created a custom model for jtable. The model is this: package tienda.funcionalidad; import java.awt.Component; import java.util.ArrayList; import javax.swing.JTable; import…
1
vote
3 answers

Remake a JTable by clicking on a JButton

I have a JTable which is 9 by 3 (27 cells), but if the user clicks on a button I want the table to change, being filled with not only new data, but possibly a different number of cells. I am not sure how to do this. I've tried removing the table…
user485498
1
vote
1 answer

StackOverflow Error when using .setValue

I am using a setValue(...) in a TableModelListener after multiplying to cell values. Without the .setValue the program runs fine, however, when it is added it seems as though the program runs indefinitely or until the exception is thrown. Here is…
1
vote
3 answers

HowTo Remove a Row in a JTable with a Custom TableModel

I've been reading posts similar to mine, and reading through the Java tutorial page but I just can't seem to get this working. I'm not sure if I'm missing something fundamental or not... I have a custom table model below that I need to be able to…
linsek
  • 3,334
  • 9
  • 42
  • 55
1
vote
1 answer

I'm not sure why a variable is inaccessible

I'm writing a small program that creates a gui to display the contents of a csv file. I've tried following the outline from the Oracle website (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data), but my problem is that the…
André Foote
  • 366
  • 3
  • 15
1
vote
0 answers

JTable custom cell editor is not firing changed event

Overview I tried to create a custom cell editor for a project I'm working on and what I need to achieve is to fire the table model changed event when an editing is done in my Cell Editor. I have used a custom cell editor and a custom Cell Renderer…
1
vote
0 answers

Why the extra calls to getColumnClass() within the Model?

Found this nice sample program illustrating the use of a TableModel. import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; public class…
Unhandled Exception
  • 1,427
  • 14
  • 30
1
vote
3 answers

How to use setValueAt, when I change cell in column in JTable

I have JTable with rows and columns, I need when I edited any cell in column with index 4 -> should changes "VALUE" in the same row, but next column with index 5. I have next code, but it doesn't work table.getModel().addTableModelListener(new…
GoldenScrew
  • 181
  • 2
  • 3
  • 14
1
vote
0 answers

TableModel setValueAt

Hi have a Problem with my AbstractTableModel... i want to specify the setValueAt and i have a LocalDate and a boolean case but i dont know how to specify that in the setValueAt method... any suggestions? AbstractTableModel Code: package…
Bobby
  • 21
  • 3
1
vote
1 answer

Display alternate toString() Method with custom cell editor

I am creating what is basically I copy of excel. The user enters data into a JTable, then that data is parsed, processed, and it displays the toString() method for the appropriate type of Cell, which is an interface that I have created several…
traviata
  • 27
  • 4
1
vote
1 answer

JTable - TableModelEvent only values that changed

The porblem is that even I change cell value to the same exact value it counts as a change. So for example if I double click a cell and don't change its value my program proceeds to write it to the server. How can I make that only cells that have…
1
vote
1 answer

How to set a Boolean object type to a table column in java

I have a JTable which has DefaultTableModel. Now I want to change table column headers and columns data types(eg :- Boolean,String,Object,etc). I try to do as following . DefaultTableModel model = (DefaultTableModel) list_table.getModel(); …
Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61