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.
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.
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.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With