0

I know this have been asked in someway state or form and I looked at others, but I can't find why I am getting this error message. I'm not too well studied with GUIs and this is one of my first GUI projects I am working with. Error Code: Exception in thread

"AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Label 1,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a horizontal group
        at javax.swing.GroupLayout.checkComponents(Unknown Source)
        at javax.swing.GroupLayout.prepare(Unknown Source)
        at javax.swing.GroupLayout.layoutContainer(Unknown Source)
        at java.awt.Container.layout(Unknown Source)
        at java.awt.Container.doLayout(Unknown Source)
        at java.awt.Container.validateTree(Unknown Source)
        at java.awt.Container.validateTree(Unknown Source)
        at java.awt.Container.validateTree(Unknown Source)
        at java.awt.Container.validateTree(Unknown Source)
        at java.awt.Container.validateTree(Unknown Source)
        at java.awt.Container.validate(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Code:

package GUI;

import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;

import java.awt.*;

public class GUI extends JFrame {
    public GUI() {
        JButton b1 = new JButton("1"),b2=new JButton("2"),b3=new JButton("3"),b4=new JButton("4"),b5=new JButton("5"),b6=new JButton("6");
        JLabel l1= new JLabel("Label 1"),l2= new JLabel("Label 2"),l3=new JLabel("Label 3"),l4=new JLabel("Label 4"),l5=new JLabel("Label 5"),l6=new JLabel("Label 6");
        JPanel pane = new JPanel();
        setTitle("Hello World");
        setSize(500,500);
        setVisible(true);

        GroupLayout layout = new GroupLayout(pane);
        pane.setLayout(layout);
        getContentPane().add(pane);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(LEADING) 
                        .addComponent(b1)
                        .addComponent(b2)
                        .addComponent(b3)
                        )
                .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(l1)
                        .addComponent(l3)
                        .addComponent(l5)
                        )
                .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(l2)
                        .addComponent(l4)
                        .addComponent(l6)
                        )
                .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(b4)
                        .addComponent(b5)
                        .addComponent(b6)
                        ));
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(b1)
                        .addComponent(l1)
                        .addComponent(l2)
                        .addComponent(b4)
                        )
                .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(b2)
                        .addComponent(l3)
                        .addComponent(l4)
                        .addComponent(b5)
                        )
                .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(b3)
                        .addComponent(l5)
                        .addComponent(l6)
                        .addComponent(b6)
                        )
                );
    }

}
Matthew
  • 3,136
  • 3
  • 18
  • 34
  • 1
    I have no idea how GroupLayouts works since it is generally used by IDE's that generate code for you. You are trying to use it manually and are probably not specifying some constraint properly. Can't tell what kind of layout your are trying to get, but maybe a GridLayout or GridBagLayout would be easier. Read the Swing tutorial on layout mangers for working examples. – camickr Oct 23 '15 at 23:18
  • So would you just recommend doing it in Eclipse GUI builder? I just don't understand how that works easily (I'm making it seems more complex than it is, lol). – Matthew Oct 23 '15 at 23:19
  • 1
    I have never used a GUI builder so no I don't recommend that. I can never understand the code generated by the IDE. Once the code is generated it can only ever be maintained in that IDE. I recommend you do the layout manually by using other layout managers or a combination of layout managers to get your desired layout. – camickr Oct 23 '15 at 23:45
  • Check [this](http://stackoverflow.com/a/21030105/2737933) out as an alternative to `GroupLayout`. You could probably debug the code you submitted, but `GridBagLayout` is a better way to go since you call the shots. It just takes a little planning. The learning curve is pretty flat until you get the hang of it, but it's worth it. – DSlomer64 Oct 23 '15 at 23:57
  • GroupLayout is the least dynamic of all the layouts – MadProgrammer Oct 24 '15 at 00:49
  • Oh, is it really? Which is the most dynamic? I thought when I read it, it seemed the most dynamic. – Matthew Oct 24 '15 at 02:30

0 Answers0