I'm trying to create a GUI which will let the user introduce and remove products in a GUI, by using a TreeSet. The user specifies the product name and the quantity, before choosing which operation to perform: add product or remove product.
My question is: how do I print a JTable that can be updated constantly each time the user adds or removes a product?
This is what I have so far. You'll also see the table content I'm planning to display. I don't know if I'm using the TreeSet right, but I'd appreciate any help.
public class Warehouse implements Serializable {
private int nrOfProducts = 0;
private TreeSet<Product> products = new TreeSet<Product>();
private JTable productsTable;
private Object[][] entries = new Object[100][10];
private String[] columnName = {"Product name", "Price", "In stock", "Understock", "Overstock"};
public Warehouse() {
}
public Warehouse(Product product, int nrOfProducts) {
this.nrOfProducts = 0;
this.products = new TreeSet();
}
public void addProduct(Product newProduct) {
products.add(newProduct);
nrOfProducts++;
}
public void removeProduct(Product product) {
products.remove(product);
nrOfProducts--;
}
public int getNrOfProducts() {
return nrOfProducts;
}
public void initFile() {
try {
FileOutputStream fos = new FileOutputStream("products.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(products);
oos.close();
}
catch(Exception exc) {
exc.printStackTrace();
}
}
public void readFile() {
try {
FileInputStream fis = new FileInputStream("products.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Product prod = (Product) ois.readObject();
while (prod != null)
addProduct(prod);
ois.close();
}
catch(Exception exc) {
exc.printStackTrace();
}
}
public void printProducts() {
for (Product p: products) {
// here's where I got stuck
}
}
}
I'll also attach here the Product class:
public class Product implements Comparable {
String productName;
private int price;
private int stock;
private boolean understock;
private boolean overstock;
public Product() {
}
public Product(String productName, int price, boolean understock, boolean overstock, int stock) {
this.productName = productName;
this.price = price;
this.understock = understock;
this.overstock = overstock;
this.stock = stock;
if (stock > 15) {
this.overstock = true;
this.understock = false;
}
else if (stock < 3) {
this.overstock = false;
this.understock = true;
}
else {
this.overstock = false;
this.understock = false;
}
}
public int compareTo(Object obj) {
Product prod = (Product) obj;
return (productName.compareTo(prod.productName));
}
}