0

My program is meant to display 4 buttons each with a number, then depending on the button pressed the next screen will show a different number of JTextFields, but when any of the buttons are pressed the next panel is blank? Any help and suggestions are greatly appreciated thanks.

public class TestGui {

  static int playersSelected = 0;

    public static void main(String[] args) {
    JFrame gameWindow = new JFrame("Trivial Pursuit");
    gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gameWindow.setSize(800, 650);
    gameWindow.setVisible(true);

    JPanel mainMenu = new JPanel();

    //create layout for main menu
    mainMenu.setLayout(new FlowLayout(FlowLayout.CENTER));

    //add the contents of the main menu panel to the application window
    gameWindow.add(mainMenu);

    JButton[] playerButton = new JButton[4];
    for (int i = 0; i < playerButton.length; i++) {
        final int FinalI = i;
        playerButton[i] = new JButton("Button " + i);
        mainMenu.add(playerButton[i]);
        playerButton[i].setOpaque(true);
        playerButton[i].setPreferredSize(new Dimension(271, 123));
        playerButton[i].setContentAreaFilled(true);
        playerButton[i].setBorder(BorderFactory.createEmptyBorder());

        playerButton[i].addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                playerButton[FinalI].setBorder(BorderFactory.createLoweredBevelBorder());
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                playerButton[FinalI].setBorder(BorderFactory.createEmptyBorder());
            }
        });
    }

    JLabel[] playerNumberText = new JLabel[5];
    JTextField[] createPlayers = new JTextField[5];

    JPanel createPlayersPanel = new JPanel();
    createPlayersPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    createPlayersPanel.setVisible(true);

    for (int i = 1; i <= playersSelected; i++) {
        final int finalI = i;
        createPlayers[i] = new JTextField("Player " + i + " input name here");
        createPlayers[i].setPreferredSize(new Dimension(160, 20));

        playerNumberText[i] = new JLabel();
        playerNumberText[i].setText("Player " + i);
        playerNumberText[i].setPreferredSize(new Dimension(350, 100));
        playerNumberText[i].setHorizontalAlignment(SwingConstants.CENTER);
        createPlayersPanel.add(playerNumberText[i]);

        createPlayers[i].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (createPlayers[finalI].getText().equals("Player " + finalI + " input name here")) {
                    createPlayers[finalI].setText("");
                }
            }
        });
    }

    for (int i = 0; i < playerButton.length; i++) {
        final int finalI = i;
        playerButton[i].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                gameWindow.remove(mainMenu);
                gameWindow.add(createPlayersPanel);
                switch (finalI) {
                    case 0:

                        playersSelected = 2;
                        break;
                    case 1:

                        playersSelected = 3;

                        break;
                    case 2:

                        playersSelected = 4;

                        break;
                    case 3:

                        break;
                }
                gameWindow.revalidate();
                gameWindow.repaint();
                System.out.println(playersSelected);
            }
          });
      }
  }
}
tom penn
  • 171
  • 10

1 Answers1

1

At creation time, your createPlayersPanel JPanel is empty since playerSelected is 0.

In your action listener, this panel is never updated, so it remains an empty panel.

What you could do is create a method to dynamically build the panel instead of building it only on startup. As playerSelected is only used here, you can remove this static variable and pass the value directly from your action listener.

    private static JPanel buildPlayersPanel(int playerSelected) { 

        JPanel createPlayersPanel = new JPanel();
        createPlayersPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        createPlayersPanel.setVisible(true);

        for (int i = 1; i <= playersSelected; i++) {
            final int finalI = i;
            createPlayers[i] = new JTextField("Player " + i + " input name here");
            createPlayers[i].setPreferredSize(new Dimension(160, 20));

            playerNumberText[i] = new JLabel();
            playerNumberText[i].setText("Player " + i);
            playerNumberText[i].setPreferredSize(new Dimension(350, 100));
            playerNumberText[i].setHorizontalAlignment(SwingConstants.CENTER);
            createPlayersPanel.add(playerNumberText[i]);
            createPlayersPanel.add(createPlayers[i]);

            createPlayers[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (createPlayers[finalI].getText().equals("Player " + finalI + " input name here")) {
                        createPlayers[finalI].setText("");
                    }
                }
            });
        }

   return createPlayersPanel;

}

And inside the action listener :

            @Override
            public void actionPerformed(ActionEvent ae) {

                int playersSelected  = 0;

                switch (finalI) {
                    case 0:

                        playersSelected = 2;
                        break;
                    case 1:

                        playersSelected = 3;

                        break;
                    case 2:

                        playersSelected = 4;

                        break;
                    case 3:

                        break;
                }
                gameWindow.remove(mainMenu);
                gameWindow.add(buildPlayersPanel(playersSelected));
                gameWindow.revalidate();
                gameWindow.repaint();
                System.out.println(playersSelected);
            }
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • `gameWindow.add(buildPlayersPanel(playersSelected));` This line of code is throwing the error 'non-static method buildPlayersPanel(int) cannot be referenced from a static context' – tom penn Mar 02 '17 at 15:59
  • Oh your calls are static, I missed that. Make the method static in that case (see my last edit). Note that you should avoid using so many static things, but for the sake of the example it will do :) – Arnaud Mar 02 '17 at 16:04
  • also the JTextField isn't appearing, the JLabel is though. Yeah i was only ever taught using static so i'm unsure of any other ways – tom penn Mar 02 '17 at 16:17
  • Indeed it seems that the `JTextField` s in `createPlayers` were never added, just edited my answer. – Arnaud Mar 02 '17 at 16:29
  • Thank you. One last thing, is having global static objects bad practice? as the JTextFields will be Used to setup Player Objects in my actual program – tom penn Mar 02 '17 at 16:35
  • See this question, you will have plenty of good answers about `static` : http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – Arnaud Mar 02 '17 at 16:39
  • `gameWindow.add(buildPlayersPanel(playersSelected));` i want to remove the panel when the method returns but im unsure on how to do this – tom penn Mar 03 '17 at 11:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137145/discussion-between-tom-penn-and-berger). – tom penn Mar 03 '17 at 12:03