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

Qt TableModel with not ready data

So, I have QTableView in my dialog, subclassed QAbstractTableModel, and a list of URLs of images to show using Qt::DecorationRole. Data called by reimplemented MyModel::data(const QModelIndex &index, int role) const method. I can load an image using…
While True
  • 423
  • 2
  • 15
0
votes
2 answers

Java Swing JTable: cannot access the values in the table due to incorrect usage/creation of table model

I am just beginning to program using Java Swing. I am not very familiar with each of the many Java Swing Classes, and have ran into a stumbling block regarding the creation of a Table. I am trying to figure out how to add a table to a Java Swing…
fudge22it
  • 103
  • 1
  • 2
  • 13
0
votes
1 answer

JTable reading text files from second line

I'm working on JTable that reads text from a .txt file. the txt file gets updated dynamically after 3 sec. Now when I run the application, everything is good except that the output of .txt files comes in JTable from the second line. The first line…
Ingila Ejaz
  • 399
  • 7
  • 25
0
votes
1 answer

reading file and sending data from the file to a JTable

public void showTotalSummary() { JFrame totalSummary = new JFrame("Total Leave Credit Summary"); totalSummary.setSize(970, 523); totalSummary.setResizable(false); …
Alex Mikki
  • 9
  • 1
  • 4
0
votes
2 answers

Tooltips on filtered rows in a table

I have a JTable and any single row in it has associated a different tooltip when mouse hover a row. I have created a "filter" for this table; when it is applied it perfectly hides the rows need to be hidden but when I hover the mouse on the…
Randomize
  • 8,651
  • 18
  • 78
  • 133
0
votes
2 answers

Pattern for updating JTable on guava events thread safe

Preliminary question How can I build a JTable so that it would be updated when a Google Guava Event is fired (And guarantee it's thread safe)? The simple way would be to do model.setValueAt(aValue, row, column); Two problems: using the standard…
kotoko
  • 599
  • 2
  • 6
  • 22
0
votes
2 answers

How to fire ValueChangeEvent for table?

There are two listeners: table.addListener(new ItemClickListener() { public void itemClick(ItemClickEvent event) { // fireEvent(...); } }); table.addListener(new Table.ValueChangeListener() { public void valueChange(final…
user1134181
0
votes
1 answer

How to properly fill data in a table when extending from AbstractTableModel

I am getting an array of class object (named: store). I have to retrive some values from the store array and want to fill my JTable (Object[][] data) with those values. I have passed this array into a class in which i am planning to draw my user…
mu_sa
  • 2,685
  • 10
  • 39
  • 58
0
votes
1 answer

jcheckbox in jtable is doing nothing on clicking it, please help needfull

My table model is as follows. The first coloumn of my table is checkbox. I am able to put checkbox in jtable but when I cliked on that checkbox it does nothing. I used the DefaultTableCellRenderer to put checkbox in my table. public class…
0
votes
1 answer

Is there a neater way to fill a JTable with a web page's attributes retrieved from XPath?

I'm creating a a web page scraper. I'm attempting to fill a JTable from data on the page, retrieved using XPath. I want the data for a single row put into an array. {"Name","Phone","Address","City","State","Postal Code","Link"} I retrieve the…
dead beef
  • 673
  • 2
  • 6
  • 20
0
votes
1 answer

Converting selected items on a Table model to an object

I have Components I have created that are being put into a table model with two columns as below. if (!newAcList.isEmpty()) { for (Acronym acc : newAcList) { tableModel.addRow(new String[]{acc.getName(), acc.getDefinition()}); …
yams
  • 942
  • 6
  • 27
  • 60
0
votes
2 answers

JTable Selection and TableModel

I am facing an issue with JTable and the TableModel associated with it. The problem here is that let's say if I make a row/rows selections on my JTable, I would like to get the particular row object from the TableModel and pass it somewhere. Does…
Bytekoder
  • 192
  • 1
  • 7
  • 23
0
votes
2 answers

String path displayed when adding Image in JTable

I tried using setValueAt in adding an image but the problem is it prints the string and does not load the image. any help on this. the code is below int selectedColumn = table1.getSelectedColumn(); int selectedRow =…
0
votes
2 answers

Button in Cell of JTable: Keep Caption constant while storing data in the cell

I am working on a JTable that has one column that contains a List>. This column should show a button to the user. When he clicks on the Button, something is to be done with the data of the cell. All that works really fine with the…
Skrodde
  • 41
  • 10
0
votes
1 answer

How to add new row in jtable while click a button

Possible Duplicate: Adding rows to a JTable I am creating a jTable. Initially it has no rows.If we click "add" button a new row will insert.Is it possible to do this in a jTable?
Siddhu
  • 1,188
  • 2
  • 14
  • 24