0

Im using GridBagLayout to align JButtons, JLabels, and JTextFields into a login form. But sometimes all the objects get crammed into one line. (not allways, it happens randomly but too often to ignore).

this is how I'm aligning the objects

package test;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

public class ManagerMain {
    public static JFrame frame = new JFrame("this is a frame");
    private JLabel loginNameLB = new JLabel("username:");
    private JLabel loginPasswordLB = new JLabel("password:");
    private JTextField loginName = new JTextField();
    private JPasswordField loginPassword = new JPasswordField();
    private JButton loginBTN = new JButton("Login");
    private JButton loginCreateBTN = new JButton("Create An Account");
    private Container login = new Container();
    //login is a container
    public ManagerMain() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        frame.setLayout(new BorderLayout());
        login.setLayout(new GridBagLayout());
        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.insets = new Insets(2, 2, 15, 15);
        gbc1.weightx = 1.0;
        gbc1.weightx = 1.0;
        //user name label
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginNameLB, gbc1);
        //user name text field
        gbc1.gridx = 1;
        gbc1.gridy = 0;
        gbc1.gridwidth = 2;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginName, gbc1);
        //password label
        gbc1.gridx = 0;
        gbc1.gridy = 1;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginPasswordLB, gbc1);
        //password text field
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.gridwidth = 2;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginPassword, gbc1);
        //login button
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginBTN, gbc1);
        //create account button
        gbc1.gridx = 2;
        gbc1.gridy = 2;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginCreateBTN, gbc1);
        frame.add(login, BorderLayout.CENTER);//add container to frame
    }
}

Any suggestions? Image of what it looks like / what it should look like is bellow

Screenshot 1

sometimes it does this also:

Screenshot 2

Bryce Hahn
  • 1,585
  • 4
  • 16
  • 26
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Oct 12 '14 at 02:59
  • Get rid of the gridwidth and gridheight to start with – MadProgrammer Oct 12 '14 at 03:00
  • Looks like the layout has been rest to FlowLayout – MadProgrammer Oct 12 '14 at 03:01
  • This is why I avoid Swing layout managers (just look at all that code). I would suggest you use a third-party library like MigLayout. I realise this doesn't answer your question and you might not want to add dependencies to your project, but it's just a suggestion. – dramzy Oct 12 '14 at 03:08
  • @dramzy can you link me to a good tutorial/project example using `MigLayout`? – Bryce Hahn Oct 12 '14 at 03:23
  • It's all on their [site](http://www.miglayout.com): here is the [quick start guide](http://www.miglayout.com/QuickStart.pdf) and the [white paper](http://www.miglayout.com/whitepaper.html) – dramzy Oct 12 '14 at 03:24
  • I turned out at [Java GridBagLayout not working](http://stackoverflow.com/questions/26088675/java-gridbaglayout-not-working) that `GridBagLayout` doesn't always work as expected. Maybe `GroupLayout` or [JGoodies FormLayout](http://www.jgoodies.com/freeware/libraries/forms/) are other options. – Gerold Broser Oct 12 '14 at 03:31
  • @dramzy you're gonna regret it once you start deploying applications. Absolute layout (null layout, as you'd call it) may render different results on different platforms. It's best allow LayoutManagers to configure the layout – Vince Oct 12 '14 at 03:43
  • @VinceEmigh MigLayout IS a layout manager, it is not the same as using an absolute layout. It is just a simpler third-party layout manager with simpler constraints. I have used it for various projects and tested it on Mac OS, Windows, and some linux distros. – dramzy Oct 12 '14 at 03:48
  • Setting the GridBagConstraints, specifically gridy, should take care of putting the components on different lines. In fact, when I run this code myself it works as it should (per the above image). – Woodchuck Aug 26 '17 at 18:32

1 Answers1

3

it happens randomly but too often to ignore. Any suggestions?

  1. You should invoke setVisible(...) AFTER all the components have been added to the frame.
  2. You should invoke pack() BEFORE setVisible()..
  3. Make sure the GUI components are created on the Event Dispatch Thread (EDT)
camickr
  • 321,443
  • 19
  • 166
  • 288