java - Cant Stop JTextFeild from getting default focus -


i have simple login screen. want username , password show in first , second text fields until clicked on. functionality working. first text field focused when app launches , therefore shows "" until loses focus. tried set default button , request focus no avail. looks button defaulting correctly, not receiving focus reason. know how fix this?

public class basics implements actionlistener{  private jframe frmbasics; private jtextfield usernamefeild; private jtextfield passwordfeild; private jbutton btnsignin; private jbutton btnsignup;  /**  * launch application.  */ public static void main(string[] args) {        eventqueue.invokelater(new runnable() {         public void run() {             try {                 basics window = new basics();                 window.frmbasics.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     });   }//end main  /**  * create application.  */ public basics() {     initialize(); }  /**  * initialize contents of frame.  */ private void initialize() {     frmbasics = new jframe();     frmbasics.settitle("welcome poopalace!!!");     frmbasics.setbounds(100, 100, 511, 344);     frmbasics.setdefaultcloseoperation(jframe.exit_on_close);     frmbasics.getcontentpane().setlayout(null);      usernamefeild = new jtextfield("username");     usernamefeild.setbounds(148, 79, 214, 20);     usernamefeild.addfocuslistener(new focusedclass());     frmbasics.getcontentpane().add(usernamefeild);     usernamefeild.setcolumns(10);      passwordfeild = new jtextfield("password");     passwordfeild.setbounds(148, 126, 214, 20);     passwordfeild.addfocuslistener(new focusedclass());     frmbasics.getcontentpane().add(passwordfeild);     passwordfeild.setcolumns(10);      btnsignin = new jbutton("sign in");     btnsignin.setbounds(148, 182, 89, 23);     btnsignin.addactionlistener(this);     frmbasics.getcontentpane().add(btnsignin);      btnsignup = new jbutton("sign up");     btnsignup.setbounds(273, 182, 89, 23);     btnsignup.addactionlistener(this);     frmbasics.getcontentpane().add(btnsignup);      //from i've been reading these 2 lines should solution     //but request focus seems not working      frmbasics.getrootpane().setdefaultbutton(btnsignin);;     btnsignin.requestfocus();   }  @override public void actionperformed(actionevent e) {      //frmbasics.getcontentpane().removeall();     //frmbasics.repaint();     system.out.println(usernamefeild.gettext());     system.out.println(passwordfeild.gettext());  }//actionperformed   private class focusedclass implements focuslistener {      @override     public void focusgained(focusevent arg0) {          if(arg0.getsource().equals(usernamefeild) && usernamefeild.gettext().compareto("username") == 0){             usernamefeild.settext("");             }         if(arg0.getsource().equals(passwordfeild) && passwordfeild.gettext().compareto("password") == 0){             passwordfeild.settext("");         }      }      @override     public void focuslost(focusevent arg0) {          if(usernamefeild.gettext().compareto("") == 0){             usernamefeild.settext("username");         }          if(passwordfeild.gettext().compareto("") == 0){             passwordfeild.settext("password");         }          frmbasics.getcontentpane().repaint();     }  }    }//class 

requesting focus works after layout of window complete. need call requestfocusinwindow() in 1 of 3 specific situations:

  1. in windowopened() method.
  2. in eventqueue's invokelater(), run after pending events processed.
  3. in overriden jframe's setvisible() method.

the first option:

//btnsignin.requestfocusinwindow();  frmbasics.addwindowlistener(new windowadapter() {     @override     public void windowopened(windowevent e) {         btnsignin.requestfocusinwindow();     } }); 

also note requestfocusinwindow() more portable requestfocus().


Comments