java issue adding / removing panel onclick
Good day,

I have created multiple panels and the first one is showing. I need the panel to be removed and a new one added when the user clicks a next icon. In the code below, the panel reference is not recognized in the action listener. How can I work round this?

        int n=0;
        for (int l=0; l < layOutPanelCount; l++) {
            layOutPanel[l] = new JPanel();
            layOutPanel[l].setLayout(null);
            layOutPanel[l].setBounds(0, 0, screenWidth, screenHeight);
            
            ImageIcon nextIcon = new ImageIcon("src/icons/next.png");
            JLabel nextLabel = new JLabel(nextIcon);
            nextLabel.setBounds(xPos, yPos, 48, 48);
            nextLabel.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e){
                    layOutFrame.remove(layOutPanel[l]);
                    layOutFrame.add(layOutPanel[l + 1]);
                    //Here the problem occurs, the layOutPanel[] is not recognized.
                }
            });
            
            layOutPanel[l].add(nextLabel);
       }
       layOutFrame.add(layOutPanel[1]);
java issue adding / removing panel onclick
===

Good day,

I have created multiple panels and the first one is showing. I need the panel to be removed and a new one added when the user clicks a next icon. In the code below, the panel reference is not recognized in the action listener. How can I work round this?

        int n=0;
        for (int l=0; l < layOutPanelCount; l++) {
            layOutPanel[l] = new JPanel();
            layOutPanel[l].setLayout(null);
            layOutPanel[l].setBounds(0, 0, screenWidth, screenHeight);
            
            ImageIcon nextIcon = new ImageIcon("src/icons/next.png");
            JLabel nextLabel = new JLabel(nextIcon);
            nextLabel.setBounds(xPos, yPos, 48, 48);
            nextLabel.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e){
                    layOutFrame.remove(layOutPanel[l]);
                    layOutFrame.add(layOutPanel[l + 1]);
                    //Here the problem occurs, the layOutPanel[] is not recognized.
                }
            });
            
            layOutPanel[l].add(nextLabel);
       }
       layOutFrame.add(layOutPanel[1]);