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;
    }
    
    
    
}

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();
        
            jButton1.setVisible(false);
            for(int i = 0; i < aList.size(); i++){
            jLabel6.setText(aList.get(i).getProduct());
            jLabel7.setText(String.valueOf(aList.get(i).getPrice()));
            jLabel8.setText(String.valueOf(aList.get(i).getQuantity()));
            jLabel9.setText(aList.get(i).getUPC());
            jLabel10.setText("Press Enter to Continue.");
            JOptionPane.showInternalConfirmDialog(mainPanel, "");
            }
            jLabel10.setText("");
            jButton1.setVisible(true);
        
        
        
    }
Be a part of the DaniWeb community

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