views:

72

answers:

2

Using GroupLayout and the code below, I noticed that the leftPanel is not updating it's preferred size. I thought this was supposed to be automatic and appears to work in other layout managers. How can I get that behaviour with GroupLayout?

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SizeTesting {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);

        JSplitPane splitPane = new JSplitPane();

        final JPanel leftPanel = new JPanel();
        final DefaultListModel listModel = new DefaultListModel();
        final JList list = new JList(listModel);
        final JScrollPane jsp = new JScrollPane(list);
        leftPanel.add(jsp);

        splitPane.setLeftComponent(leftPanel);

        GroupLayout panel1Layout = new GroupLayout(leftPanel);
        leftPanel.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup()
                .addComponent(jsp, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
        );
        panel1Layout.setVerticalGroup(
        panel1Layout.createParallelGroup()
            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jsp, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
        );



        JPanel rightPanel = new JPanel();

        JButton button = new JButton("Add Long Item");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Preferred Size of List BEFORE: " + list.getPreferredSize());
                System.out.println("Preferred Size of ScorllPane BEFORE: " + jsp.getPreferredSize());
                System.out.println("Preferred size of LeftPanel BEFORE: " + leftPanel.getPreferredSize());
                System.out.println();

                listModel.addElement("Really long, new Item in List");
                System.out.println("Preferred Size of List AFTER: " + list.getPreferredSize());
                System.out.println("Preferred Size of ScorllPane AFTER: " + jsp.getPreferredSize());
                System.out.println("Preferred size of LeftPanel AFTER: " + leftPanel.getPreferredSize());
            }
        });
        rightPanel.add(button);
        splitPane.setRightComponent(rightPanel);

        listModel.addElement("Test");
        listModel.addElement("Test2");
        listModel.addElement("Test3");

        frame.add(splitPane);
        frame.setVisible(true);
    }
} // end class SizeTesting
+1  A: 

Not all LayoutManagers take Component.preferredSize() into account when doing their layout. The two that I know of that do are FlowLayout and BoxLayout (others might, but they might not).

Seth
I know that preferredSize isn't always honored, but the thing is it's not even being recomputed. My understanding is that generally speaking, containers will recompute their preferredSize based on the preferredSizes of their components, even if those sizes aren't honored by the layoutManager. Perhaps I'm not correct in my assumption.
Aaron L. Carlow
+1  A: 

The preferred size of a component is based on the preferred size of the components added to it.

Yes, the preferred size of the JList will change as you add items to the list. However, the JList is added to a JScrollPane, so its the preferred size of the JScrollPane that is used to determine the preferred size of the JPanel.

Since you are using an IDE to create your GUI is looks like the preferred size of the JScrollPane is determined at design time and hardcoded into the constraint. The preferred size of the JScrollPane will not change as the frame is resized or as you add items to the JList. The "size" of the JScrollPane and JPanel will change as the frame is resized.

camickr
Yes, it seems I pointed the finger at GroupLayout unfairly. In my code, the GUI builder is setting the preferred size of the JScrollPane in the addComponent() method. Once a component's preferredSize is set manually like this, it never "auto sizes" its preferredSize again and this was the cause of the problem.Is there any way to restore the "auto sizing" behavior of a component's preferredSize, I wonder?
Aaron L. Carlow