dimanche 11 mars 2018

Use the Observer pattern to display different content in a JPanel when user clicks on a JButton

picture of the gui <---

What I want to do with my code:

When the user clicks on a JButton ('single player' or 'options'), the right JPanel will change its content depending on which button the user clicks on. I wanna take advantage of the Observer pattern where the PanelBoss is the subject who "tells" the observer (Gui) when the user has clicked on a button. The Gui will then change content of the right JPanel. I can run the code but when I click on a JButton I get the following error:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Gui.lambda$makeMenu$0(Gui.java:43)".

Does someone know what I've done wrong? What about my update() method?

//PanelBoss class:

public class PanelBoss extends Observable {

private String panel;

public void changePanelContent(String p){
    panel = p;
    setChanged();
    notifyObservers(panel);
}

//Gui class: public class Gui extends JFrame implements Observer {

private JFrame frame;
private JPanel buttonPane;
private JPanel pane;

private JButton singleB;
private JButton optionsB;

private JPanel singlePane;
private JPanel optionsPane;

PanelBoss pb;

    public Gui(PanelBoss pb){
        pb.addObserver(this);
        makeMenu();
    }

    public void makeMenu(){

        //buttons
        buttonPane = new JPanel();
        singleB = new JButton("Single Player");
        optionsB = new JButton("Options");

        //Put the button in buttonPane
        buttonPane.add(singleB);
        buttonPane.add(optionsB);
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.Y_AXIS));
        buttonPane.setBackground(Color.BLACK);

        //Right panel that will show the contents
        pane = new JPanel();
        singlePane = new JPanel();
        singlePane.setBackground(Color.RED);
        singlePane.setVisible(false);
        optionsPane = new JPanel();
        optionsPane.setVisible(false);
        optionsPane.setBackground(Color.GREEN);
        pane.add(singlePane);
        pane.add(optionsPane);


        //The base window.
        frame = new JFrame("title");
        frame.setSize(471, 298);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().add(buttonPane, BorderLayout.WEST);
        frame.getContentPane().add(pane, BorderLayout.CENTER);

        //actionListeners
        singleB.addActionListener(e -> pb.changePanelContent("singlePane"));
        optionsB.addActionListener(e -> pb.changePanelContent("optionsPane"));
    }

public void update(Observable obs, Object o){
    String showPanel = new String((String)o);
    String a = new String("singlePane");
    String b = new String("optionsPane");

    if(showPanel.equals(a)) {
        optionsPane.setVisible(false);
        singlePane.setVisible(true);
    }
    if(showPanel.equals(b)){
        singlePane.setVisible(false);
        optionsPane.setVisible(true);
    }
}
        public static void main(String[] args){
            PanelBoss pb = new PanelBoss();
            Gui gui = new Gui(pb);

Aucun commentaire:

Enregistrer un commentaire