java - How to make cursor appear in JTextField by default? -
i want create cursor appear in jtextfield "box" default. currently, have place cursor in box before appear. here code.
class xyz extends jframe implements actionlistener { jtextfield box = new jtextfield(); jbutton 1 time again = new jbutton("restart"); jbutton ext = new jbutton("exit"); xyz() { box.addactionlistener(this); again.addactionlistener(this); ext.addactionlistener(this); box.setpreferredsize(new dimension(20,20)); box.sethorizontalalignment(jtextfield.center) jpanel s3 = new jpanel(new borderlayout()); //this holds 3 panels below jpanel restart = new jpanel(new borderlayout()); //this holds button jpanel leave = new jpanel(new borderlayout()); //also holds button //the next holds jlabel , textfield jpanel middle = new jpanel(new flowlayout(flowlayout.center,20,0)); jlabel j2 = new jlabel("enter guess!"); middle.add(j2); middle.add(box); restart.add(again,borderlayout.south); leave.add(ext,borderlayout.south); //adding 3 panels s3 s3.add(middle,borderlayout.center); s3.add(restart,borderlayout.west); s3.add(leave,borderlayout.east); } }
you can request textfield focused calling requestfocusinwindow
. invoke 1 time frame containing textfield visible:
box.requestfocusinwindow();
java swing cursor focus jtextfield
No comments:
Post a Comment