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();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

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

		
		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();
		panel.add(centerSplit);
		contentPane.add(panel, BorderLayout.CENTER);
		
		splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		tabbedPane.addTab("New tab", null, splitPane, null);
		
		scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);
		
		textArea = new JTextArea();
		textArea.setPreferredSize(new Dimension(600,200));
		scrollPane.setViewportView(textArea);
		
		scrollPane_1 = new JScrollPane();
		splitPane.setRightComponent(scrollPane_1);
		
		table = new JTable();
		scrollPane_1.setViewportView(table);
		
		statusLabel = new JLabel("");
		southPanel.add(statusLabel);
		
		scrollPane_2 = new JScrollPane();
		centerSplit.setLeftComponent(scrollPane_2);
		centerSplit.setRightComponent(tabbedPane);
		
		tree = new JTree();
		tree.setPreferredSize(new Dimension(100,200));
		scrollPane_2.setViewportView(tree);
	}

}

Open in new window

LVL 5
blackfrancis75Asked:
Who is Participating?
 
objectsCommented:
thats actually how its works
your problem is the (default) FlowLayout used by the panel you add to the center
try:

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


or just add the split pane to the content pane directly
0
 
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.
0
 
zzynxSoftware engineerCommented:
thanx 4 axxepting
0
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.