[BAEL-3946] Javafx adding ActionListener to button (#11701)

* Add implementation for article.

* Add new line at the end of FXML layout file.

* Remove default prefWidth from button, convert eventHandler to lambda.

* Fix FXML file formatting.
This commit is contained in:
AttilaUhrin 2022-01-25 06:49:12 +01:00 committed by GitHub
parent 319d5cceef
commit c81fe2f74e
3 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package com.baeldung.button.eventhandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
public class ButtonEventHandlerController {
private static final Logger logger = LoggerFactory.getLogger(ButtonEventHandlerController.class);
@FXML
private Button button;
@FXML
private Label label;
@FXML
private void initialize() {
button.setText("Click me");
handleClickEvent();
handleHoverEffect();
reuseRightClickEventHandler();
}
private void handleClickEvent() {
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
logger.info("OnAction {}", event);
}
});
button.setOnAction(event -> logger.info("OnAction {}", event));
button.setOnAction(event -> logger.info("OnAction2 {}", event));
}
private void handleHoverEffect() {
Effect shadow = new DropShadow();
button.setOnMouseEntered(e -> button.setEffect(shadow));
button.setOnMouseExited(e -> button.setEffect(null));
}
private void reuseRightClickEventHandler() {
EventHandler<MouseEvent> rightClickHandler = event -> {
if (MouseButton.SECONDARY.equals(event.getButton())) {
button.setFont(new Font(button.getFont()
.getSize() + 1));
}
};
button.setOnMousePressed(rightClickHandler);
label.setOnMousePressed(rightClickHandler);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.button.eventhandler;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/button_event-handler.fxml"));
Pane page = loader.load();
primaryStage.setTitle("Button event handler");
primaryStage.setScene(new Scene(page));
primaryStage.show();
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml"
xmlns="http://javafx.com/javafx"
fx:controller="com.baeldung.button.eventhandler.ButtonEventHandlerController"
prefHeight="200.0" prefWidth="300.0">
<center>
<Button fx:id="button" HBox.hgrow="ALWAYS"/>
</center>
<bottom>
<Label fx:id="label" text="Test label"/>
</bottom>
</BorderPane>