Search code examples
javaswingnetbeansjbutton

How do I add JButtons dynamically in Netbeans?


I am creating a simple game where i want a grid type structure of JButtons. I want their size to be increasing as the level of the game, but I am unable to find the solution. Is it possible with just drag and drop?


Solution

  • You need to put your buttons in a matrix and create buttons as your program runs. Since you haven't supplied many details, I would suggest an array list of array lists, so that you can (theoretically) add as many buttons as you wish.

    Since you haven't provided a very good description of your requirements, I can only say that you'd create buttons and assign them to the appropriate array list, referring to each by its coordinate.

    // assuming you want a reference to the button at x, y coords
    // also assuming that your "matrix" is a column array list holding row array lists
    
    myButton = columnList.get(y).get(x);
    

    Start with that idea and see where the code takes you