by default, jfilechooser
in nimbuslookandfeel
doesn't show focus on jtextfield
user types file path. focus owner in jfilechooser
jcombobox
show in figure.
now how focus jtextfield
when user opens jfilechooser
. tried doing requestfocusinwindow()
on jtextfield
getting jfilechooser
through recursive logic. here full code have done.
import javax.swing.*; import java.awt.*; import java.awt.event.*; class getfocusforjtextfield extends jframe { jbutton jb; jfilechooser jf; public getfocusforjtextfield() { createandshowgui(); } private void createandshowgui() { // nimbuslookandfeel, jtextfield not // default focus owner in jfilechooser try { uimanager.setlookandfeel("javax.swing.plaf.nimbus.nimbuslookandfeel"); }catch(exception e){} settitle("get focus jtextfield"); setlayout(new flowlayout()); setsize(400,400); setvisible(true); setdefaultcloseoperation(exit_on_close); jb=new jbutton("open jfilechooser"); jb.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { showdialog(); } }); jf=new jfilechooser(); add(jb); } // loop find jtextfield, first // jtextfield in jfilechooser private void grabfocusfortextfield(component[] c) { for(component k:c) { if(k instanceof jtextfield) { jtextfield jt=(jtextfield)k; jt.requestfocusinwindow(); break; } else if(k instanceof jpanel) { jpanel jp=(jpanel)k; grabfocusfortextfield(jp.getcomponents()); } } } private void showdialog() { jf.showopendialog(this); grabfocusfortextfield(jf.getcomponents()); } public static void main(string args[]) { swingutilities.invokelater(new runnable(){ public void run() { new getfocusforjtextfield(); } }); } }
still unable focus. why not getting this.
the thing here upon call grabfocusfortextfield()
jtextfield
not displayable result cannot focus jtextfield
. component focus, component must exist first, visible , displayable, enabled , focusable. see focus subsystem in docs more.
you have register own hierarchylistener
on jfilechooser
listen hierarchyevent
. in nimbuslookandfeel
either might not done or jcombobox
chosen focus owner. whenever component displayable, whenever jfilechooser
's hierarchy changed, event fired , @ time, jtextfield
displayable.
i have rewritten code make work.
import javax.swing.*; import java.awt.*; import java.awt.event.*; class getfocusforjtextfield extends jframe { jbutton jb; jfilechooser jf; public getfocusforjtextfield() { createandshowgui(); } private void createandshowgui() { // nimbuslookandfeel, jtextfield not // default focus owner in jfilechooser try { uimanager.setlookandfeel("javax.swing.plaf.nimbus.nimbuslookandfeel"); }catch(exception e){} settitle("get focus jtextfield"); setlayout(new flowlayout()); setsize(400,400); setvisible(true); setdefaultcloseoperation(exit_on_close); jb=new jbutton("open jfilechooser"); jb.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { showdialog(); } }); jf=new jfilechooser(); // if add other jtextfield // accessory jfilechooser jf.setaccessory(new jtextfield(20)); jf.addhierarchylistener(new hierarchylistener(){ public void hierarchychanged(hierarchyevent he) { grabfocusfortextfield(jf.getcomponents()); } }); add(jb); } // loop find jtextfield, first // jtextfield in jfilechooser // if setaccessory contains jtextfield // or jtextfield itself, not focus private void grabfocusfortextfield(component[] c) { for(component k:c) { if(k instanceof jtextfield) { jtextfield jt=(jtextfield)k; jt.grabfocus(); break; } else if(k instanceof jpanel) { jpanel jp=(jpanel)k; grabfocusfortextfield(jp.getcomponents()); } } } private void showdialog() { jf.showopendialog(this); } public static void main(string args[]) { swingutilities.invokelater(new runnable(){ public void run() { new getfocusforjtextfield(); } }); } }
you can use requestfocusinwindow()
instead of grabfocus()
Comments
Post a Comment