Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton -
i need solve error.
the window need register beer not openning.
i'm creating simple system can register , search beer's aparrently.
here code.
package cadastro; import java.awt.container; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jframe; public class entrada extends jframe { private static final long serialversionuid = 42l; private final container container; private final jbutton btcadastrar; private final jbutton btpesquisar; private final jbutton btsair; public entrada() { super("sistema de avaliação de cervejas"); container = getcontentpane(); container.setlayout(new gridlayout(0,1)); gerenciadorbotoes btmanager = new gerenciadorbotoes(); btcadastrar = new jbutton(); btpesquisar = new jbutton(); btsair = new jbutton(); btcadastrar.settext("cadastrar cervejas"); btcadastrar.setactioncommand("1"); btpesquisar.settext("pesquisar cervejas"); btpesquisar.setactioncommand("2"); btsair.settext("sair"); btsair.setactioncommand("3"); btcadastrar.addactionlistener(btmanager); btpesquisar.addactionlistener(btmanager); btsair.addactionlistener(btmanager); container.add(btcadastrar); container.add(btpesquisar); container.add(btsair); setdefaultcloseoperation(jframe.exit_on_close); setbounds(new java.awt.rectangle(300, 150, 0, 0)); setsize(300, 200); setresizable(false); } private class gerenciadorbotoes implements actionlistener { @override public void actionperformed(actionevent e) { if (null != e.getactioncommand()) { if ("1".equals(e.getactioncommand())) { new cadastra().setvisible(true); } else { if ("2".equals(e.getactioncommand())) { new pesquisar().setvisible(true); } else { } } } } } static void abrir() { try { acervo.getinstance().open(new file("agenda.poo")); } catch (ioexception | classnotfoundexception e) { } } public static void main(string args[]) { entrada.abrir(); java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new entrada().setvisible(true); } }); }
}
and here other.
package cadastro; import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jscrollpane; import javax.swing.jcombobox; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.joptionpane; public class cadastra extends jframe { private final int id = 0; private jbutton btcadastrar; private jbutton btlimpar; private jlabel lbnome; private jlabel lbempresa; private jlabel lbtipo; private jlabel lbnota; private jlabel lbcomentario; private jscrollpane scrollcoment; private jcombobox cbnota; private jtextarea tfcomentario; private jtextfield tfempresa; private jtextfield tftipo; private jtextfield tfnome; public cadastra() { initcomponents(); } private void initcomponents() { lbnome = new jlabel(); tfnome = new jtextfield(); lbempresa = new jlabel(); tfempresa = new jtextfield(); lbtipo = new jlabel(); tftipo = new jtextfield(); lbnota = new jlabel(); lbcomentario = new jlabel(); scrollcoment = new jscrollpane(); tfcomentario = new jtextarea(); cbnota = new jcombobox(); btcadastrar = new jbutton(); btlimpar = new jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.dispose_on_close); settitle("cadastrar"); setbounds(new java.awt.rectangle(300, 150, 0, 0)); setpreferredsize(new java.awt.dimension(700, 452)); setresizable(false); addwindowlistener(new java.awt.event.windowadapter() { @override public void windowclosing(java.awt.event.windowevent evt) { windowclose(evt); } }); lbnome.settext("nome da cerveja"); lbempresa.settext("empresa"); tfempresa.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { txtnaactionperformed(evt); } }); lbtipo.settext("tipo"); lbnota.settext("nota"); lbcomentario.settext("comentário"); tfcomentario.setcolumns(20); tfcomentario.setrows(5); scrollcoment.setviewportview(tfcomentario); cbnota.setmodel(new javax.swing.defaultcomboboxmodel(new string[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"})); cbnota.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { notaactionperformed(evt); } }); btcadastrar.settext("cadastrar"); btcadastrar.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); btlimpar.settext("limpar"); btlimpar.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { jbutton2actionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading). addgroup(layout.createsequentialgroup() .addcomponent(lbnome) .addgap(18, 18, 18) .addcomponent(tfnome, javax.swing.grouplayout.preferred_size, 130, javax.swing.grouplayout.preferred_size)) .addgroup(layout.createsequentialgroup() .addcomponent(lbtipo) .addgap(18, 18, 18) .addcomponent(tftipo, javax.swing.grouplayout.preferred_size, 130, javax.swing.grouplayout.preferred_size))) .addgap(79, 79, 79) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(lbnota) .addgap(18, 18, 18) .addcomponent(cbnota, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addgap(173, 173, 173)) .addgroup(layout.createsequentialgroup() .addcomponent(lbempresa) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addcomponent(tfempresa, javax.swing.grouplayout.default_size, 149, short.max_value) .addcontainergap()))) .addgroup(layout.createsequentialgroup() .addcomponent(lbcomentario) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(scrollcoment) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(lbempresa) .addcomponent(lbnome)) .addgap(82, 82, 82))))); layout.linksize(javax.swing.swingconstants.horizontal, new java.awt.component[]{btcadastrar, btlimpar}); layout.setverticalgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(lbnome) .addcomponent(tfnome, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(lbempresa) .addcomponent(tfempresa, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(26, 26, 26) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(lbtipo) .addcomponent(tftipo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(lbnota) .addcomponent(cbnota, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(30, 30, 30) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(lbcomentario) .addgroup(layout.createsequentialgroup() .addgap(25, 25, 25) .addcomponent(btcadastrar) .addgap(30, 30, 30) .addcomponent(btlimpar)) .addcomponent(scrollcoment, javax.swing.grouplayout.preferred_size, 125, javax.swing.grouplayout.preferred_size)) .addcontainergap(150, short.max_value))); pack(); } private void notaactionperformed(java.awt.event.actionevent evt) { // } private void jbutton2actionperformed(java.awt.event.actionevent evt) { // apaga os caracteres escritos tfnome.settext(""); tfempresa.settext(""); tftipo.settext(""); tfcomentario.settext(""); cbnota.setselectedindex(0); } private void jbutton1actionperformed(java.awt.event.actionevent evt) { // gravar os dados cadastrados string nome = tfnome.gettext(); string nomeempresa = tfempresa.gettext(); string tipo = tftipo.gettext(); string comentario = tfcomentario.gettext(); int selecionado = cbnota.getselectedindex(); string snota = integer.tostring(selecionado + 1); if (nome == null || nome.trim().equals("")) { // se o nome null ou o nome, retirando os espaços em branco, igual "" joptionpane.showmessagedialog(this, "não foi possível adicionar. forneça o nome da cerveja!", "erro", joptionpane.error_message); } else { boolean temcerveja = acervo.getinstance().temcerveja(nome); if (temcerveja) { joptionpane.showmessagedialog(this, "não foi possível adicionar. cerveja com mesmo nome já cadastrado!", "aviso", joptionpane.warning_message); } else { cervejaatual cerveja = new cervejaatual(nome ,nomeempresa,tipo , comentario, snota); acervo acervo = acervo.getinstance(); acervo.addcerveja(cerveja); joptionpane.showmessagedialog(this, "adicionado com sucesso!", "informação", joptionpane.information_message); this.dispose(); } } } private void txtnaactionperformed(java.awt.event.actionevent evt) { } private void windowclose(java.awt.event.windowevent evt) { }
}
the error.
exception in thread "awt-eventqueue-0" java.lang.illegalstateexception: javax.swing.jbutton[,0,0,0x0,invalid,alignmentx=0.0,alignmenty=0.5,border=javax.swing.plaf.borderuiresource$compoundborderuiresource@7e83ef0,flags=296,maximumsize=,minimumsize=,preferredsize=,defaulticon=,disabledicon=,disabledselectedicon=,margin=javax.swing.plaf.insetsuiresource[top=2,left=14,bottom=2,right=14],paintborder=true,paintfocus=true,pressedicon=,rolloverenabled=true,rollovericon=,rolloverselectedicon=,selectedicon=,text=limpar,defaultcapable=true] not attached horizontal group @ javax.swing.grouplayout.checkcomponents(grouplayout.java:1086) @ javax.swing.grouplayout.prepare(grouplayout.java:1040) @ javax.swing.grouplayout.layoutcontainer(grouplayout.java:910) @ java.awt.container.layout(container.java:1510) @ java.awt.container.dolayout(container.java:1499) @ java.awt.container.validatetree(container.java:1695) @ java.awt.container.validatetree(container.java:1704) @ java.awt.container.validatetree(container.java:1704) @ java.awt.container.validatetree(container.java:1704) @ java.awt.container.validate(container.java:1630) @ java.awt.container.validateunconditionally(container.java:1667) @ java.awt.window.pack(window.java:818) @ cadastro.cadastra.initcomponents(cadastra.java:173) @ cadastro.cadastra.<init>(cadastra.java:36) @ cadastro.entrada$gerenciadorbotoes.actionperformed(entrada.java:66) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2348) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6535) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3324) @ java.awt.component.processevent(component.java:6300) @ java.awt.container.processevent(container.java:2236) @ java.awt.component.dispatcheventimpl(component.java:4891) @ java.awt.container.dispatcheventimpl(container.java:2294) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4888) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4525) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4466) @ java.awt.container.dispatcheventimpl(container.java:2280) @ java.awt.window.dispatcheventimpl(window.java:2750) @ java.awt.component.dispatchevent(component.java:4713) @ 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$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$javasecurityaccessimpl.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$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ 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)
these lines:
.addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(lbempresa) .addcomponent(lbnome))
and these lines:
.addgroup(layout.createsequentialgroup() .addgap(25, 25, 25) .addcomponent(btcadastrar) .addgap(30, 30, 30) .addcomponent(btlimpar))
you have change lbempresa , lbnome btcadastrar , btlimpar.
Comments
Post a Comment