-1

I am trying to create a register form for my application(school project), I wanted to set the layout to BoxLayout but the Jtextfields and combo box is having issue as you can see below, does this issue relates to setSize() or is it something I am doing incorrect,I just want the Jtextfields sorts vertically , I appreciate the support enter image description here

private JPanel SetUpRegister() {
        JLabel registerLabel = new JLabel("Registera");

        registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
        loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
        passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
        fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
        lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
        ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
        String[] genderlist = new String[] { "Male", "Female", "Other" };
        JComboBox<String> registerList = new JComboBox<>(genderlist);

        JPanel registerPanel = new JPanel();    
        registerPanel.setBackground(new Color(255, 140, 0));
        registerPanel.add(registerLabel);
        registerPanel.add(loginRegisterInput);
        registerPanel.add(passwordRegisterInput);
        registerPanel.add(fnRegisterInput);
        registerPanel.add(lnRegisterInput);
        registerPanel.add(ageRegisterInput);
        registerPanel.add(registerList);
        registerPanel.setLayout(new BoxLayout(registerPanel,BoxLayout.Y_AXIS));

        return registerPanel;

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Asaranow
  • 19
  • 5
  • 1
    Set the layout before you add the components - you might find that improves your chances – MadProgrammer May 04 '20 at 08:17
  • @MadProgrammer I did it before, unfortunately nothing was resolved – Asaranow May 04 '20 at 08:21
  • 1
    1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) `new Font("Arial", Font.BOLD, 30)` better to use `new Font(Font.SANS_SERIF, Font.BOLD, 30)` as Arial is likely to be missing from OS X & they would prefer Helvetica in any case. – Andrew Thompson May 04 '20 at 09:15
  • 1
    "_Jtextfields and combo box is having issue as you can see below_" **what** is the issue ? – c0der May 04 '20 at 09:21
  • @c0der The input fields are huge – Asaranow May 04 '20 at 10:01

2 Answers2

1

The input fields are huge

The BoxLayout will attempt to resize components when extra space is available on the panel. It will resize a component up to its maximum size.

For some reason the maximum height of a JTextField is Integer.MAX_VALUE which makes no sense to me, since the height of the text never changes as you enter more text.

In any case you have a couple of choices:

  1. Use a different layout manager, like the GridBagLayout. The GridBagLayout, will respect the preferred size of the text fields.
  2. Create a custom JTestField and override the getMaximumSize() method to return the preferred height of the component
  3. Use a wrapper panel.

For the wrapper panel you could do:

JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(registerPanel, BorderLayout.PAGE_START);
return wrapper;
//return registerPanel;

The BorderLayout will respect the preferred height of any component added to the PAGE_START, so there is no need for the BoxLayout to resize any component.

camickr
  • 321,443
  • 19
  • 166
  • 288
0
private JPanel SetUpRegister() {
    JLabel registerLabel = new JLabel("Registera");
    JLabel registerLabel1 = new JLabel("Login :");
    JLabel registerLabel2 = new JLabel("Password :");
    JLabel registerLabel3 = new JLabel("First Name :");
    JLabel registerLabel4 = new JLabel("Last Name :");
    JLabel registerLabel5 = new JLabel("Age :");
    JLabel registerLabel6 = new JLabel("Gender :");
    JLabel registerLabel7 = new JLabel("Bio :");
    registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
    JButton createAccButton = new JButton("Create");
    createAccButton.addActionListener(new CreateAccountListener());
    loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    bioRegisterInput = new JTextField(INPUT_FIELD_WIDTH);

    String[] genderlist = new String[] { "Male", "Female", "Other" };
    registerList = new JComboBox(genderlist);
    JPanel registerPanel = new JPanel();
    registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));
    registerPanel.add(registerLabel);

    JPanel registerLabPanel = new JPanel();
    registerLabPanel.setLayout(new FlowLayout());
    registerLabPanel.add(registerLabel);

    JPanel usernamePanel = new JPanel();
    usernamePanel.setLayout(new FlowLayout());
    usernamePanel.add(registerLabel1);
    usernamePanel.add(loginRegisterInput);

    JPanel passwordPanel = new JPanel();
    passwordPanel.setLayout(new FlowLayout());
    passwordPanel.add(registerLabel2);
    passwordPanel.add(passwordRegisterInput);

    JPanel fnPanel = new JPanel();
    fnPanel.setLayout(new FlowLayout());
    fnPanel.add(registerLabel3);
    fnPanel.add(fnRegisterInput);

    JPanel lnPanel = new JPanel();
    lnPanel.setLayout(new FlowLayout());
    lnPanel.add(registerLabel4);
    lnPanel.add(lnRegisterInput);

    JPanel agePanel = new JPanel();
    agePanel.setLayout(new FlowLayout());
    agePanel.add(registerLabel5);
    agePanel.add(ageRegisterInput);

    JPanel genderPanel = new JPanel();
    genderPanel.setLayout(new FlowLayout());
    genderPanel.add(registerLabel6);
    genderPanel.add(registerList);

    JPanel bioPanel = new JPanel();
    bioPanel.setLayout(new FlowLayout());
    bioPanel.add(registerLabel7);
    bioPanel.add(bioRegisterInput);

    JPanel buttonLoginPanel = new JPanel();
    buttonLoginPanel.setLayout(new FlowLayout());
    buttonLoginPanel.add(createAccButton);

    registerPanel.add(registerLabel);
    registerPanel.add(usernamePanel);
    registerPanel.add(passwordPanel);
    registerPanel.add(fnPanel);
    registerPanel.add(lnPanel);
    registerPanel.add(agePanel);
    registerPanel.add(genderPanel);
    registerPanel.add(bioPanel);
    registerPanel.add(buttonLoginPanel);

    return registerPanel;

}

I fixed the issue by making a Panel for each input and label

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Asaranow
  • 19
  • 5