Home > Back-end >  Java ArrayList returning 0 value
Java ArrayList returning 0 value

Time:04-20

I'm trying to build an application on java with swing that the user need to enter some product and store them in an arraylist and show the list of products on table (JTable) in another Jframe , the problem i face that when i'm adding the product and click on button of liste the table does not contain the products even the Arraylist size return 0

Class Product

package Inventory;

public class Produit {
    private int code;
    private int StockInitial;
    private int StockEntree;
    private int StockSortie;
    private int StockReel;

    public Produit(int code, int StockInitial, int StockEntree, int StockSortie) {
        this.code = code;
        this.StockInitial = StockInitial;
        this.StockEntree = StockEntree;
        this.StockSortie = StockSortie;
        this.StockReel = (this.StockInitial this.StockEntree)-this.StockSortie;
    }
    public void AjouterProduit(int code, int StockInitial, int StockEntree, int StockSortie) {
        this.code = code;
        this.StockInitial = StockInitial;
        this.StockEntree = StockEntree;
        this.StockSortie = StockSortie;
        this.StockReel = (this.StockInitial this.StockEntree)-this.StockSortie;
    }

    public Produit() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getStockInitial() {
        return StockInitial;
    }

    public void setStockInitial(int StockInitial) {
        this.StockInitial = StockInitial;
    }

    public int getStockEntree() {
        return StockEntree;
    }

    public void setStockEntree(int StockEntree) {
        this.StockEntree = StockEntree;
    }

    public int getStockSortie() {
        return StockSortie;
    }

    public void setStockSortie(int StockSortie) {
        this.StockSortie = StockSortie;
    }

    public int getStockReel() {
        return this.StockReel;
    }

    public void setStockReel(int StockReel) {
        this.StockReel = StockReel;
    }
}

Class Inventory the jframe that user able to enter details of product

package Inventory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

public class Inventory extends JFrame implements ActionListener {

    public ArrayList<Produit> listProduit = new ArrayList<Produit>();
    JLabel l1 = new JLabel("Code : ");
    JLabel l2 = new JLabel("Stock initial : ");
    JLabel l3 = new JLabel("Stock entree : ");
    JLabel l4 = new JLabel("Stock sortie : ");
    JTextField t1 = new JTextField(20);
    JTextField t2 = new JTextField(20);
    JTextField t3 = new JTextField(20);
    JTextField t4 = new JTextField(20);
    JButton b1 = new JButton("Suivant");
    JButton b2 = new JButton("liste");
    JFrame frame = new JFrame("Inventory");
    public Inventory(){
        frame.setLayout(new FlowLayout());
        frame.setSize(400,300);
        frame.getContentPane().add(l1);
        frame.getContentPane().add(t1);
        frame.getContentPane().add(l2);
        frame.getContentPane().add(t2);
        frame.getContentPane().add(l3);
        frame.getContentPane().add(t3);
        frame.getContentPane().add(l4);
        frame.getContentPane().add(t4);
        frame.getContentPane().add(b1);
        frame.getContentPane().add(b2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b1.addActionListener(this);
        b2.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b1){
            int code = Integer.parseInt(t1.getText());
            int StockInitial = Integer.parseInt(t2.getText());
            int StockEntree = Integer.parseInt(t3.getText());
            int StockSortie = Integer.parseInt(t4.getText());
            Produit p = new Produit(code,StockInitial,StockEntree,StockSortie);
            listProduit.add(p);
        }
        if(e.getSource()==b2) {
            new Liste().frame.setVisible(true);
        }
    }
    public ArrayList<Produit> getList() {
        return listProduit;
    }

    public static void main(String[] args) {
        new Inventory().frame.setVisible(true);
    }
}

Class Inventory the jframe that show to the user the details of product

package Inventory;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class Liste extends JFrame {
    JFrame frame = new JFrame("Inventory");
    String head[] = {"Code","Stock Intial","Stock Entree","Stock Sortie","Sotck Reel"};
    String[][] Rows = {};
    DefaultTableModel model = new DefaultTableModel(Rows,head);
    Inventory n;
    JTable table = new JTable(model);
    JScrollPane sp = new JScrollPane(table);
    Container con = null;
    public Object[] objProduit;
    public void addRowToJTable()
    {

    }
    public Liste(){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
        for(int i=0;i<n.listProduit.size();i  ){
            Object[] obj = {n.listProduit.get(i).getCode(),n.listProduit.get(i).getStockInitial(),n.listProduit.get(i).getStockEntree(),n.listProduit.get(i).getStockSortie(),n.listProduit.get(i).getStockReel()};
            model.addRow(obj);
        }
        //return 0
        System.out.println(n.listProduit.size());
        frame.getContentPane().add(table);
        frame.setLayout(new FlowLayout());
        frame.setSize(400,300);
        frame.add(sp);
        }
}

