javaswinglayout-managerspringlayout

Location of the buttons go to a strange location (SpringLayout)


I am making a java application where you can add new Accounts through a form, i am using the SpringLayout for it because it looks neat, however after adding the JTextFields with the text of it, the button stays at the left top whilst it shouldn't be coming there, i am using SpringUtilities (https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/SpringGridProject/src/layout/SpringUtilities.java)

package dinges.Containers;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import dinges.Utilities.SpringUtilities;

@SuppressWarnings("serial")
public class Addnew extends JPanel {

    String[] options = {"User", "Accountant", "Administrator", "Developer"};

    /**
     *  > Add a text input for the following:
     *  > Id, Name, last name, current balance, and the state. But this has to be in order of the new Account.
     *  > we're just going to be using JTextFields, a JButton for saving and JLabels for writing what it is
     * 
     **/

    public Addnew() {
        // frame size is WIDTH =   280     ,      HEIGHT =     480
        SpringLayout layout = new SpringLayout();
        setLayout(layout);

        JButton save = new JButton("Save data");
        JTextField name = new JTextField(15);
        JTextField lastname = new JTextField(15);
        JComboBox<String> accounttype = new JComboBox<String>(options);
        JLabel label1 = new JLabel("First name: ", JLabel.TRAILING);
        JLabel label2 = new JLabel("Last name: ", JLabel.TRAILING);
        JLabel label3 = new JLabel("Account type: ", JLabel.TRAILING);
        JLabel label4 = new JLabel("Save data: ", JLabel.TRAILING);
        label1.setLabelFor(name);
        label2.setLabelFor(lastname);
        label3.setLabelFor(accounttype);




        add(label1);
        add(name);
        add(label2);
        add(lastname);
        add(label3);
        add(accounttype);
        add(save);
        add(label4);

        SpringUtilities.makeCompactGrid(this, 3, 2, 6, 6, 6, 6);
    }

}

This makes it look like this:

enter image description here

But the button should be under the JComboBox and its JLabel should be positioned just like the others.

Where is the issue here? I have been switching things around for a while now but i really cannot find it.


Solution

  • the button stays at the left top

    Where should it be?

    I'm guessing it should be at the bottom, but you should be specifying that as part of your question because we are not mind readers and don't know what you are thinking.

    i am using SpringUtilities

    Did you tell SpringUtilities how many rows/columns you need?

    That is did you modify the parameters from the demo code or did you just copy the demo code without changing it?