I want the cursor to hover on Button JavaFX , it is highlighted. How to do it? Did not find suitable Property .
2 answers
If you just want to change the style when you hover the cursor, then you can achieve it with css, just by setting the desired parameter in the pseudo-class :hover .
.your-class:hover { // some parameters } that is, you will need to write styles in a css file and connect it as described here . Then add a style class or identification to the Button .
style.css file
.my-button:hover { -fx-background-color: green; } Here is the java class.
package styled.button; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class StyledButton extends Application { @Override public void start(Stage primaryStage) throws Exception { AnchorPane anchorPane = new AnchorPane(); anchorPane.getStylesheets() .add(getClass().getResource("/style.css").toExternalForm()); Button button = new Button("Hover Button"); button.getStyleClass().add("my-button"); anchorPane.getChildren().add(button); primaryStage.setScene(new Scene(anchorPane, 400, 600)); primaryStage.show(); } } When you hover the cursor, the background of the button changes to green.
or it can be done programmatically:
package button.hover; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class ButtonHover extends Application { @Override public void start(Stage primaryStage) throws Exception { AnchorPane anchorPane = new AnchorPane(); Button button = new Button("Hover Button"); button.setOnMouseEntered(event -> { button.setStyle("-fx-background-color: green"); }); button.setOnMouseExited(event -> button.setStyle("")); anchorPane.getChildren().add(button); primaryStage.setScene(new Scene(anchorPane, 400, 600)); primaryStage.show(); } } - 1 option is good, how to set for a specific button? - Youlfey
|
Defines a function that will be called when the mouse is over an element.
|