aherlambang
aherlambang

Reputation: 14418

setting the size of a JPanel

I have a class that extends a JPanel called Row. I have a bunch of Row added to a JLabel, the code is the following:

JFrame f=new JFrame();

JPanel rowPanel = new JPanel();
//southReviewPanel.setPreferredSize(new Dimension(400,130));
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(test1);
rowPanel.add(test1);
rowPanel.add(test2);
rowPanel.add(test3);
rowPanel.add(test4);
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize()); 
rowPanel.setMinimumSize(rowPanel.getPreferredSize());

f.setSize(new Dimension(300,600));

JScrollPane sp = new JScrollPane(rowPanel);
sp.setSize(new Dimension(300,600));
f.add(sp);

f.setVisible(true);

where test1...etc is a Row. However when I resize the window the layout of the Row somehow becomes messy (it resizes as well)... how can I prevent this from happening?

Upvotes: 4

Views: 60401

Answers (2)

Cesar
Cesar

Reputation: 5488

I took the code in http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java and adapted it with what you are trying to do, only using buttons instead of custom JPanels:

public class BoxLayoutDemo {
    public static void addComponentsToPane(Container pane) {
        JPanel rowPanel = new JPanel();
        pane.add(rowPanel);

        rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
        rowPanel.add(addAButton("Button 1"));
        rowPanel.add(addAButton("Button 2"));
        rowPanel.add(addAButton("Button 3"));
        rowPanel.add(addAButton("Button 4"));
        rowPanel.add(addAButton("5"));
        rowPanel.setPreferredSize(new Dimension(600, 400));
        rowPanel.setMaximumSize(rowPanel.getPreferredSize()); 
        rowPanel.setMinimumSize(rowPanel.getPreferredSize());
    }

    private static JButton addAButton(String text) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        return button;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("BoxLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

The end result is this: alt text

As you can see, the button row is perfectly aligned. If you resize the JFrame, they stay aligned. Is that what you are looking for?

Upvotes: 1

camickr
camickr

Reputation: 324088

Read the Swing tutorial on Using Layout Managers. Each layout manager has its own rules about what happens when the container is resized. Experiment and play.

In the case of a BoxLayout it should respect the maximum size of the components added to the panel so you can do:

childPanel.setMaximumSize( childPanel.getPreferredSize() );

If you need more help post your SSCCE demonstrating the problem.

Upvotes: 3

Related Questions