问题
I use a GroupLayout
for my panel :
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup()
.addComponent(title_panel)
.addComponent(subtitle_panel)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(title_panel)
.addComponent(subtitle_panel)
);
Even if I define both horizontal AND vertical groups, it still gives me a
java.lang.IllegalStateException
But not always. Sometimes it does it, sometimes it does not. And even if there is this exception, my GUI works and displays what I want.
I think I have a start of answer. When I display the frame on which the panel is for the first time, I do not have this exception. But, when I display it for the second time, it gives me this exception. It's like the program keeps a part of my groupLayout, or something like that, between two launchs of the frame.
It's quite frustrating, even if it works in spite of this exception ^^' I do not like when my own code trolls me!
Here is an example of the errors I get (derp is the name of my projet and Panel is a class inherited from JPanel) :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: derp.Panel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] 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.validate(Container.java:1623)
at javax.swing.RepaintManager$2.run(RepaintManager.java:679)
at javax.swing.RepaintManager$2.run(RepaintManager.java:677)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:676)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1650)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:100)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
juin 27, 2013 4:39:35 PM io.socket.IOConnection transportMessage
回答1:
I think the problem comes from these two line of code, "this" refers to the instance of your class, the parent of a GroupLayout must be a Swing container, a panel for example
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
try something like this
JComponent panel = ...;
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
来源:https://stackoverflow.com/questions/17345770/illegalstateexception-in-a-grouplayout