What kind of layout should I use to create a page Like this: It should be resizable It has two main panels Right and Left?
-
2Use `BorderLayout`. Place the left panel in the `CENTER` location and right in the `EAST`. – Reimeus Jul 26 '13 at 14:05
-
@Reimeus I'd use `LINE_END` over `EAST`, to account for different locales. – Andrew Thompson Jul 26 '13 at 14:12
-
and what kind of layout for right to get buttons vertically aligned? – s_puria Jul 26 '13 at 14:13
-
I would use a couple of panels with a couple of layouts: The first being the main panel with a BorderLayout, split with a Center and East. The center would contain a panel with another BorderLayout, where it would have a North and Center. The east would contain a gridlayout for the vertical column of buttons. This would give you the effect where your East panel will touch both the top and bottom of your application window. – Clark Kent Jul 26 '13 at 14:26
-
Nice image to demonstrate the desired layout, BTW. The thing I liked best about it was that it was <2Kb! – Andrew Thompson Jul 26 '13 at 15:44
4 Answers
Extra space will be given to the 'Main Text' text area, and extra height will be given to the button panel while centering them.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class EndOfLineButtonLayout {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
JPanel textPanel = new JPanel(new BorderLayout(5,5));
textPanel.add(new JScrollPane(new JTextArea("Top Text",3,20)),
BorderLayout.PAGE_START);
textPanel.add(new JScrollPane(new JTextArea("Main Text",10,10)));
gui.add(textPanel, BorderLayout.CENTER);
JPanel buttonCenter = new JPanel(new GridBagLayout());
buttonCenter.setBorder(new EmptyBorder(5,5,5,5));
JPanel buttonPanel = new JPanel(new GridLayout(0,1,5,5));
for (int ii=1; ii<6; ii++) {
buttonPanel.add(new JButton("Button " + ii));
}
// a component added to a GBL with no constraint will be centered
buttonCenter.add(buttonPanel);
gui.add(buttonCenter, BorderLayout.LINE_END);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}

- 168,117
- 40
- 217
- 433
You can use gridbag layout, try using netbeans, I have tried it and found really usefull. Once you create it with netbeans you can use the same and build infact any kind of layout.
best of luck with other solutions.
p.s. border layout is perfect for your requirement, but I mentioned this just in case you would like to do lot more .

- 93
- 9
I would use BorderLayout.
Create Three JPanels and add them to a JFrame as follows:
public class YourClass extends JFrame{
//code here
this.setLayout(new BorderLayout());
this.add(TopPanel, BorderLayout.NORTH);
this.add(RightPanel, BorderLayout.EAST);
this.add(MainPanel, BorderLayout.CENTER);
this.pack();
this.setVisible(true);

- 310
- 1
- 6
-
I don't think this will give the effect he's looking for where the east panel goes from the top of the application to the bottom of the application. – Clark Kent Jul 26 '13 at 14:20
-
there is a constructor that takes a horizontal and vertical gap that can be set to negative values to have components overlap. I suggest playing with these values. BorderLayout(int hgap, int vgap) – Isaac Jul 26 '13 at 14:23
-
If your NorthPanel is a JTextArea, and your EastPanel is a panel of buttons, won't the EastPanel then overlap the text in the JTextArea? – Clark Kent Jul 26 '13 at 14:27
The two main panels would be placed inside a main JPanel using a BorderLayout. The left panel would be placed using BorderLayout.CENTER, and the right panel would be placed using BorderLayout.LINE_END.
The left panel would use a BoxLayout, Y axis to separate the two JPanels within the left panel.
The right buttons panel would use a GridBagLayout. This sizes the buttons the same and allows you to use Insets to add some spacing to the buttons.
The buttons would be spaced from the top to the bottom of the right buttons panel. If you want all the buttons towards the top of the right buttons panel, you would put the right buttons panel inside of another JPanel using a FlowLayout.

- 50,182
- 6
- 67
- 111