problem with BorderLayout

How can I make 'panel' (in the center ) stretch vertically and horizontally to fill the complete space in between the North and South areas of the BorderLayout?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame {

	private static final long serialVersionUID = -573737534454859870L;
	private JPanel contentPane;
	private JPanel northPanel;
	private JButton btnNew;
	private JButton btnOpen;
	private JButton btnSave;
	private JPanel southPanel;
	private JLabel statusLabel;
	private JTree tree = null;
	private JTabbedPane tabbedPane = null;
	private JSplitPane splitPane;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JTextArea textArea;
	private JTable table = null;
	private JScrollPane scrollPane_2;
	private JSplitPane centerSplit;

	 * Launch the application.
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
				} catch (Exception e) {

	 * Create the frame.
	public Main() {
		setBounds(100, 100, 750, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(1, 1));
		northPanel = new JPanel();
		northPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		contentPane.add(northPanel, BorderLayout.NORTH);
		btnNew = new JButton("New");
		btnOpen = new JButton("Open");
		btnSave = new JButton("Save");

		southPanel = new JPanel();
		southPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		contentPane.add(southPanel, BorderLayout.SOUTH);
		tabbedPane = new JTabbedPane();
		centerSplit = new JSplitPane();
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		splitPane = new JSplitPane();
		tabbedPane.addTab("New tab", null, splitPane, null);
		scrollPane = new JScrollPane();
		textArea = new JTextArea();
		textArea.setPreferredSize(new Dimension(600,200));
		scrollPane_1 = new JScrollPane();
		table = new JTable();
		statusLabel = new JLabel("");
		scrollPane_2 = new JScrollPane();
		tree = new JTree();
		tree.setPreferredSize(new Dimension(100,200));


Open in new window

Who is Participating?
thats actually how its works
your problem is the (default) FlowLayout used by the panel you add to the center

            centerSplit = new JSplitPane();
            JPanel panel = new JPanel(new BorderLayout());
            contentPane.add(panel, BorderLayout.CENTER);

or just add the split pane to the content pane directly
zzynxSoftware engineerCommented:
objects is completely right.
Maybe some extra explanation is welcome.

>> How can I make 'panel' (in the center ) stretch vertically and horizontally to fill the complete space in between the North and South areas of the BorderLayout?
The component placed in the center of a BorderLayout takes up all available space (not used by the components in the north, south, west and east) by design.
So in your case, 'panel' is stretched the way you want.

But since you defined it as

         JPanel panel = new JPanel();

it has a FlowLayout by default. (since not specifying a layout for a JPanel == means ==> FlowLayout is used by default)

This means that 'panel' won't use all the available space of the center of its parent container "contentPane" (only what it needs)

You can force your panel to also stretch "together with its parent container" by giving it the BorderLayout:

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

and add the centerSplit component in its center by writing

       panel.add(centerSplit, BorderLayout.CENTER);

or for short:

        panel.add(centerSplit);  // If you don't specify the position when adding a component to a panel having BorderLayout, it's BorderLayout.CENTER by default

I hope that makes the things as clear as possible.
zzynxSoftware engineerCommented:
thanx 4 axxepting
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.