Peter Lock
Peter Lock

Reputation: 11

How can we have a contained GridBagLayout, with preferredSize, with vertical scroll only?

Does anyone know how to add a ScrollBar to a JPanel with a Gridbaglayout, allowing the Gridbaglayout to still reposition components on the horizontal axis. I've tried adding the scroll bar - but when I do the Gridbaglayout repositioning of components on the horizontal stops working and the components go off the side of the screen when I reduce the window width. How can we include vertical scroll, while allowing the Gridbaglayout to still reposition components on the horizontal axis. And help would be appreciated.

The GridBagLayout should still move the components.

The code:

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

public class SwingTest {



    public static void main(String[] args) {

        final JScrollPane scrollPane = new JScrollPane(addCustomer());


        JFrame frame = new JFrame("Swing Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setMinimumSize(new Dimension(300, 480));
        frame.setLocation(200, 200);
        frame.getContentPane().add(scrollPane);

        frame.setVisible(true);
    }

    public static Component addCustomer(){
        JTextField textField1;
        /***********************Initialize Fields******************************/

        JLabel labels[] = new JLabel[2];

        labels[0] = new JLabel("Left side - more components to add here");
        labels[1] = new JLabel("Right side - more components to add here");

        textField1 = new JTextField(10);
        JComboBox<String> materialTitlesCombo = new JComboBox<String>();

        materialTitlesCombo.addItem("-");
        materialTitlesCombo.addItem("test");
        materialTitlesCombo.setPreferredSize(new Dimension(250, 500));
        materialTitlesCombo.setMinimumSize(materialTitlesCombo.getPreferredSize());
        /****************************Create the canvas**************************/
        GridBagConstraints constraints;

        constraints = new GridBagConstraints();


        final JPanel panel = new JPanel(new GridBagLayout());

        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridheight = 1;
        constraints.gridwidth = 1;
        constraints.weightx = 0.5;
        constraints.weighty = 1;
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(-20,20,0,0);
        panel.add(labels[0], constraints); //Left side

        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridheight = 1;
        constraints.gridwidth = 1;
        constraints.weightx = 0.5;
        constraints.weighty = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.insets = new Insets(-10,-9,0,9);
        panel.add(textField1, constraints); // TextField 1

        constraints.gridx = 8;
        constraints.gridy = 3;
        constraints.gridheight = 1;
        constraints.gridwidth = 1;
        constraints.weightx = 0.5;
        constraints.weighty = 1;
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(-15,15,0,0);
        panel.add(labels[1], constraints);//Right side

        return panel;
    }
}

Upvotes: 0

Views: 129

Answers (2)

Peter Lock
Peter Lock

Reputation: 11

Just add setMaximumSize, and setPreferredSize with a minimumSizeValue to the parent panel, containing a nested panel that uses gridbaglayout housing your components.

panel.setMaximumSize(getYourMinimumSize); panel.setPreferredSize(getYourMinimumSize);

Works with nested tabPanes and nested Panels.

Upvotes: 0

ck1
ck1

Reputation: 5443

To add a scrollable JPanel as a tab, you can do the following:

JScrollPane scrollPane = new JScrollPane(addCustomer());
pane.addTab("...", scrollPane);

I noticed that you never add the JTabbedPane component to the CustomerPane, which means the tabbed pane will never be rendered in the UI. You can try the following:

private void initializeUI() {
    ...
    add(pane);
}

Upvotes: 1

Related Questions