java - Populating Text area using button slection -
i'm trying have text displayed depending on button selected, can't seem work,
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ package nameage; /** * * @author eddie */ public class nameage extends javax.swing.jframe { /** * creates new form nameage */ public nameage() { initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { buttongroup1 = new javax.swing.buttongroup(); jlabel1 = new javax.swing.jlabel(); jtextfield1 = new javax.swing.jtextfield(); jlabel2 = new javax.swing.jlabel(); jtextpane1 = new javax.swing.jtextpane(); jbutton1 = new javax.swing.jbutton(); jbutton2 = new javax.swing.jbutton(); jbutton3 = new javax.swing.jbutton(); jbutton4 = new javax.swing.jbutton(); jbutton5 = new javax.swing.jbutton(); jbutton6 = new javax.swing.jbutton(); jbutton7 = new javax.swing.jbutton(); jbutton8 = new javax.swing.jbutton(); jbutton9 = new javax.swing.jbutton(); jbutton10 = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jlabel1.settext("name:"); jlabel2.settext("age group:"); buttongroup1.add(jbutton1); jbutton1.setlabel("0 - 19"); jbutton2.settext("20 - 29"); buttongroup1.add(jbutton2); jbutton3.settext("30 - 39"); buttongroup1.add(jbutton3); jbutton4.settext("40 - 49"); buttongroup1.add(jbutton4); jbutton5.settext("50 - 59"); buttongroup1.add(jbutton5); jbutton6.settext("70 - 79"); buttongroup1.add(jbutton6); jbutton7.settext("60 - 69"); buttongroup1.add(jbutton7); jbutton8.settext("80 - 89"); buttongroup1.add(jbutton8); jbutton9.settext("90 - 99"); buttongroup1.add(jbutton9); jbutton10.settext("100 +"); buttongroup1.add(jbutton10); 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() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jlabel1) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, 245, javax.swing.grouplayout.preferred_size)) .addcomponent(jlabel2)) .addgap(17, 17, 17)) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addcomponent(jbutton1) .addcomponent(jbutton7)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false) .addgroup(layout.createsequentialgroup() .addcomponent(jbutton6) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton8) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton9) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton10, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)) .addgroup(layout.createsequentialgroup() .addcomponent(jbutton2) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton3) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton4) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton5))) .addgap(0, 0, short.max_value))) .addcontainergap(javax.swing.grouplayout.default_size, short.max_value)) .addgroup(layout.createsequentialgroup() .addcomponent(jtextpane1, javax.swing.grouplayout.preferred_size, 357, javax.swing.grouplayout.preferred_size) .addcontainergap(12, short.max_value)))) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel1) .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addcomponent(jlabel2) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jbutton1) .addcomponent(jbutton2) .addcomponent(jbutton3) .addcomponent(jbutton4) .addcomponent(jbutton5)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jbutton6) .addcomponent(jbutton7) .addcomponent(jbutton8) .addcomponent(jbutton9) .addcomponent(jbutton10)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jtextpane1, javax.swing.grouplayout.preferred_size, 126, javax.swing.grouplayout.preferred_size) .addcontainergap(13, short.max_value)) ); pack(); }// </editor-fold> //this trying display private void jbutton1actionperformed(java.awt.event.actionevent evt) { jtextpane1.settext("hello " + jtextfield1 + "\n you've still got lot of livin' left do!"); } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ 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(nameage.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(nameage.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(nameage.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(nameage.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new nameage().setvisible(true); } }); } // variables declaration - not modify private javax.swing.buttongroup buttongroup1; private javax.swing.jbutton jbutton1; private javax.swing.jbutton jbutton10; private javax.swing.jbutton jbutton2; private javax.swing.jbutton jbutton3; private javax.swing.jbutton jbutton4; private javax.swing.jbutton jbutton5; private javax.swing.jbutton jbutton6; private javax.swing.jbutton jbutton7; private javax.swing.jbutton jbutton8; private javax.swing.jbutton jbutton9; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jtextfield jtextfield1; private javax.swing.jtextpane jtextpane1; // end of variables declaration }
i suspect print
hello avax.swing.jtextfield[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.basictextui$updatehandler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.borderuiresource$compoundborderuiresource@58372a00,flags=296,maximumsize=,minimumsize=,preferredsize=,caretcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],disabledtextcolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.insetsuiresource[top=0,left=0,bottom=0,right=0],selectedtextcolor=sun.swing.printcoloruiresource[r=51,g=51,b=51],selectioncolor=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],columns=0,columnwidth=0,command=,horizontalalignment=leading] you've still got lot of livin' left do!
or such.
this because you're using
jtextpane1.settext("hello " + jtextfield1 + "\n you've still got lot of livin' left do!");
which automatically calls jtextfield1
's tostring
method (nice feature when you're use it).
instead should using jtextfield#gettext
text field contains, example...
jtextpane1.settext("hello " + jtextfield1.gettext() + "\n you've still got lot of livin' left do!");
the other problem is, there nothing calling jbutton1actionperformed
method print anything.
you need attach actionlistener
buttons , have them call method update text...
for example...
jbutton1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { jbutton1actionperformed(e); } });
you'll need each button...
now, suggest put away form designer , take time read through creating gui jfc/swing. give greater appreciation of how swing guis created , how tool form designer can used.
learn cut wood saw before use bandsaw...otherwise you're loose limbs ;)
Comments
Post a Comment