java - Focus not being set by method to grab focus -
learning code, building gui figure out how interacts. trying write method set focus on (component) @ points need in larger program - ie call focusgrabber(jtextfield1)
, have set input focus on jtextfield1
. in attempt best sscce, i've made simple main makes enough gui give 2 textfields should set focus on second.
package odin; import java.awt.component; import java.util.list; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield; /* */ class focusgrabber implements runnable { private jcomponent component; public focusgrabber(jcomponent component) { this.component = component; } @override public void run() { component.grabfocus(); } public static void main(string[] args){ //draw , show gui jframe gui = new jframe(); gui.settitle("new provider interface"); gui.setdefaultcloseoperation(jframe.exit_on_close); final jtextfield textid = new jtextfield("providerid ", 20); final jtextfield textname = new jtextfield("provider name ", 20); gui.add(textid); gui.add(textname); gui.pack(); gui.setvisible(true); focusgrabber(textname); } }
use requestfocusinwindow
on grabfocus
...
it helpful if called run
method @ point
also, shouldn't more like
focusgrabber fg = new focusgrabber(textname); fg.run();
Comments
Post a Comment