Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout Manager preferredSize Java

Tags:

java

layout

swing

I'm still trying to learn how layout managers work. I made a Frame with two JPanels. The first one contains a textArea with a boxLayout. The second one contains a flow layout with a button.

I set the preferredSize 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 seems to always allocate half of the available space to each control. I have been told that:

The height of each row is dependent on the height of each component added in each row.

The buttonpane's height is 20. It's allocating much more than that to it:

Wasted space

What's wrong with this code?
I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).

like image 233
David Avatar asked Aug 13 '11 14:08

David


3 Answers

That's the result of using GridLayout as layout manager. Change it to BorderLayout:

frame.getContentPane().setLayout(new BorderLayout());

For example, this code (I changed a little as possible from the original):

import java.awt.*;
import javax.swing.*;

public class LayoutMgrTest
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        //frame.setVisible(true);   
        //frame.getContentPane().setLayout(new BorderLayout());

        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,40));
        buttonPane.add(new JButton("Button1"));
        buttonPane.add(new JButton("Button2"));

        frame.add(controlPane, BorderLayout.NORTH);
        frame.add(buttonPane, BorderLayout.SOUTH);
        //frame.setSize(new Dimension(500,500));
        frame.pack();
        frame.setVisible(true);
    }
}

Generates this frame:

enter image description here

like image 175
MByD Avatar answered Sep 21 '22 00:09

MByD


I set the preferredSize of each panel accordingly,

That is another problem. You should NOT set the preferred size. That is the job of the layout manager. Just add your components to the panels and let the layout manager do its job.

Most compnents have a default preferred size. For some you need to give it a little tip. For example when using a text area you would give a "suggested" preferred size by using:

JTextArea textArea = new JTextArea(rows, columns);
like image 24
camickr Avatar answered Sep 18 '22 00:09

camickr


If you use LayoutManager, you should not set a size on a component except the frame.

the size for the components is calculated from the different layout managers.

you find more infos at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html

in your code, you can add the panel with the textarea to BorderLayout.CENTER. this should solve your problem. the component in BorderLayout.CENTER takes the whole space, except the space needed for the components in NORTH, EAST, SOUTH and WEST.

like image 40
Dragon8 Avatar answered Sep 18 '22 00:09

Dragon8