java - Clearing contents of TextField using setText does not work in AWT -
i having problems clearing contents of textfield
in awt using settext()
method. apparently, settext("")
not clear contents of textfield
on pressing 'reset' button. here's program:
import java.awt.*; import java.awt.event.*; public class form extends frame { label lbl = new label("name:"); textfield tf = new textfield(); button btn = new button("reset"); public form() { tf.setcolumns(20); addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { system.exit(0); } }); btn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { tf.settext(""); //problem occurs here. not clear contents of text field on pressing 'reset' button. } }); add(lbl); add(tf); add(btn); setlayout(new flowlayout()); setsize(400,100); setvisible(true); settitle("form"); } public static void main(string[] args) { new form(); } }
can please tell me went wrong or suggest alternative? thanks.
i see problem using java 8u11. seem remember being filed known bug, can't seem find now.
a solution works me add intermediate step:
public void actionperformed(actionevent e) { tf.settext(" "); tf.settext(""); }
i'm not sure why necessary, think it's bug settext() function ignoring empty strings. if finds filed bug there more information there.
Comments
Post a Comment