0

I am using JFrame(swing) and I want to access an ArrayList created in one JFrame form into another JFrame form. I created ArrayList here

public class addEmployee extends javax.swing.JFrame {
    public static ArrayList<EmployeeClass> empList;}

public addEmployee() {
        initComponents();
        empList = new ArrayList<>();}

I want to use it here

public class payroll extends javax.swing.JFrame {

    /**
     * Creates new form payroll
     */
    public payroll() {
        initComponents();
        ArrayList<EmployeeClass> empList = addEmployee.empList;
    }

It is giving me error when I try to iterate it in the second frame

for(int i = 0 ; i<empList.size() ; i++){
            
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Your `addEmployee ` class (which *should* be named `AddEmployee`), shouldn't create and display a new JFrame but rather a modal dialog window such as a JOptionPane or a modal JDialog. This way, the calling code (the main JFrame) can be put on hold until the dialog has been dealt with (is no longer visible) and then the dialog's data can be ***passed into the calling code***, and that last bit is key. You're creating a new ArrayList in both locations, which doesn't make sense since only one holds the data that you want. You need to *pass* that array list to where it is needed. – Hovercraft Full Of Eels Aug 08 '21 at 18:57
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Aug 16 '21 at 08:51

0 Answers0