Focus event in fxml

Asked

Viewed 370 times

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 for the case of the object being focused?
In my case, I’m looking for that to use in a TextField to remove its default content and change its style.

Please stick to resolutions for fxml files.

1 answer

2


Unfortunately you can’t do this directly in fxml. The events that can be configured directly in FXML are: setOnAction, Drag & Drop (Drag & Drop), Keyboard, Mouse, Rotation, 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 events available in FXML looking for: Textfield (Javafx 8)

  • Thanks for the reply. Unfortunately it was what I feared. Sorry for the inconvenience, as you may have noticed I’m new to this.

  • One more thing: the link is broken

  • Sorry, when inserting the label was at the end of the link. Fixed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.