1

I have a JFrame with layout BorderLayout, I add JPanels using BorderLayout.CENTER I have a JPanel(added using CENTER), that uses GridLayout to place 4 buttons.

These buttons are MASSIVE, taking up the whole screen. Or, I do it my way as follows:

  • I have a JFrame with layout null, I set JPanel bounds and add them.
  • I have a JPanel: It sets it's own bounds so it takes up center 1/2 of screen, and is only 1/8 of the screen tall.
  • It adds buttons using grid layout, and results in a nice line of buttons.

Obviously the second option looks better, but as I wish to conform to the norm, and always use layouts... How to I mix customization and layouts?(Or just solve my problem at all)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
csga5000
  • 4,062
  • 4
  • 39
  • 52

1 Answers1

4

When you add a componeent to BorderLayout.CENTER, it will expand to fill the remaining space of the container not used by the other portions of the BorderLayout. Also, you can only add one component at a time to BorderLayout.CENTER. Subsequent adds will replace the previous component.

As for solving your exact problem, I suggest you start by taking a look at A Visual Guide to Layout Managers. This will give you a primer on what layouts are available in the Swing API. Oracle also has more in-depth tutorials for each layout. By nesting layouts, you can give your UI any look that you wish while leveraging their power, especially auto-calculations when a window is resized or otherwise changed.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Ok, thanks I'll look into that for sure. But out of curiousity, is there really anything wrong with just doing it the way I do? It already uses relative portions, and if I have it update when the window's re-sized, then, it seems to works flawlessly... So if I am just to stubburn to conform is there anything "bad" about using setLayout(null) besides user ease? – csga5000 Feb 21 '13 at 01:43
  • 1
    *"..it seems to works flawlessly..."* Where is the code for that? – Andrew Thompson Feb 21 '13 at 01:45
  • 3
    @csga5000 Not every system is the same. Systems have different font and font metrics as well as resolution differences. It's much easier to allow the layout managers to manage these differences then having to code for every possible edge case you might reach... – MadProgrammer Feb 21 '13 at 01:47
  • Good point. Use layouts because of user easy? Yes, but also because of practicality in those respects. I'll probably end up messing with GridBagLayout, it seems to have potential for a lot of what I want to do. – csga5000 Feb 21 '13 at 01:50
  • @csga5000 I think it's more about easing the burden of the programmer (not the user) in creating a consistent UI. – Code-Apprentice Feb 21 '13 at 01:56
  • 2
    *"GridBagLayout, it seems to have potential for a lot of what I want to do."* Also consider [nesting layouts](http://stackoverflow.com/questions/5621338/how-to-add-jtable-in-jpanel/5630271#5630271) within one another. This is often much simpler than trying to get one layout to fit all purposes. – Andrew Thompson Feb 21 '13 at 02:36