2 Replies Latest reply: Oct 7, 2004 3:33 PM by 807587 RSS

    Problem updaing a GridLayout JPanel

    807587
      Problem updaing a GridLayout JPanel

      What I want to do in the following method is very straight forward. First, I will remove all components inside the namesPanel, then reset the layout and add new things back to the panel. However, now it does not clean up things. For example, if I set numberOfPlayers to 10, and call this method, 10 users info will be shown. Then if I set numberOfPlayers to 2, more than 2 users' info will be shown! Some info from the previous 10 users will be left behind. Is there a better way to clean up things?
      Also, if I call this method right after I add the namesPanel to its parent panel, which will be added to the contentPanel of JFrame, it won't work, but it will work if I call it after that through the actionPerformed method. The value of numberOfPlayers is set by a JComboBox. When a user selects an item, numberOfPlayers will be changed, and the populateNames() will be called through the actionPerformed method. Thank you in advance.
      public void populateNames(){
               namesPanel.removeAll();
               jf.pack();
               namesPanel.setLayout(new GridLayout(numberOfPlayers+1, 2));
               JTextField[] jt_playerNames = new JTextField[numberOfPlayers];
               playerNames = new String[numberOfPlayers];
              for(int i=0;i<numberOfPlayers;i++){
                   namesPanel.add(new JLabel("Player name:"));
                  jt_playerNames[i] = new JTextField("Player " + (i+1));
                  playerNames[i] = "Player " + (i+1);
                  namesPanel.add(jt_playerNames);
      }
      }