swing - How to add text to a single text field from many buttons ?? -- Java -
as can read title, need set text of jtextfield pressing buttons.. , buttons should disappear when clicked(which did method 'setvisible'), when delete letter button must appear again..
but focus on "how add text single text field many buttons".. each button when pressed must add letter,
the code did far// note : i'm working on netbeans ide
public class test extends javax.swing.jpanel { /** * creates new form test */ public test() { 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() { jpanel1 = new javax.swing.jpanel(); k = new javax.swing.jbutton(); o = new javax.swing.jbutton(); a1 = new javax.swing.jbutton(); = new javax.swing.jbutton(); n = new javax.swing.jbutton(); d = new javax.swing.jbutton(); r = new javax.swing.jbutton(); b = new javax.swing.jbutton(); u = new javax.swing.jbutton(); s = new javax.swing.jbutton(); l = new javax.swing.jbutton(); l1 = new javax.swing.jbutton(); txtfield = new javax.swing.jtextfield(); c = new javax.swing.jbutton(); k.settext("k"); k.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { kactionperformed(evt); } }); o.settext("o"); o.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { oactionperformed(evt); } }); a1.settext("a"); a1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { a1actionperformed(evt); } }); a.settext("a"); a.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { aactionperformed(evt); } }); n.settext("n"); n.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { nactionperformed(evt); } }); d.settext("d"); d.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { dactionperformed(evt); } }); r.settext("r"); r.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { ractionperformed(evt); } }); b.settext("b"); b.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { bactionperformed(evt); } }); u.settext("u"); u.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { uactionperformed(evt); } }); s.settext("s"); s.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { sactionperformed(evt); } }); l.settext("l"); l.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { lactionperformed(evt); } }); l1.settext("l"); l1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { l1actionperformed(evt); } }); txtfield.setfont(new java.awt.font("serif", 1, 12)); // noi18n txtfield.setfocusable(false); txtfield.setmaximumsize(new java.awt.dimension(202, 31)); txtfield.setminimumsize(new java.awt.dimension(202, 31)); txtfield.setopaque(false); txtfield.setpreferredsize(new java.awt.dimension(202, 31)); txtfield.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { txtfieldactionperformed(evt); } }); c.settext("c"); c.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { cactionperformed(evt); } }); javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); jpanel1.setlayout(jpanel1layout); jpanel1layout.sethorizontalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addcontainergap() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(b) .addcomponent(s)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(u) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(l) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(l1) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(o) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(c) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(k)) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(a) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(n) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(d) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(r) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(a1))) .addgap(0, 0, short.max_value)) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(txtfield, javax.swing.grouplayout.preferred_size, 276, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(addd))) .addcontainergap()) ); jpanel1layout.setverticalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addcontainergap() .addcomponent(txtfield, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgroup(jpanel1layout.createsequentialgroup() .addgap(34, 34, 34) .addcomponent(addd))) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(n) .addcomponent(d) .addcomponent(r) .addcomponent(s) .addcomponent(a) .addcomponent(a1)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(b) .addcomponent(u) .addcomponent(l) .addcomponent(l1) .addcomponent(o) .addcomponent(k) .addcomponent(c)) .addgap(19, 19, 19)) ); jlabel1.seticon(new javax.swing.imageicon(getclass().getresource("/test3/fotot/sanda.jpg"))); // noi18n jlabel1.setverticalalignment(javax.swing.swingconstants.top); javax.swing.grouplayout layout = new javax.swing.grouplayout(this); this.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jlabel1, javax.swing.grouplayout.default_size, 404, short.max_value) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 292, short.max_value) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) ); private void sactionperformed(java.awt.event.actionevent evt) { s.setvisible(false); } private void aactionperformed(java.awt.event.actionevent evt) { a.setvisible(false); } private void nactionperformed(java.awt.event.actionevent evt) { n.setvisible(false); } private void dactionperformed(java.awt.event.actionevent evt) { d.setvisible(false); } private void ractionperformed(java.awt.event.actionevent evt) { r.setvisible(false); } private void a1actionperformed(java.awt.event.actionevent evt) { a1.setvisible(false); } private void bactionperformed(java.awt.event.actionevent evt) { b.setvisible(false); } private void uactionperformed(java.awt.event.actionevent evt) { u.setvisible(false); } private void lactionperformed(java.awt.event.actionevent evt) { l.setvisible(false); } private void l1actionperformed(java.awt.event.actionevent evt) { l1.setvisible(false); } private void oactionperformed(java.awt.event.actionevent evt) { o.setvisible(false); } private void kactionperformed(java.awt.event.actionevent evt) { k.setvisible(false); } private void cactionperformed(java.awt.event.actionevent evt) { c.setvisible(false); } // variables declaration - not modify private javax.swing.jbutton a; private javax.swing.jbutton a1; private javax.swing.jbutton b; private javax.swing.jbutton c; private javax.swing.jbutton d; private javax.swing.jbutton k; private javax.swing.jbutton l; private javax.swing.jbutton l1; private javax.swing.jbutton n; private javax.swing.jbutton o; private javax.swing.jbutton r; private javax.swing.jbutton s; public javax.swing.jtextfield txtfield; private javax.swing.jbutton u; // end of variables declaration }
any or suggestion more welcome!! thank you.
do mean follows?:
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.hashmap; import java.util.map; import java.util.map.entry; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.event.documentevent; import javax.swing.event.documentlistener; public class testtextfield { private jtextfield textfield; private map<character, jbutton> buttons = new hashmap<>(); private void initui() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); textfield = new jtextfield(30); frame.add(textfield, borderlayout.north); jpanel panel = new jpanel(new gridlayout(0, 8, 15, 15)); (int = 0; < 26; i++) { final char c = (char) ('a' + i); final jbutton button = new jbutton(string.valueof(c)); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { textfield.settext(textfield.gettext() + c); } }); buttons.put(c, button); panel.add(button); } textfield.getdocument().adddocumentlistener(new documentlistener() { @override public void removeupdate(documentevent e) { updatebuttonvisibility(); } @override public void insertupdate(documentevent e) { updatebuttonvisibility(); } @override public void changedupdate(documentevent e) { updatebuttonvisibility(); } }); frame.add(panel); frame.pack(); frame.setvisible(true); } protected void updatebuttonvisibility() { string text = textfield.gettext(); (entry<character, jbutton> e : buttons.entryset()) { e.getvalue().setvisible(!text.touppercase().contains(string.valueof(e.getkey()))); } } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { try { testtextfield test = new testtextfield(); test.initui(); } catch (exception e) { e.printstacktrace(); } } }); } }
Comments
Post a Comment