Focus event in fxml

3

In the fxml file of a GUI we can direct the code to a method when a particular event occurs. For action events, it would be something like this:

<Button fx:id="btn1" onAction="#actionPause" />

But as to whether the object has been focused? In my case, I'm looking for this to use a TextField to remove your default content and change its style.

Please kindly attach to the resolutions for fxml files.

    
asked by anonymous 18.07.2017 / 06:54

1 answer

2

Unfortunately you can not do this directly in fxml. Events that can be configured directly in FXML are: setOnAction, Drag & Drop, Keyboard, Mouse, Rotate, Swipe and Zoom.

But in your code you can do the following:

public class FXMLDocumentController implements Initializable{

@FXML
// Link entre o controlador e a interface
private TextField idtextfield;

// ... Algum código

public void initialize(URL url, ResourceBundle rb){
    /* Adicionando um listener para capturar mudanças de foco
    *  Obs.: O primeiro componente de cima para baixo normalmente recebe o foco
    *  da aplicação, então tenha cautela 
    */
    idtextfield.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            // newValue terá o valor do foco atual, oldValue o valor anterior
            // Se estiver com o foco o valor será true
            System.out.println(newValue);
         }
    });

See the full list of available FXML events by looking for setOn: TextField (JavaFX 8)

    
18.07.2017 / 14:59