Home > Mobile >  Displaying input on JTable
Displaying input on JTable

Time:12-03

I want to display the data on JTable for every button press. Before that, the button will create and store data to the List and should display the data on the table. But, only the creation and data storing is working and the data is not displayed. What should I do to display the data on the table.

This is the abstract model I made to fill my table.

import javax.swing.JList;
import javax.swing.table.AbstractTableModel;
import com.main.Products;
import java.util.List;

public class CartTableModel extends AbstractTableModel{
    List<Products> productList;
    
    private final String[] columnNames = new String[] {
            "Product:", "ID:", "Variant:", "Size:","Unit Price:","Quantity:","Unit Total:"
    };
    private Class[] columnClass = new Class[] {
        String.class, Integer.class, String.class, String.class, Double.class, Integer.class, Double.class
    };
    
    public CartTableModel(List<ProductInfo> productList)
    {
        this.productList = productList;
    }
   public String getColumnName(int column)
    {
        return columnNames[column];
    }
 
    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return columnClass[columnIndex];
    }
 
    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }
 
    @Override
    public int getRowCount()
    {
        return productList.size();
    }
 
    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        Products row = productList.get(rowIndex);
        if(0 == columnIndex) {
            return row.getProductName();
        }
        else if(1 == columnIndex) {
            return row.getProductID();
        }
        else if(2 == columnIndex) {
            return row.getVariant();
        }
        else if(3 == columnIndex) {
            return row.getSize();
        }
        else if(4 == columnIndex){
            return row.getUnitPrice();
        }
        else if(5 == columnIndex){
            return row.getQuantity();
        }
        else if(6 == columnIndex){
            return row.getTotal();
        }
        return null;
    }
    
     @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return true;
    }
 
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        Products row = productList.get(rowIndex);
        if(0 == columnIndex) {
            row.setProductName((String) aValue);
        }
        else if(1 == columnIndex) {
            row.setProductID((int) aValue);
        }
        else if(2 == columnIndex) {
            row.setVariant((String) aValue);
        }
        else if(3 == columnIndex) {
            row.setSize((String) aValue);
        }
        else if(4 == columnIndex){
            row.setUnitPrice((double) aValue);
        }
        else if(5 == columnIndex){
            row.setQuantity((int) aValue);
        }
    }
    
}

This is how the JTable is created:

CartTableModel model = new CartTableModel(productList);
JTable cartTable = new JTable(model);

Also, This is how the data is created:

public void actionPerformed(ActionEvent e) {
        if(e.getSource() == coconutPieBtn){
            cart.addProduct(new Product("Coconut Creamy Pie", 101,"Pastry","Med",79.99));
            
        }
    }

CodePudding user response:

fireTableCellUpdated(productList.size(), columnNames.length)

The row/column values you specify are wrong for two reasons:

  1. you always use the same value no matter what cell you update and
  2. java indexes are 0 based, so you are referring to a row/column that doesn't exist.

Just use:

fireTableCellUpdated(rowIndex, columnIndex)

Also:

cart.addProduct(new Product("Coconut Creamy Pie", 101,"Pastry","Med",79.99));

is wrong. Once the model is created you need to add the new Product to the CartTableModel, NOT the List.

So you need to create a method in your CartTableModel to dynamically add products.

The basic code would be:

public void addProduct(Product product)
{
    insertProduct(getRowCount(), product);
}
 
public void insertProduct(int row, Product product)
{
    products.add(row, product);
    fireTableRowsInserted(row, row);
}

Check out Row Table Model for a complete example of creating your own custom TableModel with methods for dynamically updating the model.

  • Related