How to check which button (in fxml) has invoked a function in controller.java when both buttons have same onAction method

那年仲夏 提交于 2020-01-06 05:15:08

问题


If there are multiple buttons in an fxml file, with the sam onAction method , how to check which button has invoked the method in the controller.java file.

Fxml code is like this

<Button fx:id="b12", onAction="#push">
<Button fx:id="b13", onAction="#push">

Controller.java looks like this

@FXML
Button   b12,  b13;

I want to write a function such that if it is invoked by the first button , then its id has to be printed and otherwise the second id.


回答1:


As Slaw mentioned in their comment, you would want to use the EventObject.getSource() method to determine which object triggered the event.

Here is a simple application to demonstrate the details. There are 3 files in this example:

Main.java - Simply starts the application

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Just loads the sample GUI
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Layout.fxml"));
            loader.setController(new Controller());

            primaryStage.setScene(new Scene(loader.load()));

            primaryStage.setTitle("Event Source Sample");
            primaryStage.setWidth(300);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Controller.java - Provides the code to handle interactions with the UI

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class Controller {

    // Declare the controls used in the FXML file
    @FXML
    private Button button1;
    @FXML
    private Button button2;
    @FXML
    private Button button3;
    @FXML
    private Label lblSource;

    // This method is called when any of the buttons in the FXML file is clicked
    // The "ActionEvent" parameter includes all the details of the event that calls this method
    @FXML
    void handleClick(ActionEvent event) {

        // Assuming only a Button will call this method, determine which button it was by retrieving the Source of
        // the event.
        Button sourceButton = (Button) event.getSource();

        // We have the source, so let's update the label to show the text of the Button that was clicked.
        lblSource.setText(sourceButton.getText());
    }
}

Layout.fxml - The actual FXML design

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
    </padding>
    <HBox alignment="CENTER" spacing="10.0">
        <Button fx:id="button1" mnemonicParsing="false" onAction="#handleClick" text="Button 1"/>
        <Button fx:id="button2" mnemonicParsing="false" onAction="#handleClick" text="Button 2"/>
        <Button fx:id="button3" mnemonicParsing="false" onAction="#handleClick" text="Button 3"/>
    </HBox>
    <Label text="Source:"/>
    <Label fx:id="lblSource"/>
</VBox>

The main code to be concerned with is in the Controller.java class. You'll see the handleClick(ActionEvent event) method, which is called by each of the Button nodes in the FXML file.

When the method is called, it receives an ActionEvent object. We can then determine which Button created the event by calling the ActionEvent.getSource() method. From there, you can do any processing needed based on the source.



来源:https://stackoverflow.com/questions/51456418/how-to-check-which-button-in-fxml-has-invoked-a-function-in-controller-java-wh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!