Issue
I have JTable that's supposed to show data that's present in a model that's a custom class that extends DefaultTableModel. It is supposed to start as a blank table, and that works but unfortunately, whenever I try to add new row, it seems like model is not properly assigned to JTable (data doesn't show up on JTable at all).
Here's TableOrdersModel:
public class TableOrdersModel extends AbstractTableModel{
String tableHeaders[] = {"KATEGORIJA", "NAZIV", "CENA", "KOLIČINA"};
private LinkedList<Product> products;
private DefaultTableModel tableModel;
public TableOrdersModel(LinkedList<Product> products) {
this.tableModel = new DefaultTableModel(0, tableHeaders.length);
this.tableModel.setColumnIdentifiers(tableHeaders);
this.products = products;
}
public void addRow(Product product) {
products.add(product);
tableModel.addRow(new Object[]{product.getCategory(), product.getName(), product.getPrice(), product.getQuantity()});
}
@Override
public int getColumnCount() {
return tableHeaders.length;
}
@Override
public String getColumnName(int index) {
return tableHeaders[index];
}
@Override
public void setValueAt(Object value, int row, int col) {
fireTableCellUpdated(row, col);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Product product = products.get(rowIndex);
switch (columnIndex) {
case 0:
value = product.getCategory();
break;
case 1:
value = product.getName();
break;
case 2:
value = product.getPrice();
break;
case 3:
value = product.getQuantity();
break;
default:
System.err.println("ERROR: Invalid column index.");
}
return value;
}
public Product getProductAt(int row) {
return products.get(row);
}
@Override
public int getRowCount() {
return products.size();
}
}
And here's how I'm trying to assign it:
void displayOrdersForThisTable(Table selectedTable) {
LinkedList<Product> products = selectedTable.getOrders();
Iterator<Product> it = products.iterator();
TableOrdersModel tableModel = (TableOrdersModel) table.getModel();
while(it.hasNext()) {
Product productIterator = it.next();
tableModel.addRow(productIterator);
}
Here is how JTable is initialized:
private JTable getTable() {
if (table == null) {
TableOrdersModel tableModel = new TableOrdersModel(new LinkedList<Product>());
table = new JTable(tableModel) {
public boolean editCellAt(int row, int column, java.util.EventObject e) {
return false;
}
};
table.setFont(new Font("Segoe UI", Font.PLAIN, 18));
}
return table;
}
Also, the reason why I'm using a custom model is because of this post (How to set a custom object in a JTable row) where I want to retrieve whole row as a Object (in this case an instance of class Product) instead of having to retrieve each cell from same row and peice it together into a Product.
Any help is greatly appericiated, thanks!
Solution
Your model returns 0 always from its getRowCount()
method, and a JTable made with this model will show no rows of data ever. Instead, if you're overriding DefaultTableModel, then best not to override this method. Let the super's object handle it.
Also, you're trying to use two data nuclei for the model, the super's internal Vector as well as your own linked list of Product, and that's not good. Either use the super's data or extend AbstractTableModel. If you do extend the abstract model, then yes, override getRowCount and return the count of rows in the data, here the size of your linked list.
Answered By - Hovercraft Full Of Eels
Answer Checked By - Pedro (JavaFixing Volunteer)