Question

I have two classes (lay and panelTwo). In "lay" I have my MainMethod and this is where I've put my CardLayout. What I'm trying to do is access that class within another outside class. But I can't seem to access it whilst trying to write my ActionListener:

 public class lay {

JFrame frame;
JPanel panelCont;
JPanel panelOne;
JButton buttonOne;
CardLayout cards;

PanelTwo panelTwo;

public lay() {
    frame = new JFrame("Start");
    panelCont = new JPanel();
    panelOne = new JPanel();
    panelTwo = new PanelTwo();
    buttonOne = new JButton("Got to two");
    cards = new CardLayout();

    panelCont.setLayout(cards);

    panelOne.add(buttonOne);
    panelOne.setBackground(Color.BLUE);

    panelCont.add(panelOne, "1");
    panelCont.add(panelTwo, "2");
    cards.show(panelCont, "1");

    buttonOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cards.show(panelCont, "2");
        }
    });

    frame.add(panelCont);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new lay();
        }
    });
}

}

And here is my other class where the problem is:

public class PanelTwo extends JPanel {

JButton buttonTwo;

public PanelTwo() {

    buttonTwo = new JButton("Go to one");

    add(buttonTwo);
    setBackground(Color.GREEN);

    buttonTwo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cards.show(panelCont, "1");
        }
    });


    }

}

As you can probably tell, I get errors in "cards.show(panelCont, "1")". And I do understand why that is. I just don't know what the solution is.

Was it helpful?

Solution

Do they need to be in separate files? You can make PanelTwo an inner class of lay class. You're getting the error, because the tow variables are not in the scope of the lay class which those are members of. If you did the below, errors would go away.

public class lay {
   ...
   private class PanelTwo {

   }
}

EDIT

What you need to do using two separate class files is to create a constructor in the PanelTwo where you take in arguments of your CardLayout and the JPanel from the lay class. Then instantiate the PanelTwo with those two arguments.

Try this out. I passed the CardLayout and the JPanel to a constructor of PanelTwo. Works fine.

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class lay {

    JFrame frame;
    JPanel panelCont;
    JPanel panelOne;
    JButton buttonOne;
    CardLayout cards;

    PanelTwo panelTwo;

    public lay() {
        frame = new JFrame("Start");
        panelCont = new JPanel();
        panelOne = new JPanel();
        cards = new CardLayout();
        panelTwo = new PanelTwo(cards, panelCont);
        buttonOne = new JButton("Got to two");


        panelCont.setLayout(cards);

        panelOne.add(buttonOne);
        panelOne.setBackground(Color.BLUE);

        panelCont.add(panelOne, "1");
        panelCont.add(panelTwo, "2");
        cards.show(panelCont, "1");

        buttonOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cards.show(panelCont, "2");
            }
        });

        frame.add(panelCont);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new lay();
            }
        });
    }

}

class PanelTwo extends JPanel {

    JButton buttonTwo;
    CardLayout layout;
    JPanel panelCont;

    public PanelTwo(final CardLayout layout, final JPanel panelCont) {
        this.layout = layout;
        this.panelCont = panelCont;
        buttonTwo = new JButton("Go to one");

        add(buttonTwo);
        setBackground(Color.GREEN);

        buttonTwo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                layout.show(panelCont, "1");
            }
        });

    }
}

I also initialized the CardLayout in the lay constructor before initializing the PanelTwo. This avoids a NullPointerException

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top