How to know if the cursor is on a certain component in javafx

Asked

Viewed 251 times

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.

  • 1

    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)?

  • Ready-made, edited.

1 answer

1


I believe this is the result you want, from what I understand:

@FXML
private Button btn;

// ...

@FXML
public void mostrarPopup(){
    Popup popup = new Popup();
    popup.setAutoHide(true);

    HBox hb = new HBox();
    Button btn2 = new Button("Botão 2");
    Button btn3 = new Button("Botão 3");
    hb.getChildren().addAll(btn2, btn3);

    popup.getContent().addAll(hb);
    Bounds bounds = btn.localToScreen(btn.getBoundsInLocal());
    // X + Width = Lado direito
    popup.show(btn, bounds.getMinX() + bounds.getWidth(), bounds.getMinY());

    // Fecha o popup quando o mouse sai do HBox
    hb.setOnMouseExited((MouseEvent t) -> {
        popup.hide();
    });
}

Just add the show method Pup() to onMouseEntered from your button:

<Button fx:id="btn" layoutX="129.0" layoutY="26.0" mnemonicParsing="false" onMouseEntered="#mostrarPopup" text="Button" />

The result is as follows:

inserir a descrição da imagem aqui

  • This method would be for a menu, right? Here’s a problem. I need a menu with the options horizontally, but I couldn’t find a method to change this in a menu, so I tried to implement it myself. But then gave some problems and to do the business well done I need to know if the mouse is on a component at a given time.

  • Tried to at least put an Hbox in the context menu?

  • I tried now, and he stopped expanding

  • Ta, but you really don’t know if the cursor is on a component? Type a component method that when called returns a Boolean saying whether or not the cursor is over?

  • I edited with another workaround, see if it gets better.

Browser other questions tagged

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