0


I want to use GroupLayout to get a usernameLabel/Field and passwordLabel/Field display, like so:

Username: usernameField
Password: passwordField

The code I am using is as follows:

layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(usernameLabel)
                .addComponent(usernameField) 
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField)
                    )
            );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(usernameLabel)
                    .addComponent(usernameField)
                     )
                .addGroup(layout.createSequentialGroup()
                    .addComponent(passwordLabel)
                    .addComponent(passwordLabel)
                    )
            );

This compiles but gives me the following errors:

Exception in thread "main" java.lang.IllegalStateException: javax.swing.JPasswordField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.apple.laf.AquaTextFieldBorder@5910e440,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=164,g=205,b=255],columns=20,columnWidth=0,command=,horizontalAlignment=LEADING,echoChar=●] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
    at java.awt.Container.layout(Container.java:1510)
    at java.awt.Container.doLayout(Container.java:1499)
    at java.awt.Container.validateTree(Container.java:1695)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validate(Container.java:1630)
    at java.awt.Container.validateUnconditionally(Container.java:1667)
    at java.awt.Window.show(Window.java:1033)
    at java.awt.Component.show(Component.java:1671)
    at java.awt.Component.setVisible(Component.java:1623)
    at java.awt.Window.setVisible(Window.java:1014)
    at xWing.GUIlogin.<init>(GUIlogin.java:107)
    at xWing.Driver.main(Driver.java:7)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JPasswordField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.apple.laf.AquaTextFieldBorder@5910e440,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=164,g=205,b=255],columns=20,columnWidth=0,command=,horizontalAlignment=LEADING,echoChar=●] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
    at java.awt.Container.layout(Container.java:1510)
    at java.awt.Container.doLayout(Container.java:1499)
    at java.awt.Container.validateTree(Container.java:1695)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validate(Container.java:1630)
    at java.awt.Window.dispatchEventImpl(Window.java:2744)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Why am I getting this error, and how do I fix it?

Resource 1: I compared my logic ( i.e. the grid logic I used to decide which group should be parallel / sequential ) to the answer in this and it seemed to be right.
Resource 2

Community
  • 1
  • 1
  • This is just my opinion, but GroupLayout doesn't lend itself well to hand coding and was designed for form editors, personally, I avoid it in favour of other options – MadProgrammer Jan 26 '17 at 02:13
  • @MadProgrammer Thanks for commenting =) Will take what you said into account. –  Jan 26 '17 at 03:50

2 Answers2

1

Take a look at this:

            .addGroup(layout.createSequentialGroup()
                .addComponent(passwordLabel)
                .addComponent(passwordLabel)
                )

That looks wrong. Why add the label twice?

And if one of those is supposed to be passwordField, that may explain why the exception says that there is a PasswordText object that is not attached to a vertical group.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

JPasswordField ... is not attached to a vertical group says clearly that it is not attached to vertical group.

You have typo here:

        .addGroup(layout.createSequentialGroup()
            .addComponent(passwordLabel)
            .addComponent(passwordLabel)
            )

Using group layout can by tricky. In my experience, i found that it is easier to use 3rd party layout manager for Swing applications like very good MigLayout It has plenty of documentation and is very easy to use. Try it out.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • +1. Could not choose both of your answers; accepted the oldest answer to break the tie. Thank you very much for commenting, and will check out MigLayout. –  Jan 26 '17 at 03:55