I am trying to create one AbstractTableModel instead of three. The application uses the MVC architecture so the controller would update the model. I am a bit confused on how to do the column names, creation of an empty list, and setValueAt method. Should columns names and the data be a static ArrayList? The setValueAt method is currently done using a switch statement but could it be done easier with an ArrayList of arrays? The controller could call a method from the tableModel to empty the list and then the method would do a fireTableDataChanged(). The goal is reusability and to follow good coding practices while learning.
I decided to do two tables and I was able to combine two tables.
package model;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import model.common.Order;
import model.common.Product;
public class OrderTableModel extends AbstractTableModel {
private List<Order> orderList;
private String[] columnNames = { "Product ID", "Description",
"Supplier ID", "Order Quantity", "Unit of Measure", "Total Cost",
"Order Date" };
public OrderTableModel(ArrayList<Order> orderList) {
this.orderList = orderList;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "<html>Product ID</html>";
case 1:
return "<html>Description</html>";
case 2:
return "<html>Supplier ID</html>";
case 3:
return "<html>Order<br></br>Quantity</html>";
case 4:
return "<html>Unit of<br></br>Measure</html>";
case 5:
return "<html>Total<br></br>Cost</html>";
case 6:
return "<html>Order<br></br>Date</html>";
default:
return null;
}
}
@Override
public int getRowCount() {
return orderList.size();
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
Order order = (Order) orderList.get(row);
switch (column) {
case 0:
return order.getProduct().getId();
case 1:
return order.getProduct().getProductDescription();
case 2:
return order.getSupplier().getId();
case 3:
return order.getProduct().getOrderQty();
case 4:
return order.getProduct().getUOM();
case 5:
return order.getTotalCost();
case 6:
return order.getDate();
}
}
}
The other tablemodel.
package model;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.common.Product;
public class ProductTableModel extends AbstractTableModel {
private List<Product> productList;
private String[] columnNames = { "ID", "Description", "Inventory",
"Minimum Quantity", "Cost", "Order Quantity" };
public ProductTableModel() {
productList = new ArrayList<Product>();
}
public ProductTableModel(ArrayList<Product> productList) {
this.productList = productList;
}
public void resetTable() {
productList.clear();
fireTableDataChanged();
}
public void createEmptyTable() {
while (productList.size() <= 12) {
productList.add(new Product("", "", 0, 0, 0, 0));
}
}
public void addRow(Product product) {
productList.add(product);
fireTableRowsInserted(productList.size() - 1, productList.size() - 1);
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "<html>ID<br></html>";
case 1:
return "<html>Description<br></html>";
case 2:
return "<html>Inventory<br></html>";
case 3:
return "<html>Minimum<br>Quantity</html>";
case 4:
return "<html>Cost<br></html>";
case 5:
return "<html>Order<br>Quantity</html>";
default:
return null;
}
}
@Override
public int getRowCount() {
return productList.size();
}
@Override
public boolean isCellEditable(int row, int column) {
return true;
}
@Override
public Object getValueAt(int row, int column) {
Product product = productList.get(row);
switch (column) {
case 0:
return product.getId();
case 1:
return product.getProductDescription();
case 2:
return product.getQtyOnHand();
case 3:
return product.getMinQty();
case 4:
return product.getCost();
case 5:
return product.getOrderQty();
default:
throw new IndexOutOfBoundsException();
}
}
@Override
public void setValueAt(Object value, int row, int column) {
Product product = productList.get(row);
switch (column) {
case 0:
product.setId((String) value);
break;
case 1:
product.setProductDescription((String) value);
break;
case 2:
product.setQtyOnHand((int) value);
break;
case 3:
product.setMinQty((int) value);
break;
case 4:
product.setCost((double) value);
break;
case 5:
product.setOrderQty((int) value);
break;
default:
throw new IndexOutOfBoundsException();
}
fireTableCellUpdated(row, column);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.