multithreading - working with Threads "AWT-EventQueue-0" java.lang.IllegalStateException -
note:i put complete code example make executable user; real code long!
i'm obtaining exceptions sometimes:
exception in thread "awt-eventqueue-0" java.lang.illegalstateexception: javax.swing.jpanel[,0,0,0x0,invalid,layout=java.awt.flowlayout, alignmentx=0.0, alignmenty=0.0,border=javax.swing.plaf.synth.synthborder@24dab6, flags=9,maximumsize=,minimumsize=,preferredsize=] not attached horizontal group exception in thread "awt-eventqueue-0" java.lang.illegalstateexception: javax.swing.jpanel[,0,0,0x0,invalid,layout=java.awt.flowlayout, alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.synth.synthborder@bab939, flags=9,maximumsize=,minimumsize=,preferredsize=] not attached vertical group
i'm working threads performs task.. this
i need put inside 1 panel other new panel when events occurs
private void insertpanelinpanel(jpanel jpcontainer, jpanel jpcontained) { grouplayout jpcontainerlayout = (grouplayout)jpcontainer.getlayout(); jpcontainer.removeall(); //grouplayout jpcontainerlayout = new grouplayout(jpcontainer); //jpcontainer.setlayout(jpcontainerlayout); jpcontainerlayout.sethorizontalgroup( jpcontainerlayout.createparallelgroup() .addcomponent(jpcontained) ); jpcontainerlayout.setverticalgroup( jpcontainerlayout.createparallelgroup() .addcomponent(jpcontained) ); }
when jframe resized, componentresized thrown.....
package test; import java.awt.*; import java.util.*; import java.util.list; import java.util.map.entry; import javax.swing.*; public class newjframe extends javax.swing.jframe { thread thrdone = null; thread thrdtwo = null; public newjframe() { initcomponents(); } private boolean isrunningplotthreads() { boolean isrunning = false; if (!(thrdone == null)) { if (thrdone.getstate() == thread.state.terminated) { thrdone = null; } else { isrunning = true; } } if (!(thrdtwo == null)) { if (thrdtwo.getstate() == thread.state.terminated) { thrdtwo = null; } else { isrunning = true; } } return isrunning; } private void insertpanelinpanel(jpanel jpcontainer, jpanel jpcontained) { grouplayout jpcontainerlayout = (grouplayout)jpcontainer.getlayout(); jpcontainer.removeall(); // grouplayout jpcontainerlayout = new grouplayout(jpcontainer); // jpcontainer.setlayout(jpcontainerlayout); jpcontainerlayout.sethorizontalgroup( jpcontainerlayout.createparallelgroup() .addcomponent(jpcontained) ); jpcontainerlayout.setverticalgroup( jpcontainerlayout.createparallelgroup() .addcomponent(jpcontained) ); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jtpbot = new javax.swing.jtabbedpane(); jpone = new javax.swing.jpanel(); jbfirst = new javax.swing.jbutton(); jponeplot = new javax.swing.jpanel(); jptwo = new javax.swing.jpanel(); jbtwo = new javax.swing.jbutton(); jptwoplot = new javax.swing.jpanel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); setbackground(new java.awt.color(255, 255, 255)); addcomponentlistener(new java.awt.event.componentadapter() { public void componentresized(java.awt.event.componentevent evt) { formcomponentresized(evt); } }); jtpbot.setbackground(new java.awt.color(0, 0, 0)); jtpbot.setborder(javax.swing.borderfactory.createbevelborder(javax.swing.border.bevelborder.raised)); jpone.setbackground(new java.awt.color(0, 191, 191)); jbfirst.settext("first"); jbfirst.setmaximumsize(new java.awt.dimension(70, 23)); jbfirst.setminimumsize(new java.awt.dimension(70, 23)); jbfirst.setpreferredsize(new java.awt.dimension(70, 23)); jbfirst.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbfirstactionperformed(evt); } }); jponeplot.setbackground(new java.awt.color(191, 191, 191)); jponeplot.setborder(javax.swing.borderfactory.createetchedborder()); javax.swing.grouplayout jponeplotlayout = new javax.swing.grouplayout(jponeplot); jponeplot.setlayout(jponeplotlayout); jponeplotlayout.sethorizontalgroup( jponeplotlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 291, short.max_value) ); jponeplotlayout.setverticalgroup( jponeplotlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 0, short.max_value) ); javax.swing.grouplayout jponelayout = new javax.swing.grouplayout(jpone); jpone.setlayout(jponelayout); jponelayout.sethorizontalgroup( jponelayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jponelayout.createsequentialgroup() .addcontainergap() .addcomponent(jbfirst, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jponeplot, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcontainergap()) ); jponelayout.setverticalgroup( jponelayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jponelayout.createsequentialgroup() .addcontainergap() .addgroup(jponelayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jponeplot, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addgroup(jponelayout.createsequentialgroup() .addcomponent(jbfirst, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addgap(0, 143, short.max_value))) .addcontainergap()) ); jtpbot.addtab("one", jpone); jptwo.setbackground(new java.awt.color(191, 0, 191)); jbtwo.settext("two"); jbtwo.setmaximumsize(new java.awt.dimension(70, 23)); jbtwo.setminimumsize(new java.awt.dimension(70, 23)); jbtwo.setpreferredsize(new java.awt.dimension(70, 23)); jbtwo.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbtwoactionperformed(evt); } }); jptwoplot.setbackground(new java.awt.color(191, 191, 191)); jptwoplot.setborder(javax.swing.borderfactory.createetchedborder()); javax.swing.grouplayout jptwoplotlayout = new javax.swing.grouplayout(jptwoplot); jptwoplot.setlayout(jptwoplotlayout); jptwoplotlayout.sethorizontalgroup( jptwoplotlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 291, short.max_value) ); jptwoplotlayout.setverticalgroup( jptwoplotlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 0, short.max_value) ); javax.swing.grouplayout jptwolayout = new javax.swing.grouplayout(jptwo); jptwo.setlayout(jptwolayout); jptwolayout.sethorizontalgroup( jptwolayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jptwolayout.createsequentialgroup() .addcontainergap() .addcomponent(jbtwo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jptwoplot, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcontainergap()) ); jptwolayout.setverticalgroup( jptwolayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jptwolayout.createsequentialgroup() .addcontainergap() .addgroup(jptwolayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jptwoplot, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addgroup(jptwolayout.createsequentialgroup() .addcomponent(jbtwo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addgap(0, 143, short.max_value))) .addcontainergap()) ); jtpbot.addtab("two", jptwo); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jtpbot) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(69, 69, 69) .addcomponent(jtpbot) .addcontainergap()) ); pack(); }// </editor-fold> private void formcomponentresized(java.awt.event.componentevent evt) { if (jtpbot.getselectedindex() == jtpbot.indexoftab("one")) { if (jbfirst.isenabled()) { jbfirstactionperformed(null); } } if (jtpbot.getselectedindex() == jtpbot.indexoftab("two")) { if (jbtwo.isenabled()) { jbtwoactionperformed(null); } } } private void jbfirstactionperformed(java.awt.event.actionevent evt) { try { system.out.println("jbfirstactionperformed"); final jpanel jponenew = new jpanel(); int c = 32+(int)(208*math.random()); jponenew.setbackground(new color(c,0,0)); runnable plotone = new runnable() { public void run() { try { thread.sleep((int)(500*math.random())); //simulate time proccesing insertpanelinpanel(jponeplot, jponenew); } catch (exception ioe) { system.out.println("-insertpanelinpanel(jponeplot, jponenew):"+ioe.tostring()); } } }; if (!isrunningplotthreads()) { thrdone = new thread(plotone); thrdone.start(); } } catch (exception ioe) { system.out.println("jponenew:"+ioe.tostring()); } } private void jbtwoactionperformed(java.awt.event.actionevent evt) { try { system.out.println("jbtwoactionperformed"); final jpanel jptwonew = new jpanel(); int c = 32+(int)(208*math.random()); jptwonew.setbackground(new color(0,c,0)); runnable plottwo = new runnable() { public void run() { try { thread.sleep((int)(500*math.random())); //simulate time proccesing insertpanelinpanel(jptwoplot, jptwonew); } catch (exception ioe) { system.out.println("-insertpanelinpanel(jptwoplot, jptwonew):"+ioe.tostring()); } } }; if (!isrunningplotthreads()) { thrdtwo = new thread(plottwo); thrdtwo.start(); } } catch (exception ioe) { system.out.println("jptwonew:"+ioe.tostring()); } } public static void main(string args[]) { //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> java.awt.eventqueue.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); } boolean bcaniuseone = false; boolean bcaniusetwo = false; boolean bthreadfree = true; boolean bmousefree = false; private javax.swing.jbutton jbfirst; private javax.swing.jbutton jbtwo; private javax.swing.jpanel jpone; private javax.swing.jpanel jponeplot; private javax.swing.jpanel jptwo; private javax.swing.jpanel jptwoplot; private javax.swing.jtabbedpane jtpbot; }
output
jbfirstactionperformed jbfirstactionperformed jbtwoactionperformed jbtwoactionperformed exception in thread "awt-eventqueue-0" java.lang.illegalstateexception: javax.swing.jpanel[,0,0,0x0,invalid,layout=java.awt.flowlayout,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.synth.synthborder@fa9981,flags=9,maximumsize=,minimumsize=,preferredsize=] not attached horizontal group @ javax.swing.grouplayout.checkcomponents(grouplayout.java:1086) @ javax.swing.grouplayout.prepare(grouplayout.java:1040) @ javax.swing.grouplayout.minimumlayoutsize(grouplayout.java:896) @ java.awt.container.minimumsize(container.java:1846) @ java.awt.container.getminimumsize(container.java:1830) @ javax.swing.jcomponent.getminimumsize(jcomponent.java:1746) @ javax.swing.grouplayout$componentspring.calculatenonlinkedminimumsize(grouplayout.java:3008) @ javax.swing.grouplayout$componentspring.calculateminimumsize(grouplayout.java:2972) @ javax.swing.grouplayout$spring.getminimumsize(grouplayout.java:1336) @ javax.swing.grouplayout$group.getspringsize(grouplayout.java:1636) @ javax.swing.grouplayout$group.calculatesize(grouplayout.java:1627) @ javax.swing.grouplayout$group.calculateminimumsize(grouplayout.java:1598) @ javax.swing.grouplayout$spring.getminimumsize(grouplayout.java:1336) @ javax.swing.grouplayout$group.getspringsize(grouplayout.java:1636) @ javax.swing.grouplayout$group.calculatesize(grouplayout.java:1622) @ javax.swing.grouplayout$group.calculateminimumsize(grouplayout.java:1598) @ javax.swing.grouplayout$parallelgroup.calculateminimumsize(grouplayout.java:2569) @ javax.swing.grouplayout$spring.getminimumsize(grouplayout.java:1336) @ javax.swing.grouplayout$group.getspringsize(grouplayout.java:1636) @ javax.swing.grouplayout$group.calculatesize(grouplayout.java:1622) @ javax.swing.grouplayout$group.calculateminimumsize(grouplayout.java:1598) @ javax.swing.grouplayout$spring.getminimumsize(grouplayout.java:1336) @ javax.swing.grouplayout.calculateautopadding(grouplayout.java:1068) @ javax.swing.grouplayout.prepare(grouplayout.java:1058) @ javax.swing.grouplayout.minimumlayoutsize(grouplayout.java:896) @ java.awt.container.minimumsize(container.java:1846) @ java.awt.container.getminimumsize(container.java:1830) @ javax.swing.jcomponent.getminimumsize(jcomponent.java:1746) @ javax.swing.plaf.basic.basictabbedpaneui$tabbedpanelayout.calculatesize(basictabbedpaneui.java:2303) @ javax.swing.plaf.basic.basictabbedpaneui$tabbedpanelayout.minimumlayoutsize(basictabbedpaneui.java:2282jbtwoactionperformed ) @ java.awt.container.minimumsize(container.java:1846) jbtwoactionperformed @ java.awt.container.getminimumsize(container.java:1830) @ javax.swing.jcomponent.getminimumsize(jcomponent.java:1746) jbtwoactionperformed @ javax.swing.grouplayout$componentspring.calculatenonlinkedminimumsize(grouplayout.java:3008) @ javax.swing.grouplayout$componentspring.calculateminimumsize(grouplayout.java:2972) @ javax.swing.grouplayout$spring.getminimumsize(grouplayout.java:1336) @ javax.swing.grouplayout$componentspring.calculatepreferredsize(grouplayout.java:2979) @ javax.swing.grouplayout$spring.getpreferredsize(grouplayout.java:1346) @ javax.swing.grouplayout$group.getspringsize(grouplayout.java:1638) @ javax.swing.grouplayout$group.calculatesize(grouplayout.java:1622) @ javax.swing.grouplayout$group.calculatepreferredsize(grouplayout.java:1602) @ javax.swing.grouplayout$spring.getpreferredsize(grouplayout.java:1346) @ javax.swing.grouplayout$group.getspringsize(grouplayout.java:1638) @ javax.swing.grouplayout$group.calculatesize(grouplayout.java:1622) @ javax.swing.grouplayout$group.calculatepreferredsize(grouplayout.java:1602) @ javax.swing.grouplayout$spring.getpreferredsize(grouplayout.java:1346) @ javax.swing.grouplayout$sequentialgroup.setvalidsize(grouplayout.java:2017) @ javax.swing.grouplayout$group.setsize(grouplayout.java:1587jbtwoactionperformed ) @ javax.swing.grouplayout.calculateautopadding(grouplayout.java:1079) @ javax.swing.grouplayout.layoutcontainer(grouplayout.java:918) @ java.awt.container.layout(container.java:1508) @ java.awt.container.dolayout(container.java:1497) @ java.awt.container.validatetree(container.java:1693jbtwoactionperformed ) @ java.awt.container.validatetree(container.java:1702) @ java.awt.container.validatetree(container.java:1702) @ java.awt.container.validatetree(container.java:1702) @ java.awt.container.validate(container.java:1628) @ java.awt.window.dispatcheventimpl(window.java:2748) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:758) @ java.awt.eventqueue.access$500(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:709) @ java.awt.eventqueue$3.run(eventqueue.java:703) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:731) @ java.awt.eventqueue$4.run(eventqueue.java:729) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.awt.eventqueue.dispatchevent(eventqueue.java:728) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) jbtwoactionperformed
questions
what wrong?
how prevent exception thrown?
how can catch such exceptions?
try this!!!
private void insertpanelinpanel(jpanel jpcontainer, jpanel jpcontained) { jpcontainer.removeall(); jpcontainer.setlayout(new java.awt.gridlayout(1,1)); jpcontainer.add(jpcontained); }
Comments
Post a Comment