title: Java add buttons dynamically as an array
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4279435/java-how-would-i-dynamically-add-swing-component-to-gui-on-click">java - How would I dynamically add swing component to gui on click?</a>  </p>
  </blockquote>
  
  
  
  <p>I want to add array of buttons dynamically.
  I tried like this:</p>
  
  <pre><code>this.pack();
      Panel panel = new Panel();
      panel.setLayout(new FlowLayout());
      this.add(panel);
      panel.setVisible(true);
      for (int i = 0; i &lt; Setting.length; i++) {
          for (int j = 0; j &lt; Setting.width; j++) {
              JButton b = new JButton(i+"");
              b.setSize(30, 30);
              b.setLocation(i * 30, j * 30);
              panel.add(b);
              b.setVisible(true);
          }
      }
  </code></pre>
  
  <p>but didn't get anything , what mistake did I make?</p>
  
  <h1>Edit</h1>
  
  <p>I have jFrame class "choices" on it i have a button , when I press the button, this is supposed to happen:</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
          // TODO add your handling code here:
         structure.Setting s = new Setting(8, 8, 3, 1, 1);
         Game g = new Game();
         g.setSetting(s);
         this.dispose();
         g.show();
      }
  </code></pre>
  
  <p>then i go to the Game class (also jFrame class) to the function setSetting and it is like this:</p>
  
  <pre><code>void setSetting(Setting s) {
          this.setting = s;
          structure.Game game = new structure.Game(setting);
          JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4));
          for (int i = 1; i &lt;= 5; i++) {
              for (int j = 1; j &lt;= 5; j++) {
                  JButton b = new JButton(String.valueOf(i));
                  panel.add(b);
              }
          }
          add(panel);
          pack();
          setVisible(true);
      }
      structure.Setting setting;
  }
  </code></pre>