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++){
/* 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);
                pro = s.nextLine();
            words = pro.split(",");
                for (int i = 0; i < words.length; i++)
            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;

Recommended Answers

All 2 Replies

Well, it's not strange that you only get the last item since each call to getItems() will loop through the whole list and end up with the last element each time.

Depending on the layout of your program (didn't read all of your source code, only the getItems() method) you can either have a global variable to keep track of which index to get from the list or change getItems() to something like

getItems(int n)

and return the n:th element of the array, still there's a need to keep track of the current index.

Good luck

This worked just fine, thanks for the advice anyway.

public void getItems() {
        Product p = new Product();
        Scanner s = new Scanner(System.in);
        ArrayList <Product> aList = p.getArrList();
            for(int i = 0; i < aList.size(); i++){
            jLabel10.setText("Press Enter to Continue.");
            JOptionPane.showInternalConfirmDialog(mainPanel, "");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.