2

In my application i am looking to fill the JPanel with buttons generated dynamically. I am using GridBagLayout for that.

1.) When i setup the layout manager to fill just one row with many columns even if there is just one button it fills the entire area which is fine.

2.) In the case of same layout with one column and multiple row its is not filling the whole JPanel but it put the button on the top of the panel. I want to fill the button to fit the panel and i dont want to see any empty spaces.

1 and 2 are 2 different panels.

I wonder why is it doing to the second panel i was talking about when i use same kind of code for both. The code i am using is shown below.

left_panel.setLayout(new GridLayout(count, 1, 5, 5));
top_panel.setLayout(new GridLayout(1, count, 5, 5));

Here left_panel and top_panel are 2 JPanel

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • Is it `GridLayout` as in the title & code, `GridBagLayout` as in the 2nd sentence, or a combination of both? For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). – Andrew Thompson Apr 27 '11 at 07:41
  • no its just GridLayout, not gridbaglayout.. the code block i have given is the only place i am settin the layout for the JPanel i am using. and in loop i am adding buttons to it.. – Deepak Apr 27 '11 at 07:55
  • @Deepak "the code block i have given is the only place i am settin the layout for the JPanel i am using." There are apparently at least two `JPanel` instances. I could ask "What layout does the parent container have?", "Does the code call `pack()` or `validate()`?", "Is the GUI created on the EDT?" and many more questions. But since an SSCCE answers all those questions, I could not be bothered playing '20 questions'. Good luck with it. – Andrew Thompson Apr 27 '11 at 08:01
  • @Andrew Thompson: I can understand that but i am creating the GUI in NetBeans and it is not calling pack() i just checked that. It is hard for me to make SSCCE from that. that will cost me lot of time. Actually i am creating the GUI in netbeans and this JPanel which i am talking about is done on another class which adds it to the parent JPanel. does that helps ? – Deepak Apr 27 '11 at 08:51
  • i am validating the panel after i append the buttons to the panels (left_panel and top_panel) – Deepak Apr 27 '11 at 08:52
  • 1
    @Deepak: "It is hard for me to make SSCCE from that. that will cost me lot of time." but in order to figure out a solution to your problem we'd need to create the SSCCE. Whose should put in this effort, the one asking for free advice or the volunteers helping him on their free time? – Hovercraft Full Of Eels Apr 27 '11 at 12:00
  • @Hovercraft Full Of Eels: Sorry for that i am not lazy to do that but really i am in hurry thats y i told you guys like that.. – Deepak Apr 27 '11 at 12:37

1 Answers1

3

In this example, which uses GridLayout, the buttons fill the space available in both directions at each game level. You might compare your code to the code in resetGame().

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • but aren't they dealing with just one JPanel ?? – Deepak Apr 28 '11 at 03:35
  • so they are like using 4 rows 5 cols. but i want to use 5 rows and 1 column.. that is where i am getting trouble at.. – Deepak Apr 28 '11 at 03:36
  • Don't forget, you can use `0` to mean any number of rows or columns (but not both), as shown [here](http://stackoverflow.com/questions/5750068/java-swing-how-to-change-gui-dynamically/5751044#5751044). – trashgod Apr 28 '11 at 04:23
  • You are the man!!! i got it working. Actually i was setting the layout in a loop so it was setting everytime i add the component. I removed that and put in on the place where it is executed just once. And that "0" logic is awesome man!! i m gonna use it throughout my application.. thanks you verymuch!! – Deepak Apr 28 '11 at 06:30