2 Attachment(s)
How to get panels to occupy all available space and components in the center?
I am writing a program, which has a toolbar panel at the bottom (using BorderLayout) of the content pane, and a main panel at the center of the content pane.
The main panel has a label at the bottom (using BorderLayout) and a "view" panel in the center which has a cardLayout. The view panel has two components at the moment, a label and a text area.
The problem is that except for the toolbar panel, all other panels do not occupy all the space available (see diagrams attached).
I know this problem is solved by using a different layout manager, but the problem is i'm not sure which one would be the best to use here. I could use some advice.
I've attached three things
-a simplified compilable version of the code, to illustrate the problem
-a screenshot of what I get when I run the code
-an image of the kind of layout I want.
Thanks for your help.
Code
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class LayoutProblem {
private JFrame window;
private JButton button;
private JLabel label;
private JPanel contentPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
private JLabel textAreaLabel;
private JTextArea textArea;
public LayoutProblem()
{
window = new JFrame("Window");
window.setSize(600,400);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bottomPanel = new JPanel();
bottomPanel.setBackground(Color.BLUE);
button = new JButton("Button");
bottomPanel.add(button);
window.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
contentPanel = new JPanel();
label = new JLabel ("Label");
middlePanel = new JPanel();
middlePanel.setBackground(Color.RED);
middlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
textAreaLabel = new JLabel("Hi");
textArea = new JTextArea("Stuff Stuff Stuff");
middlePanel.add(textAreaLabel);
middlePanel.add(textArea);
contentPanel.add(middlePanel,BorderLayout.CENTER);
contentPanel.add(label,BorderLayout.SOUTH);
window.getContentPane().add(contentPanel,BorderLayout.CENTER);
}
public static void main(String args[])
{
new LayoutProblem();
}
}
What I get:
Attachment 1894
What I want: (Sorry, the image kinda burns the eyes a bit)
Attachment 1895