java - error: String.format is not accepting event.getActionCommand() [as object] -
i writing basic gui
program in java using event handler. code working fine except little error @
string s = string.format("field 1 %s", event.getactioncommand())
error
the method format(string, object) of type string not applicable arguments format(string, string)`'.
so event.getactioncommand())
returning string. format method not accepting that.
i doing code in eclipse (kepler) using 1 of famous tutorial on internet. code worked not mine. quite beginner in java. please me in finding silly error.
here code.
package tushar_gui; import java.awt.flowlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jtextfield; import javax.swing.jpasswordfield; public class tush extends jframe{ private jtextfield textbox1; private jtextfield textbox2; private jtextfield textbox3 ; private jpasswordfield textbox_pass1; public tush(){ super("title"); setlayout(new flowlayout()); textbox1 = new jtextfield(10); add(textbox1); textbox2 = new jtextfield("enter text here", 30); add(textbox2); textbox3 = new jtextfield("this uneditable textbox", 30); textbox3.seteditable(false); add(textbox3); textbox_pass1 = new jpasswordfield("password"); add(textbox_pass1); thehandler handler = new thehandler(); textbox1.addactionlistener(handler); textbox2.addactionlistener(handler); textbox3.addactionlistener(handler); textbox_pass1.addactionlistener(handler); } private class thehandler implements actionlistener{ public void actionperformed(actionevent event){ string string = ""; if(event.getsource() == textbox1) string = string.format("field 1 %s", event.getactioncommand()); else if(event.getsource() == textbox2) string = string.format("field 2 %s", event.getactioncommand()); else if(event.getsource() == textbox3) string = string.format("field 3 %s", event.getactioncommand()); else if(event.getsource() == textbox_pass1) string = string.format("password field %s", event.getactioncommand()); joptionpane.showmessagedialog(null, string); } } }
and here main class
package tushar_gui; import javax.swing.jframe; public class gui1 { public static void main (string[] args){ tush tushobject = new tush(); tushobject.setdefaultcloseoperation(jframe.exit_on_close); tushobject.setsize(500, 500); tushobject.setvisible(true); } }
you haven't added action command textboxes
solution:
textbox1 = new jtextfield(10); textbox1.setactioncommand("textbox1 "); add(textbox1); textbox2 = new jtextfield("enter text here", 30); textbox2.setactioncommand("textbox2 "); add(textbox2); textbox3 = new jtextfield("this uneditable textbox", 30); textbox3.setactioncommand("textbox3 "); textbox3.seteditable(false);
Comments
Post a Comment