So this is the first time using a Spring Layout and I gotta say, it honestly seems like a very nice Layout to use, but I can't quite seem to get it working the way I want it to. Let me explain:

I have a JList which should be inset 5px on each side.
I have a Button which should be inset 5px below the JList, and 5px from the right side.
I have another Button which should be inset 5px below the list, and 5px to the left of the previous Button.
Each Button should be inset 5px from the bottom.

Here's my code:

JFrame frame = new JFrame("Force StartUp");
        frame.setPreferredSize(new Dimension(600, 600));
        frame.setMinimumSize(new Dimension(600, 600));
        frame.setMaximumSize(new Dimension(1200, 900));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        SpringLayout springLayout = new SpringLayout();
        frame.setLayout(springLayout);
        Container contentPane = frame.getContentPane();

        JList<String> entryList = new JList<String>();
        entryList.setPreferredSize(new Dimension(590, 555));

        JButton addButton = new JButton("Add");
        addButton.setPreferredSize(new Dimension(75, 25));

        JButton removeButton = new JButton("Remove");
        removeButton.setPreferredSize(new Dimension(100, 25));

        springLayout.putConstraint(SpringLayout.WEST, entryList, 5, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, entryList, 5, SpringLayout.NORTH, contentPane);
        //springLayout.putConstraint(SpringLayout.SOUTH, entryList, 35, SpringLayout.SOUTH, contentPane);

        springLayout.putConstraint(SpringLayout.NORTH, addButton, 5, SpringLayout.SOUTH, entryList);
        springLayout.putConstraint(SpringLayout.WEST, addButton, 115, SpringLayout.WEST, contentPane);

        springLayout.putConstraint(SpringLayout.NORTH, removeButton, 5, SpringLayout.SOUTH, entryList);
        springLayout.putConstraint(SpringLayout.WEST, removeButton, 5, SpringLayout.EAST, addButton);
        springLayout.putConstraint(SpringLayout.WEST, removeButton, 5, SpringLayout.EAST, addButton);


        springLayout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, entryList);
        springLayout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, addButton);
        springLayout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, removeButton);
        springLayout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, removeButton);

        contentPane.add(entryList);
        contentPane.add(addButton);
        contentPane.add(removeButton);

        frame.pack();
        frame.setVisible(true);

Is there another Layout that I can use that would work? I'm not great with Layouts. I did try a GridBagLayout but it's not ideal.

I've always used GridBagLayout and it's never let me down. What's "not ideal" about it for your application?

I wanted something like the picture of the "Name Chooser" GUI only with a Vertical JList from List Tutorial from Oracle.

Not sure how to accomplish the layout of that using GridBagLayout. Also, when I use GridBagLayout, it always displays the components in the middle, and they're smaller than the set preferred size. Obviously this is just something I'm overlooking on my part.

Here's my basic GridBagLayout which doesn't layout the button's the way I would like them to be, and it's positions center and not the correct size.

        frame = new JFrame("Force StartUp");
        frame.setPreferredSize(new Dimension(600, 600));
        frame.setMinimumSize(new Dimension(600, 600));
        frame.setMaximumSize(new Dimension(1200, 900));
        frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new GridBagLayout());

        GridBagConstraints entryListGBC = new GridBagConstraints();
        entryListGBC.gridx = 0;
        entryListGBC.gridy = 0;
        entryListGBC.gridwidth = 2;
        entryListGBC.fill = GridBagConstraints.BOTH;
        entryListGBC.insets = new Insets(5, 5, 0, 5);

        GridBagConstraints addButtonGBC = new GridBagConstraints();
        addButtonGBC.gridx = 0;
        addButtonGBC.gridy = 1;
        addButtonGBC.fill = GridBagConstraints.BOTH;
        addButtonGBC.insets = new Insets(5, 5, 5, 5);

        GridBagConstraints removeButtonGBC = new GridBagConstraints();
        removeButtonGBC.gridx = 1;
        removeButtonGBC.gridy = 1;
        removeButtonGBC.fill = GridBagConstraints.BOTH;
        removeButtonGBC.insets = new Insets(5, 5, 5, 5);

        JList<String> entryList = new JList<String>();
        entryList.setPreferredSize(new Dimension(590, 555));
        entryList.setLayoutOrientation(JList.VERTICAL);

        JScrollPane entryLisScrollPane = new JScrollPane(entryList);
        entryLisScrollPane.setPreferredSize(entryList.getPreferredSize());

        JButton addButton = new JButton("Add");
        addButton.setPreferredSize(new Dimension(75, 25));

        JButton removeButton = new JButton("Remove");
        removeButton.setPreferredSize(new Dimension(100, 25));

        frame.add(entryLisScrollPane, entryListGBC);
        frame.add(addButton, addButtonGBC);
        frame.add(removeButton, removeButtonGBC);

        frame.pack();

For things to be scaled/filled properly you need to specify the weights, otherwize it assumes 0 and groups them all at minimum size in the center.

Alright, I fixed that. However, still not sure what to do about the "pushing" the buttons over to the right.

Anchor RIGHT

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.