i want to change color my label "Kategori" when mouse pressed and exit on "paneKategori" (Look at the picture). my code in fxml controller
@FXML
void btnProdukMouseEntered(javafx.scene.input.MouseEvent event) {
if ( event.getSource() == paneKategori) {
labelKategori.setStyle("-fx-background-color: #FF0000;");
}
}
that is no working for me.
Create handlers for the MOUSE_PRESSED
, MOUSE_DRAGGED
and MOUSE_RELEASED
handlers. Check, if the event's location is inside the node in MOUSE_DRAGGED
.
Example
@Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button("Drag me");
final String style = "-fx-background-color: red;";
button.setOnMousePressed(evt -> {
if (evt.isPrimaryButtonDown()) {
button.setStyle(style);
}
});
button.setOnMouseDragged(evt -> {
if (evt.isPrimaryButtonDown()) {
button.setStyle(button.contains(evt.getX(), evt.getY()) ? style : null);
}
});
button.setOnMouseReleased(evt -> {
button.setStyle(null);
});
Scene scene = new Scene(new StackPane(button), 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
BTW: Checking the source in an event handler is bad practice in most cases, since you could easily avoid the check by only registering the event handler for the node you're comparing the source
property of the event to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.