简体   繁体   中英

Java - How do I modify static ArrayList in one class with an ActionListener in another class?

I'm trying to modify a static ArrayList, "selectList," in the class "AnimalGUI."

I have to take user input through Java GUI so I implemented JFrame and all that in another class.

I encounter the problem when I pass the ArrayList to an ActionListener that exists in a different class, "HandleGUI." The ActionListener cannot modify the static ArrayList for some reason.

I know it's not working because I tried to print the ArrayList's content after, supposedly, modifying it and the system printed nothing.

Below is the class Animal GUI, nothing special to focus on besides the ArrayList "selectList" at the bottom of the code.

public class AnimalGUI {

public static void main(String[] args) {
    AnimalGUI a = new AnimalGUI();
    HandleGUI h = new HandleGUI();

    h.initialisation();
    a.printSelectList();
}

//Print selectList
void printSelectList(){
    for(int x: selectList){
        System.out.println(x);
    }
}

static ArrayList<Integer> selectList = new ArrayList<Integer>();
static ArrayList<Animal> aniList = new ArrayList<Animal>();
static String[][] Forest = new String[15][15];
}

Below is the subclass "HandleGUI".

You can ignore most of the Java GUI components. The important part here is the ActionListener "selectListener."

public class HandleGUI{

JFrame menu = new JFrame("Startup Menu");
JPanel panelA = new JPanel();
JCheckBox cCB, dCB, fCB, hCB, lCB, tCB, uCB, wCB;
JButton cBT, dBT, fBT, hBT, lBT, tBT, uBT, wBT, start;

//Constructor
public HandleGUI(){

    //Declare all the JCheckBoxes.
    cCB = new JCheckBox("Cat");
    dCB = new JCheckBox("Dog");
    fCB = new JCheckBox("Fox");
    hCB = new JCheckBox("Hippo");
    lCB = new JCheckBox("Lion");
    tCB = new JCheckBox("Tiger");
    uCB = new JCheckBox("Turtle");
    wCB = new JCheckBox("Wolf");

    //Declare all the JButtons.
    cBT = new JButton("Pick an alternative icon.");
    dBT = new JButton("Pick an alternative icon.");
    fBT = new JButton("Pick an alternative icon.");
    hBT = new JButton("Pick an alternative icon.");
    lBT = new JButton("Pick an alternative icon.");
    tBT = new JButton("Pick an alternative icon.");
    uBT = new JButton("Pick an alternative icon.");
    wBT = new JButton("Pick an alternative icon.");

    start = new JButton("Start");

    //Declare JFrame and set its size.
    //Make the startup menu display in centre of monitor.
    menu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    menu.setSize(300, 300);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    menu.setLocation(dim.width/2-menu.getSize().width/2, dim.height/2-menu.getSize().height/2);


}

//Generates a setup menu that allows user to select animals to be included in the simulation. 
public void initialisation(){

    //Make the startup menu display in centre of monitor.
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    menu.setLocation(dim.width/2-menu.getSize().width/2, dim.height/2-menu.getSize().height/2);

    //Set the JPanel to grid layout so elements on both columns align.
    panelA.setLayout(new GridLayout(8, 2, 5, 5));

    //Add the elements into JPanel.
    panelA.add(cCB);
    panelA.add(cBT);
    panelA.add(dCB);
    panelA.add(dBT);
    panelA.add(fCB); 
    panelA.add(fBT);
    panelA.add(hCB);
    panelA.add(hBT);
    panelA.add(lCB);
    panelA.add(lBT);
    panelA.add(tCB);
    panelA.add(tBT);
    panelA.add(uCB);
    panelA.add(uBT);
    panelA.add(wCB);
    panelA.add(wBT);

    //Add the Panel to the Frame.
    menu.getContentPane().add(panelA, BorderLayout.CENTER);
    menu.getContentPane().add(start, BorderLayout.SOUTH);

    //Make Frame visible.
    menu.setVisible(true);

    start.addActionListener(new selectListener());
}

class selectListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        AnimalGUI a = new AnimalGUI();


        if(cCB.isSelected())
            a.selectList.add(0);

        if(dCB.isSelected())
            a.selectList.add(1);

        if(fCB.isSelected())
            a.selectList.add(2);

        if(hCB.isSelected())
            a.selectList.add(3);

        if(lCB.isSelected())
            a.selectList.add(4);

        if(tCB.isSelected())
            a.selectList.add(5);

        if(uCB.isSelected())
            a.selectList.add(6);

        if(wCB.isSelected())
            a.selectList.add(7);

        menu.setVisible(false);
    }
}
}

Looks like conditions in your selectListener are not met. So, the issue is not in static list not modifyable, but that program doesn't enter any if statement:

if(cCB.isSelected())
    a.selectList.add(0);
.....

You are calling static fields from objects.

Instead of

AnimalGUI a = new AnimalGUI(); <...> a.selectList.add(0);

Try AnimalGUI.selectList.add(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM