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
0 answers

TableModelListener changing the values

I am trying the write the code of the number converter tool in 8085 simulator. I created Jtable called "noConTool with the size 2,8. Then I wrote the listener. However it does not work. private class hexaListener implements TableModelListener { …
0
votes
1 answer

Building Java TableModel from list of results

Hi I have problems with populating a TableModel, I cannot understand what the problem is here is my method private TableModel buildTableModel(List result) { // build the columns Vector columnNames = new…
nuvio
  • 2,555
  • 4
  • 32
  • 58
0
votes
1 answer

TableModel, causing duplicate Database Info in a Java JFrame

I have this method setup in a JFrame to be called when the user presses a button to "Load File"; however, when the user presses the button a second time . . . they get another table underneath the first table so all the information gets duplicated…
Ray Wade
  • 25
  • 1
  • 5
0
votes
0 answers

Creating a Table Model component using Netbeans

I'm trying to create a custom table model, but I got stuck. I want to create a table model and bind it using the Netbeans GUI. However, as you can see on the screenshot, the Component dropdown is inactive. How can I create such a model component,…
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
0
votes
3 answers

How to get value of Last cell in JTable?

I have a query ... code is running fine, but am not able to get value of last cell of last row and last column. Below is the code... pls guide with this code am adding rows dynamically to JTable : if(e.getSource()==addb) { …
shounak
  • 51
  • 2
  • 8
0
votes
1 answer

Change TableModel structure

so the scenario is that I've got a JTable with a number of JComboBox's as cells. On the selection of an element of a JComboBox, there needs to be a change in the structure of the Table Model. I've also got an 'output table' below which listens to…
TheRealJimShady
  • 3,815
  • 4
  • 21
  • 40
0
votes
1 answer

Using java swing table to update MySQL database values

This program is used to read data from database. In the database,there are three tables pki17, pki18, pkn18. For viewing different tables,JComboBox is used and it works by changing the TableModel of the table. Also by using tableChanged method I…
user2064898
  • 3
  • 1
  • 2
0
votes
1 answer

Java TableModel : Adding a row dynamically by clicking on the last row

So I am trying to implement a dynamically editable list and I want to add a row dynamically when I click on the last row or if I edit the last row. I know how to add the row although I would like to know how to implement the actionlistener. Help…
Rohit Deshmukh
  • 381
  • 2
  • 8
  • 21
0
votes
2 answers

how to Merge Two Jtable into Third Jtable?

I have two JTable having same header name and number of columns. Now I want to create a third JTable containing all values of this two JTables when i click on merge Button. Please suggest me any answer.
user2042166
  • 121
  • 1
  • 1
  • 6
0
votes
2 answers

Unable to populate SummaryTable with results

private void myProfileTabStateChanged(javax.swing.event.ChangeEvent evt) { if (myProfileTab.getSelectedComponent() == EditProfile) { editProfile(); } else if (SearchAcademic ==…
MooHa
  • 819
  • 3
  • 11
  • 23
0
votes
1 answer

JTable display list of events under one date

I want to post a question about something I wish to do particularly but have no idea how to approach this as I am new with JTables. This is what I have atm: But I would like to arrange the table like this: So that under a particular date, all…
MooHa
  • 819
  • 3
  • 11
  • 23
0
votes
1 answer

Setting JTable Column width from another class

public TableModel getTableData() { TableModel model=null; try { String sql = "SELECT eventID as 'Event ID', date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event"; pst = conn.prepareStatement(sql); …
MooHa
  • 819
  • 3
  • 11
  • 23
0
votes
3 answers

Recreating a JTable when updating content

I have a application that uses a JTable in it. when I add something to the database it comes into database but I am not able to recreate the JTable somehow.. I have tried to repaint(); the method that creates my table I have tried the Revalidate();…
Reshad
  • 2,570
  • 8
  • 45
  • 86
0
votes
2 answers

Removing all the Rows then reloading rows in a JTable is causing the getSelectedRow to return a -1

When using some software I have created that has a gui with a JTable with the DefualtTableModel called validAcTableModel, When I initilize the validAcTable this is the logic I am using: ListSelectionModel cellSelectModel =…
yams
  • 942
  • 6
  • 27
  • 60
0
votes
1 answer

I'm able to update the TableModel using setValueAt() but changes are not visible in the Table

I'm able to update the TableModel using setValueAt() but changes are not visible in the Table below is the code: import javax.swing.JFrame; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JScrollPane; …
Shivang Doshi
  • 245
  • 1
  • 8
  • 19