Dynamic window size and buttons
I have a JPanel and i am creating JButtons dynamically on runtime. I want the JPanel size to be adjusted dynamically according to the number of JButtons added to it. What layout should i use ? And my JPanel will be added to the JFrame so can anyone explain what do i have to do to establish what I am trying to do ?
EDIT After a bit of re开发者_C百科search I am using the following layout
mode.setLayout(new GridLayout(5, 0, 1, 1));
Here mode
is the JPanel. Now if i have just 2 JButtons added to the JPanel the buttons will be displayed one below the other which is fine. But I am getting extra space at the bottom of those 2 buttons. If I have 15 buttons I am getting 5 rows and 3 columns which is again fine but the window size is small I and I have to resize the window manually on runtime for displaying the buttons fully.
Specific Question: How to manage the window size on the above dynamic buttons scenario ?
The basic code for adding components dynamically is:
panel.add(...);
panel.revalidate();
Update:
You can use the following to get the Window that needs to be packed:
SwingUtilities.windowForComponent(...);
精彩评论