GroupLayout - IllegalStateException: JLabel is not attached to a horizontal group

Moin,

wo ist die schön einfache versprochene Welt der Layouts??


	private static final int	WIDTH	= 640;
	private static final int	HEIGHT	= 480;

	public AccountEditor(JFrame parent) {
		super(parent, true);

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);
		setTitle("Benutzeraccounts");

		int x = (parent.getX() + (parent.getWidth() / 2)) - (WIDTH / 2);
		int y = (parent.getY() + (parent.getHeight() / 2)) - (HEIGHT / 2);
		setBounds(x, y, WIDTH, HEIGHT);

		setLayout(new GridLayout(0, 2));
		add(scroller);
		add(createDetailEdits());

		setVisible(true);
	}



	private JScrollPane	scroller	= new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

	private JTextField	username	= new JTextField();
	private JTextField	password	= new JTextField();
	private JTextField	webaccess	= new JTextField();



	@Override
	public void windowClosing(WindowEvent e) {}
	@Override
	public void windowOpened(WindowEvent e) {}
	@Override
	public void windowClosed(WindowEvent e) {}
	@Override
	public void windowIconified(WindowEvent e) {}
	@Override
	public void windowDeiconified(WindowEvent e) {}
	@Override
	public void windowActivated(WindowEvent e) {}
	@Override
	public void windowDeactivated(WindowEvent e) {}



	private JPanel createDetailEdits() {
		JPanel panel = new JPanel();
		GroupLayout layout = new GroupLayout(panel);
		panel.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		SequentialGroup group = layout.createSequentialGroup();
		group.addGroup(layout.createParallelGroup().addComponent(new JLabel("Benutzername")).addComponent(username));
		group.addGroup(layout.createParallelGroup().addComponent(new JLabel("Passwort")).addComponent(password));
		
		layout.setVerticalGroup(group);
		
		return panel;
	}

}```

Eigentlich will ich nur neben der ScrollPane noch ein paar Eingabefelder haben. Aber das klappt nicht. Ich habe das Wiki durchforstet, Oracle bemüht. Aber ich bekomme ständig eine IllegalStateException bei `setVisble(true);`


Exception in thread “AWT-EventQueue-0” java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@22ea9129,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Passwort,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Container.validateUnconditionally(Container.java:1660)
at java.awt.Dialog.conditionalShow(Dialog.java:908)
at java.awt.Dialog.show(Dialog.java:1043)
at java.awt.Component.show(Component.java:1651)
at java.awt.Component.setVisible(Component.java:1603)
at java.awt.Window.setVisible(Window.java:1014)
at java.awt.Dialog.setVisible(Dialog.java:1005)
at de.x8bit.free.workspace.desktop.accounts.AccountEditor.(AccountEditor.java:42)
at de.x8bit.free.workspace.desktop.GUI.openAccountEditor(GUI.java:88)
at de.x8bit.free.workspace.desktop.GUI$1.actionPerformed(GUI.java:70)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)



*argh* Layouts hassen mich - und ich wette das ist nur eine Kleinigkeit -.-

hand, mogel

Einmal Form- oder MigLayout verstehen und nie wieder Probleme. :slight_smile:

How to Use GroupLayout:

"GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. You do not need to worry about the vertical dimension when defining the horizontal layout, and vice versa, as the layout along each axis is totally independent of the layout along the other axis.

When focusing on just one dimension, you only have to solve half the problem at one time. This is easier than handling both dimensions at once. This means, of course, that each component needs to be defined twice in the layout. If you forget to do this, GroupLayout will generate an exception"

Einmal vertikal und einmal horizontal layouten. Horizontal fehlt.

“§$%&/(/&%$§”$%&/(

		JLabel lblusername = new JLabel("Benutzername");
		JLabel lblpassword = new JLabel("Passwort");
		
		JPanel panel = new JPanel();
		GroupLayout layout = new GroupLayout(panel);
		panel.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		layout.setHorizontalGroup(
			layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
					.addComponent(lblusername)
					.addComponent(lblpassword)
					)
				.addGroup(layout.createParallelGroup()
					.addComponent(username)
					.addComponent(password)
					)
			);
		
		layout.setVerticalGroup(
			layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
					.addComponent(lblusername)
					.addComponent(username)
					)
				.addGroup(layout.createParallelGroup()
						.addComponent(lblpassword)
						.addComponent(password)
						)
			);
		
		return panel;
	}```

Ich habe den Quelltext jetzt etwas umgestrickt. Zum Einen die JLabel vorher erzeugt und zum Anderen das ganze doppelt definiert.

danke, mogel