0

I am current having a problem with designing an GUI for an assignment. While the project functions at the moment there is a time at the start of the program that I want it to display that the user has to wait for a player so I want the gamePanel to not be visible at first and waitingForPlayerPanel to display "Waiting for other player".

Unfortunately for some reason The waitingForPlayerPanel is not being displayed despite being set to visible in hideGamePanel. I am currently doing this project on Ubuntu 14.04 if that helps.

Here is a snippet of the code I have written.

public class ClientGame extends JFrame implements ActionListener, Runnable {  
    private Socket server = null;
    private BufferedReader in = null;
    private PrintWriter out = null;

    private static final Color[] playerColours = { Color.GREEN, Color.RED };

    private JButton[][] myButtons = new JButton[3][3];
    private JLabel waitingForPlayer = new JLabel("Waiting for other player");
    private JPanel waitingForPlayerPanel = new JPanel();
    private JPanel gamePanel = new JPanel();

    ClientGame() {
        try {
            this.server = new Socket("127.0.0.1", 4444);
            this.in = new BufferedReader(new InputStreamReader(this.server.getInputStream()));
            this.out = new PrintWriter(this.server.getOutputStream(), true);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Alert", 
                    "Couldn't connect to the server.", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
    }

    public void initUI() {
        this.waitingForPlayerPanel.setLayout(new BorderLayout());
        this.waitingForPlayerPanel.setBackground(Color.white);
        this.waitingForPlayerPanel.add("Center", this.waitingForPlayer);
        this.getContentPane().add(this.waitingForPlayerPanel);

        this.gamePanel.setLayout(new GridLayout(3,3));
        this.gamePanel.setSize(450,450);
        this.gamePanel.setBackground(Color.white);
        this.getContentPane().add(this.gamePanel);
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                this.myButtons[i][j] = new JButton();
                this.myButtons[i][j].setPreferredSize(new Dimension(150, 150));
                this.myButtons[i][j].addActionListener(this);
                this.gamePanel.add(this.myButtons[i][j]);
            }
        }

        this.hideGamePanel();

        this.setVisible(true);

        WindowListener l = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        this.addWindowListener(l);
    }

    private void hideGamePanel() {
        this.gamePanel.setVisible(false);
        this.waitingForPlayerPanel.setVisible(true);
        this.pack();
    }

    private void showGamePanel() {
        this.gamePanel.setVisible(true);
        this.waitingForPlayerPanel.setVisible(false);
        this.pack();
    }
    // ...
}
Tom
  • 16,842
  • 17
  • 45
  • 54
PurityLake
  • 1,110
  • 10
  • 18
  • Well I think I'm learning more here than I can instruct but, do you need `this.` on that? Shouldn't you try `super().` – David Pulse Oct 11 '15 at 20:08
  • I fail to see what you are getting at, where should I use `super().`? – PurityLake Oct 11 '15 at 20:10
  • 1
    Use `CardLayout` to switch between panels – Vince Oct 11 '15 at 20:11
  • Pffft, duh, sorry. You've got `this` everywhere lol. On `this.waitingForPlayerPanel().visibility` in hideGamePanel() – David Pulse Oct 11 '15 at 20:12
  • `this.gamePanel.setVisible(false); this.waitingForPlayerPanel.setVisible(true);` Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) instead, as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Oct 11 '15 at 20:24
  • 2
    Good that you moved your answer from the question into a dedicated post. But the word "solved" is not appropriate for a title. "We" don't write stuff like "solved", "open" or "edited" into the title. If your question is solved, then don't forget to accept the answer which helped you most, or your own answer. – Tom Oct 11 '15 at 20:40

1 Answers1

1

Solved the problem use CardLayout

public class ClientGame extends JFrame implements ActionListener, Runnable {  
    private Socket server = null;
    private BufferedReader in = null;
    private PrintWriter out = null;

    // IDs for CardLayout
    private static final String WAITING_PANEL_ID = "waiting";
    private static final String GAME_PANEL_ID = "game";

    private JButton[][] myButtons = new JButton[3][3];
    private JLabel waitingForPlayer = new JLabel("Waiting for other player");
    private JPanel cardPanel = new JPanel();
    private JPanel waitingForPlayerPanel = new JPanel();
    private JPanel gamePanel = new JPanel();
    private int squares = 9;

    protected boolean isTurn = false;

    ClientGame() {
        try {
            this.server = new Socket("127.0.0.1", 4444);
            this.in = new BufferedReader(new InputStreamReader(this.server.getInputStream()));
            this.out = new PrintWriter(this.server.getOutputStream(), true);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Alert", 
                    "Couldn't connect to the server.", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
    }

    public void initUI() {
        this.cardPanel.setLayout(new CardLayout());

        this.waitingForPlayerPanel.setLayout(new BorderLayout());
        this.waitingForPlayerPanel.setBackground(Color.white);
        this.waitingForPlayerPanel.add(BorderLayout.CENTER, this.waitingForPlayer);
        this.cardPanel.add(this.waitingForPlayerPanel, WAITING_PANEL_ID);

        this.gamePanel.setLayout(new GridLayout(3,3));
        this.gamePanel.setSize(450,450);
        this.gamePanel.setBackground(Color.white);
        this.cardPanel.add(this.gamePanel, GAME_PANEL_ID);
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                this.myButtons[i][j] = new JButton();
                this.myButtons[i][j].setPreferredSize(new Dimension(150, 150));
                this.myButtons[i][j].addActionListener(this);
                this.gamePanel.add(this.myButtons[i][j]);
            }
        }

        this.getContentPane().add(this.cardPanel);

        this.hideGamePanel();

        this.setVisible(true);

        WindowListener l = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        this.addWindowListener(l);
    }

    // changes made here
    private void hideGamePanel() {
        CardLayout cl = (CardLayout)this.cardPanel.getLayout();
        cl.show(this.cardPanel, WAITING_PANEL_ID);
        this.pack();
    }

    // changes made here
    private void showGamePanel() {
        CardLayout cl = (CardLayout)this.cardPanel.getLayout();
        cl.show(this.cardPanel, GAME_PANEL_ID);
        this.pack();
    }
}
PurityLake
  • 1,110
  • 10
  • 18