-1

I need to add buttons when the amount of buttons to add is calculated.

I have my button creating code hereish..:

private void loadButtons()
{
    if (active_puzzle != null)
    {
        int devider = 5;
        int count = 0;
        JButton puzzleButton[] = new JButton[active_puzzle.getNumberOfPieces()];
        for(int row = 0; row < active_puzzle.getRows(); row++)
        {
            for(int column = 0; column < active_puzzle.getColumns(); column++)
            {
                puzzleButton[count] = new JButton(new ImageIcon( active_puzzle.getPieces()[count].getPieceImage() ) );
            }
        }
    }
}

Now how to I tell the program that new buttons need to be added to the screen?

Thank you

visc
  • 4,794
  • 6
  • 32
  • 58
  • 1
    Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. For tips on dynamically adding components, see the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556). It adds labels dynamically on button click. – Andrew Thompson Oct 28 '13 at 15:19
  • Can you provide the full class code? you have to first add them to a JFrame, Panel or something like that and then probably refresh the UI after adding the buttons so they will be displayd. – JBA Oct 28 '13 at 15:20

2 Answers2

4

The basic code for adding a component to a visible GUI is:

panel.add(...);
panel.revalidate();
panel.repaint();

then the layout manager can do its job.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Add all of the newly created buttons which you stored in the puzzleButton array to the main JPanel of your program.

Evgheni Crujcov
  • 470
  • 2
  • 8