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.
Asked
Active
Viewed 140 times
0
-
1What relationship does each of the tables have with each other? If they are modeling the same data, don't you just need multiple instance of the same model (or the same instance depending on your needs). Have you looked at `DefaultTableModel`? *"The goal is reusability and to follow good coding practices while learning."* - Then I'd avoid `static` references to anything – MadProgrammer Jun 17 '15 at 03:19
-
In this simple [example](http://stackoverflow.com/a/9134371/230513), the column names derive form the enclosed data structure: `Map
`. – trashgod Jun 17 '15 at 10:42 -
@trashgod thank you for that. The link provided to mKorbel's example helped me a lot. – Grim Jun 17 '15 at 13:05
-
@MadProgrammer the data does change but very little between two of the tableModels. – Grim Jun 17 '15 at 13:07
-
@user3282568: If the use case warrants, nothing prevents you from creating an abstract subclass of `AbstractTableModel` that you can further extend to create concrete subclasses. – trashgod Jun 17 '15 at 14:19
-
@trashgod I am going to do it that way. Thank You – Grim Jun 17 '15 at 18:53
-
Excellent; please consider [answering your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod Jun 17 '15 at 22:27
1 Answers
0
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);
}
}

Grim
- 2,398
- 4
- 35
- 54