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

Blackberry: Drawing TableModel focus properly

I need help with drawing the focus of the selected row properly. Currently if I select the first item of a category the separatorrow gets highlighted too. So how can I implement my custom focus drawing so that only the selected row gets…
Danscho
  • 466
  • 5
  • 20
0
votes
1 answer

Setting the TableModel on an already constructed Table

You can create a table model and add it to a table TableModel tm = new TableModel(); JTable table = new JTable(tm); however, if I init a Table JTable table = new JTable(); and then create a table model later on... TableModel tm = new…
Jonathan Viccary
  • 722
  • 1
  • 9
  • 27
0
votes
1 answer

Refreshing JTable when data has changed

I have a problem with refreshing a JTable. I start with an empty ArrayList and after setting my choice of a combo box I load content to the ArrayList but JTable does not react to it - it remains empty. Is it a problem with a TableModel? This is my…
Swemack
  • 11
  • 3
0
votes
2 answers

How to add data to JTable created in design mode?

I created an initial JFrame that contains a table with three columns, as shown below: This JFrame was created in design mode, and so now in the panel's constructor, I want to load some data so when the user selects to open this JFrame, the data is…
swiftcode
  • 3,039
  • 9
  • 39
  • 64
0
votes
2 answers

Mass produce JTables

I want to make 25 JTables. I generate the table names by doing: for(int i=0; i < 26; i++) { TableNames[i] = "Table" + i + ""; ... How can I go about using these String names in the array as the new JTable names? i.e. TableNames[i] = new…
user1042304
  • 609
  • 2
  • 6
  • 6
0
votes
3 answers

Vertical scrollbar in jTable swing does not appear

I am a bit new to swings, And i was trying to cough up some code involving Jtable. I find that even though i have added the scrollbar policy the vertical scrollbar does not seem to appear. THe code is pretty shabby.(warning u before hand). But…
Raveesh Sharma
  • 1,486
  • 5
  • 21
  • 38
0
votes
2 answers

get table model from table with unknown rows and columns

I know how to get a simple TableModel from a table that I knew. But I wanted to know how to get a TableModel from a table which I don't know its columns and rows details. Isn't there a way to get the table model directly from the ResultSet?
Tarik
  • 2,151
  • 4
  • 19
  • 26
0
votes
1 answer

Editting duplicate row in tableMode, edits original row also

I've created an add row function, that adds either the selected row, or last row to the end of the tableModel. When I go and edit the new row the original row also gets edited. I thought i created a new distinct row or did i create a reference to…
Tai
  • 63
  • 2
  • 7
-1
votes
2 answers

Java set data to JTable from database

i try to insert data from database to Jtable there is my code: private Vector > data; private Vector header; table.setModel(new javax.swing.table.DefaultTableModel( data,header )); GtFromDb db=new…
Edgar Buchvalov
  • 257
  • 2
  • 12
  • 22
-1
votes
2 answers

How to populate Jtable with values from Arraylist (Null Pointer Exception in Jtables)

I am attempting to create a gui with two Jtables. One which outputs object Bike (if avaliable) and one which outputs object Rent. However Default Table Model keeps returning Null Point Exception. How do I populate a JTable with an arraylist of…
Chornologic
  • 117
  • 2
  • 8
-1
votes
1 answer

How to ignore a column of a TableModel in a JTable?

I have a TableModel to a JTable and I must put it on another table, and I've done it just seting the model of the second table with setModel(firstTableModel). The problem is that I don't want that one of the columns of the model shows on the second…
halierier
  • 27
  • 5
-1
votes
1 answer

Change the column header text for DefaultTableModel

Right now, I am trying to create a table with custom code for my Java application (Swing GUI). I create the table with this code: DefaultTableModel tm = new DefaultTableModel(9,5); I got 5 columns with 9 rows for the table: I just want to…
Stoyan377
  • 11
  • 4
-1
votes
1 answer

getRowCount() and getSelectedColumn() don't work

This is how I get data from csv-file: cSVFileReader = new CSVReader(new FileReader(sciezka), ','); // csv reader with coma-separator java.util.List myEntries = cSVFileReader.readAll(); String[][] rowData = myEntries.toArray(new…
Kat Roz
  • 59
  • 9
-1
votes
1 answer

array out of bounds exception from tablemodel-

This is my method, I have gone through every out of bounds exception but cant work out whats going on. This is not the same as other questions as i have tried all the logical steps issued from other questions private void gettablecount(TableModel…
Ingram
  • 654
  • 2
  • 7
  • 29
-1
votes
1 answer

getValueAt called on mouse over cell

I noticed that when i move the cursor over any cell of the table, the function 'getValueAt' gets called, showing into the Eclipse console its output. Was just wondering why, shouldn't it be called only when i create the tableModel object? I created…
1 2 3
26
27