Michael Michael - 1 year ago 62
Java Question

Java GUI layout questions

Sorry if this has been posted before, but I cannot seem to find any good info that helps me, or I just don't understand other answers enough to help me as I just started programming GUIS.

I wrote a program that has various Items in their own (sub)Jpanel(jtextfields. combobox's, buttons etc.) and all the sub Jpanels in a main jpanel inside a jframe. My goal is to center and stack each sub JPanel on top of one another, so that when the user resizes the window each item stays centered and stacked on top of one another. (when I stacked I don't mean layered where one pane is in front of another, rather stacking the panes like a sandwich so to speak) My panes just move with the default flowlayout and I hope to stop that.

I have seen BoxLayout but like I said, I am new to GUIS and I am not sure if I can apply the BoxLayout to Jpanels.

Answer Source

First off, it is called a JPanel, not JPane :P


JPanel panel = new JPanel(new BorderLayout());

BorderLayout is probably your best bet. For more information on layouts, check out


And by the way, NetBeans GUI Builder is probably your best friend as an introductory GUI programmer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download