I'm still trying to find out how layout managers work. I made a frame with two JPanels. The first contains textArea with boxLayout. The second contains the layout of the stream using the button.
I set the preferred size of each panel accordingly, packed them, but got unexpected results.
import java.awt.*; import javax.swing.*; public class LayoutMgrTest { public static void main(String[] args) { TableBasic frame = new TableBasic(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.setVisible(true); frame.getContentPane().setLayout(new GridLayout(2,1)); JPanel controlPane = new JPanel(); JPanel buttonPane = new JPanel(); controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS)); controlPane.setPreferredSize(new Dimension(200, 200)); controlPane.add(new JScrollPane(new JTextArea())); buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT)); buttonPane.setPreferredSize(new Dimension(100,20)); buttonPane.add(new JButton("Button1")); buttonPane.add(new JButton("Button2")); frame.getContentPane().add(controlPane, BorderLayout.NORTH); frame.getContentPane().add(buttonPane, BorderLayout.SOUTH); frame.setSize(new Dimension(500,500)); frame.pack(); } }
Whatever I do, if I use a grid layout, it always allocates half the available space for each control. I was told that:
The height of each row depends on the height of each component added to each row.
The height of the button is 20. She sets aside much more for her:

What is wrong with this code? I would like to leave two JPanels intact, please. It's easy to just add a text box and buttons directly to the frame, but I need to do this with JPanels (because I will add borders and other things).
David source share