Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide a JPanel into left and right segments?

I want to divide a JPanel into left and right segments. How do I do that ? After that, I will place panels in the left and right half.

like image 930
SuperStar Avatar asked Apr 03 '13 08:04

SuperStar


People also ask

How do you add a separator in Java GUI?

The code to add the menu items and separators to the menu is extremely simple, boiling down to something like this: menu. add(menuItem1); menu.

How do I use JSplitPane?

A JSplitPane displays two components, either side by side or one on top of the other. By dragging the divider that appears between the components, the user can specify how much of the split pane's total area goes to each component.


1 Answers

If there is no need to resize them, you can simply use a BorderLayout and insert your panels in the BorderLayout.EAST and BorderLayout.WEST:

JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );

You could also consider using a JSplitPane which allows to resize the UI:

JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, 
                                  leftPanel, rightPanel );
like image 144
Robin Avatar answered Oct 31 '22 13:10

Robin