CodePudding user response:

If you want your Liste class to access the listProduit of the Inventory class you have several possibilities.

In my preferred order:

1. pass the listProduit to the Liste constructor

In the actionPerformed() method, create the Liste instance as follows:

new Liste(listProduit).frame.setVisible(true);

To do this you need to change the Liste constructor as follows:

private List<Produit> listProduit;
public Liste(List<Produit> listProduit) {
    this.listProduit = listProduit;
    model.setRowCount(0);
    for (int i = 0; i < listProduit.size(); i  ) {
        Produit p = listProduit.get(i);
        Object[] obj = { p.getCode(), p.getStockInitial(), p.getStockEntree(), p.getStockSortie(), p.getStockReel() };
        model.addRow(obj);
    }
    System.out.println(n.listProduit.size());
    frame.getContentPane().add(table);
    frame.setLayout(new FlowLayout());
    frame.setSize(400,300);
    frame.add(sp);
}

The field Inventory n in the Liste class is not needed.


2. pass the Inventory instance to the Liste constructor

In the actionPerformed() method, create the Liste instance as follows:

new Liste(this).frame.setVisible(true);

To do this you need to change the Liste constructor as follows:

public Liste(Inventory inventory) {
    this.n = inventory;
    model.setRowCount(0);
    for (int i = 0; i < n.listProduit.size(); i  ) {
        Produit p = n.listProduit.get(i);
        Object[] obj = { p.getCode(), p.getStockInitial(), p.getStockEntree(), p.getStockSortie(), p.getStockReel() };
        model.addRow(obj);
    }
    System.out.println(n.listProduit.size());
    frame.getContentPane().add(table);
    frame.setLayout(new FlowLayout());
    frame.setSize(400,300);
    frame.add(sp);
}

3. make the listProduit a static field

This is only for completeness. You should create as few static fields as possible - static fields are a lazy "solution" that can create big problems if your program grows, and to change from this approach to something better (preferably the first approach) grows harder the more code you have that relies on static fields.

In the class Inventory change the declaration of the field listProduit as follows:

public static List<Produit> listProduit = new ArrayList<Produit>();

You can then change the constructor of Liste as follows:

public Liste(Inventory inventory) {
    model.setRowCount(0);
    for (int i = 0; i < Inventory.listProduit.size(); i  ) {
        Produit p = Inventory.listProduit.get(i);
        Object[] obj = { p.getCode(), p.getStockInitial(), p.getStockEntree(), p.getStockSortie(), p.getStockReel() };
        model.addRow(obj);
    }
    System.out.println(n.listProduit.size());
    frame.getContentPane().add(table);
    frame.setLayout(new FlowLayout());
    frame.setSize(400,300);
    frame.add(sp);
}

You can further improve the constructor by using either an enhanced for loop or by using the forEach() method of the List interface.

With an enhanced for loop:

private List<Produit> listProduit;
public Liste(List<Produit> listProduit) {
    this.listProduit = listProduit;
    model.setRowCount(0);
    for (Produit p: listProduit) {
        Object[] obj = { p.getCode(), p.getStockInitial(), p.getStockEntree(), p.getStockSortie(), p.getStockReel() };
        model.addRow(obj);
    }
    System.out.println(n.listProduit.size());
    frame.getContentPane().add(table);
    frame.setLayout(new FlowLayout());
    frame.setSize(400,300);
    frame.add(sp);
}

With forEach():

private List<Produit> listProduit;
public Liste(List<Produit> listProduit) {
    this.listProduit = listProduit;
    model.setRowCount(0);
    listProduit.forEach(p -> {
        Object[] obj = { p.getCode(), p.getStockInitial(), p.getStockEntree(), p.getStockSortie(), p.getStockReel() };
        model.addRow(obj);
    });
    System.out.println(n.listProduit.size());
    frame.getContentPane().add(table);
    frame.setLayout(new FlowLayout());
    frame.setSize(400,300);
    frame.add(sp);
}
  • Related