Question

I have JPanel inside vertical JSplitPane. JPanel contains jlabels and jtextfields. When I shrink height of JPanel by moving JSplitPane's divider All components in jpanel are resized themselves. How to tell them not to resize when height of parent jpanel shrinks. I cannot size minSize for JPanel because it makes impossible for JSplitPane to move divider.

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerSize(10);
        splitPane.setOneTouchExpandable(true);

        JPanel searchPanel = createSearchPanel();
        splitPane.setDividerLocation(searchPanel.getPreferredSize().height + 5);
        splitPane.setTopComponent(searchPanel);

As you see there is searchPanel. Let's see it:

JPanel searchPanel = new JPanel(new GridBagLayout()) {
            Dimension minSize = new Dimension(200, 0);

            @Override
            public Dimension getMinimumSize() {
                return minSize;
            }
        };
        searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
        GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                new Insets(5, 2, 5, 3), 0, 0);
        searchPanel.add(eventLabel, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 3;
        searchPanel.add(eventLabelField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        searchPanel.add(timestampLabel, gbc);
        gbc.gridx = 1;
        searchPanel.add(timestampStartField, gbc);
        gbc.gridx = 2;
        searchPanel.add(timestammpToLabel, gbc);
        gbc.gridx = 3;
        searchPanel.add(timestampEndField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        searchPanel.add(locationLabel, gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        searchPanel.add(locationField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        searchPanel.add(durationMagnitudeLabel, gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        searchPanel.add(durationMagnitudeMinField, gbc);
        gbc.gridx = 2;
        searchPanel.add(durationToLabel, gbc);
        gbc.gridx = 3;
        searchPanel.add(durationMagnitudeMaxField, gbc);
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.PAGE_END;
        gbc.weighty = 1.0;
        gbc.ipadx = 2;
        gbc.ipady = 2;
        gbc.insets = new Insets(15, 0, 0, 0);
        searchPanel.add(searchButton, gbc);

When I move up vertical divider the height of searchPanel shrinks and components look like:

enter image description here

You see that jtextFields got much smaller than they were after I moved divider up

Please, help me here.

Was it helpful?

Solution

I have changed your code. Use weightx property of GridBagConstraint with GridBagConstraints.HORIZONTAL fill, it helps components to fill their cell properly. Example code:

    JPanel searchPanel = new JPanel(new GridBagLayout()) {
        Dimension minSize = new Dimension(200, 0);

        @Override
        public Dimension getMinimumSize() {
            return minSize;
        }
    };
    searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
    GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
            new Insets(5, 2, 5, 3), 0, 0);
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 1;
    gbc.gridwidth = 3;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.weightx = 0;
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 1;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 2;
    gbc.weightx = 0;
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 3;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.weightx = 0;
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 3;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.weightx = 0;
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 2;
    gbc.weightx = 0;
    searchPanel.add(new JLabel("1"), gbc);
    gbc.gridx = 3;
    gbc.weightx = 1;
    searchPanel.add(new JTextField(), gbc);
    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.gridwidth = 2;
    gbc.anchor = GridBagConstraints.PAGE_END;
    gbc.weighty = 1.0;
    gbc.ipadx = 2;
    gbc.ipady = 2;
    gbc.insets = new Insets(15, 0, 0, 0);
    gbc.weightx = 0;
    searchPanel.add(new JButton("b"), gbc);
    return searchPanel;

Watch docs How to use GridBagLayout

Results:

enter image description here

Edit: Sorry, I'm using awful names for labels)

OTHER TIPS

Add a dummy JPanel to the bottom and set weightY=1 for the JPanel. ALl the rest component should have weightY=0. Thus on height increasing all the additional pixels are targeted to the dummy JPanel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top