I am making a Gui for an inventory. And I want for each time a person presses my next button, the jLabels to display the next item in the arrayList. Right now I'm stuck as it only displays the last element, Any help please?
// action on the button
public void getItems() {
Product p = new Product();
ArrayList <Product> aList = p.getArrList();
for (int i = 0; i < aList.size(); i++){
jLabel6.setText(aList.get(i).getProduct());
jLabel7.setText(aList.get(i).getPrice());
jLabel8.setText(aList.get(i).getQuantity());
jLabel9.setText(aList.get(i).getUPC());
}
}
/* class
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package products;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Product {
private String product;
private String price;
private String quantity;
private String UPC;
static ArrayList <Product> inventory = new ArrayList<Product>();
public Product() {
}
public Product(ArrayList <Product> List){
inventory = List;
}
public Product(String product, String price, String quantity, String UPC) {
this.product = product;
this.price = price;
this.quantity = quantity;
this.UPC = UPC;
}
public ArrayList<Product> getArrList()
{
FileReader fr;
String pro;
String words[];
try {
fr = new FileReader("inventory.csv");
Scanner s = new Scanner(fr);
s.nextLine();
do
{
pro = s.nextLine();
words = pro.split(",");
for (int i = 0; i < words.length; i++)
System.out.println(words[i]);
System.out.println(inventory.size());
inventory.add(new Product(words[0], words[1], words[2], words[3]));
} while (s.hasNext());
} catch (FileNotFoundException ex) {
Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
}
return inventory;
}
public String getUPC() {
return UPC;
}
public String getPrice() {
return price;
}
public String getProduct() {
return product;
}
public String getQuantity() {
return quantity;
}
}