1
I have a component in javafx and would like to know if the cursor is positioned on it. There is a function for this?
I tried to create a boolean
that stores it using the events setOnMouseEntered
and setOnMouseExited
, but I use this for two components and the threads of the java events ended up creating bugs (one event ended before the other).
Then I wonder if you have any method to know if the cursor is on a component when the method is called.
Code:
public class BarraLogoffControlador implements Initializable {
@FXML
private HBox menu;
@FXML
private Button botao_expandir;
private final HBox painel = new HBox();
private boolean esta_no_botao = false;
private boolean esta_no_painel = false;
@Override
public void initialize(URL url, ResourceBundle rb) {
Polygon seta = new Polygon(
54.0, 52.0,
54.0, 58.0,
50.0, 55.0
);
this.menu.getStylesheets().add("/iftorrent/gui/barraLogoff/BarraLogoffCSS.css");
this.menu.setAlignment(Pos.CENTER_RIGHT);
this.menu.setMinSize(0, 0);
this.menu.setPrefHeight(ALTURA_BARRA_LOGOFF1);
this.botao_expandir.setOnMouseEntered((Event event) -> {
if(!menu.getChildren().contains(painel)){
menu.getChildren().add(painel);
}
esta_no_botao = true;
});
this.painel.setOnMouseEntered((Event event) -> {
esta_no_painel = true;
});
this.botao_expandir.setOnMouseExited((Event event) -> {
esta_no_botao = false;
});
this.painel.setOnMouseExited((Event event) -> {
esta_no_painel = false;
});
this.botao_expandir.setGraphic(seta);
this.botao_expandir.setMinWidth(5);
this.botao_expandir.setPrefWidth(8);
this.botao_expandir.setMaxHeight(ALTURA_BARRA_LOGOFF1);
this.painel.getChildren().addAll(
new Button("desligar"),
new Button("fechar"),
new Button("bloquear")
);
this.painel.setId("painel");
this.painel.setMaxHeight(ALTURA_BARRA_LOGOFF1);
this.painel.setAlignment(Pos.CENTER);
}
}
Basically, the idea is a component with a button in which, when positioning the cursor, expands a series of buttons.
The code in FXML
just creates a Hbox called menu and adds the Button
button expand inside.
The problem is: the component consists of two parts, a button and an hbox. As we move the mouse from one component to another, it leaves one component and enters another, thus calling two events. In the input event, it updates a Boolean variable that stores if the cursor is on the component, already in the output event, it checks if the cursor is on any of the components and, if not, hides the hbox. The problem is that the output event is occurring before the input event, and so the cursor check is done before Boolean is updated.
It wasn’t clear to me your purpose, what exactly do you want to apply this to? What is the method called? What did you try (code)?
– Gustavo Fragoso
Ready-made, edited.
– Eduardo Toffolo