GroupLayout giving error with java swing

John picture John · Jan 14, 2011 · Viewed 8.6k times · Source

I am getting a long list of errors. Can anyone suggest to me where I am going wrong with GroupLayout.

layout.setHorizontalGroup(
    layout.createSequentialGroup()
        .addComponent(new JLabel("Enter Book Name"))
        .addComponent(new JTextField("TextField", 20))
);

Error:

    Exception in thread "main" java.lang.IllegalStateException:
        javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.
            plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,
            alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$
            CompoundBorderUIResource@3d0bbf9e,flags=296,maximumSize=,
            minimumSize=,preferredSize=,caretColor=sun.swing.
            PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax
            .swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true
            ,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,
            bottom=0,right=0],selectedTextColor=sun.swing.
            PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.
            swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=20,
            columnWidth=0,command=,horizontalAlignment=LEADING]
            is not attached to a vertical group
            at javax.swing.GroupLayout.checkComponents(Unknown Source)
            at javax.swing.GroupLayout.prepare(Unknown Source)

Answer

Erick Robertson picture Erick Robertson · Feb 13, 2011

When you use GroupLayout, you have to specify a Horizontal and a Vertical layout. Each component must be specified exactly once in each group. Not doing this causes this error.

Additionally, make sure you cross hatch each item properly. If you're making.a grid, be sure that both directions are sequences of parallels or vice versa. If you are making one row of buttons or something, make sure they are parallel one way and sequential another.