Get Focus at first control in javafx


Question

my layout like :

enter image description here

i want focus at Company Name Texfield when i press TAB button at first time , but right now i get focus at Add Button how can i manage it ?

i try code like

    ChangeFocus(mTextFieldCompanyName, mTextAreaAboutUs);
    ChangeFocus(mTextAreaAboutUs, mTextAreaContactUs);
    ChangeFocus(mTextAreaContactUs, mButtonVideo);
    ChangeFocus(mButtonVideo, mButtonImage);
    ChangeFocus(mButtonImage, mButtonSave);
    public void ChangeFocus(Control mControlFrom,final Control mControlTo)
    {

   mControlFrom.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

         @Override
         public void handle(KeyEvent event)
         {
             if (event.getCode() == KeyCode.TAB)
             {
                System.out.println("TAB pressed");
                mControlTo.requestFocus();
                event.consume(); // do nothing
         }
       }
     });

     }
1
3
11/21/2016 11:39:00 AM

Try wrapping your mTextFieldCompanyName inside Platform.runLater()

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        mTextFieldCompanyName.requestFocus();  
    }   
});

Hope it helps :)

6
3/16/2015 5:14:30 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon