2

I need to rearrange items that are in my GridLayout so when a JButton is created dynamically, that the footer (i.e. JLabel) stays at the bottom of the grid, and the dynamically created button goes right above the footer.

Is this possible? If so can I see an example, please?

My grid currently is a

new GridLayout(intIndex, 1);

where intIndex is incremented every time a dynamic element is created.

mrkhrts
  • 829
  • 7
  • 17
Dennis Martinez
  • 6,344
  • 11
  • 50
  • 67

2 Answers2

4

Since it's a footer, you probably want to use BorderLayout and keep the footer down at the bottom with BorderLayout.PAGE_END. Then put your component with the GridLayout in the center with BorderLayout.CENTER. This way, your footer will always remain at the bottom and it won't interfere with the content, which you're now free to change to use any layout manager without affecting the footer.

This should be a good fix if your footer spans across the whole bottom, but if you're trying to make some sort of small footer in the bottom corner, then it'll be a little more difficult, but either way I'd suggest trying to keep the footer separate from the content.

Nate W.
  • 9,141
  • 6
  • 43
  • 65
  • Thanks for the feedback, and I didn't even think of this! The only problem I'm having is trying to "repack" (by using pack() after I added the item to the layout). It will repack just not to the new size. Any ideas? – Dennis Martinez Sep 14 '11 at 18:09
  • Unless you're adding components directly to a `JFrame` or some heavyweight container you don't need to call `pack()`. What component is not sizing correctly for you? Could you provide a little more detail? – Nate W. Sep 14 '11 at 18:13
  • When I add a new panel to the gridlayout in the BorderLayout.CENTER position, the JFrame kind of "overlaps" the previous panel and I figured pack would just resize the jframe to the appropriate size. – Dennis Martinez Sep 14 '11 at 18:18
  • See also this related [example](http://stackoverflow.com/questions/5750068/java-swing-how-to-change-gui-dynamically/5751044#5751044). – trashgod Sep 14 '11 at 20:41
  • @Dennis Martinez: Now you've got a different problem! Fortunately it's a common one - you should be able to find several posts that discuss dynamically adding UI components - check those out and come back with questions if you can't get it working! – Nate W. Sep 14 '11 at 22:41
1

I think there is more I can add, although one answer is accepted.

There are two interfaces for Layouts: LayoutManager and LayoutManager2. The second one extends the first one. So, all layout classes are inherited from LayoutManager.

Now, LayoutManager2 lets you arrange your components according to some constraints. For example, in Shakedown's answer, BorderLayout.CENTER is a constraint for BorderLayout.

However, the layouts those implements only LayoutManager does not accept constraints for a specific component. GridLayout is such a layout. It will start adding components from top-left corner and continue towards right and go to next line when one line is full.

You may be interested about GridBagLayout.

Mohayemin
  • 3,841
  • 4
  • 25
  • 54