We help IT Professionals succeed at work.
Get Started

problem with BorderLayout

blackfrancis75
on
671 Views
Last Modified: 2013-12-29
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

Comment
Watch Question
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE