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

TableModel cannot be cast to javax.swing.table.DefaultTableModel

I want to clear my custom TableModel and get the following exception: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.Test.gui.results.ResultTableModel cannot be cast to javax.swing.table.DefaultTableModel I want to clear…
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
-1
votes
2 answers

JTable values not refreshed after tables is sorted

I have JTable object, filled with data provided from my implementation of AbstractTableModel. I have mouse event listener and when I click on some cell I get its row and column position. With this values I call getValueAt(int row, int column) method…
user3546762
  • 117
  • 1
  • 2
  • 6
-1
votes
4 answers

How to delete multiple rows from beantablemodel in java?

I want to delete multiple rows at a times on selection. Here is my code: int[] indexList = queryTable.getSelectedRows(); queryTableModel.removeRows(indexList); queryTable.clearSelection(); …
user3824693
  • 49
  • 2
  • 8
-1
votes
1 answer

I keep getting nullpointer exception for jtable getrowcount() method

I am trying to create a table that can be filtered but I keep getting a null pointer exception for the getRowCount() method in my table model class. HomePagePanel(where my table is) public class homepagePanel extends JPanel implements ActionListener…
Kerpal
  • 48
  • 2
  • 9
-1
votes
1 answer

After adding a column another column hides in my table

I have a table which contains number of columns. I added a column of CheckBox components by createCell method. that added column should be the first column (the column of index 0). My problem simply when I run the code the that previously first…
PHPFan
  • 756
  • 3
  • 12
  • 46
-1
votes
2 answers

create the HashMap based on JTable

There is JTable with the following content Col1 | Col2 A | 1 A | 2 A | 3 B | 5 B | 1 C | 5 C | 4 C | 2 Based on this table, I need to create a HashMap numbers: column 1 refers to keys and column 2 refers to…
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
-1
votes
1 answer

Java - Reading data using string tokenizer for jTable

I have a file called Items.dat, which looks like this: Item1 subItemA subItemB subItemC Item2 subItemE subItemR subItemT and etc... Using string Tokenizer, how do I input these data into a jTable so Item 1 and Item 2 are headings, and corresponding…
Geuni
  • 93
  • 1
  • 2
  • 12
-1
votes
1 answer

Populate Jtable from mySQL Data

I dont know how and where to start my code. I've tried searching in google how to populate Jtable in netbeans GUI using java code. Well actually, i dont want that all the columns in my MySql database will be diplayed. so i created table model. I…
rheighy
  • 11
  • 2
  • 7
-1
votes
2 answers

How can I import the data in my array list to a JTable?

Possible Duplicate: Is there any way by which I can create a JTable using the data in an ArrayList? The code of my program is as follows: //A loan Amortization example public class LoanTable { public static void main(String[] args) { …
Maddy
  • 167
  • 1
  • 3
  • 12
-1
votes
1 answer

How to display firstrow until lastrow in java?

Possible Duplicate: How to display first row until last row to JR report using Java? i want to call the record in table from first row until last row and display it .. i used this syntax : TableModelEvent tab = new…
-2
votes
1 answer

How to add field in jtable java

How can insert this set of code in jtable. The problem is when I do md.addElement(id); it shows me a red underline on addElement() Here is my code public class hospitalisation extends javax.swing.JFrame { DefaultTableModel md = new…
-2
votes
1 answer

How to implement tablemodel in java?

I am new with JAVA. I have started using JTable. I could not understand how to use tablemodel with JTable. Objective is to understand the use of tablemodel.
Ankit Kumar Singhal
  • 167
  • 1
  • 3
  • 13
-4
votes
2 answers

JTable: getting values from model as opposed to table

DefaultTableModel model2 = (DefaultTableModel) mytable.getModel(); What is the difference between these two? model2.getValueAt(row,column); mytable.getValueAt(row,column);
-4
votes
1 answer

How can I display an Image at the JTable

I have a JTable, with custom DefaultTableModel. I want to insert a ImageIcon into the end column. So I have this code: public class MyTableModelMovimentiContiBancari extends defaultTableModel { static String[] ColName = {…
bircastri
  • 2,169
  • 13
  • 50
  • 119
1 2 3
26
27