简体   繁体   中英

How to know which button is bieng clicked in javafx

How to know which button has envoked the function. I have read other answers on stackoverflow like this one . I tried creating a new button and giving it a value of event.getSource() but it is not working

    @FXML
    Button v1;
    @FXML
    Button v2;
    @FXML
    Button v3;
    @FXML
    Button v4;
    @FXML
    Button v5;
    @FXML
    Button v6;


public void printButton(ActionEvent event){

            Button sourceButton = (Button) event.getSource();

            if(sourceButton == v1){
                System.out.print("v1");
            }

            else if(sourceButton == v2){
                System.out.print("v2");
            }

            else if(sourceButton == v3){
                System.out.print("v3");
            }

            else if(sourceButton == v4){
                System.out.print("v4");
            }

            else if(sourceButton == v5){
                System.out.print("v5");
            }

            else if(sourceButton == v6){
                System.out.print("v6");
            }
        }

I have created the button in fxml and it calls the same function printButton();

This answer is using java 8 update 211 for testing.

The comments are suggesting that changing == to .equals() was the solution to this. However, Button does not override .equals() , so both of those ways are doing effectively the same thing.

Running up the sample application below to test resulted in all of the 3 buttons working as expected. Therefore, there may have been something incorrect in the FXML file with OP's code, which (as I write this) has not been shown from OP.

In the example below, note that the fxml file:

  • Specifies the controller with fx:controller="sample.Controller"
  • Contains 3 buttons with their ids matching exactly to the ones declared in Controller
  • On each button, includes onAction="#printButton" , and the name in quotes matches the method name in Controller onAction="#printButton" .

Please note all of these are within the same package.

Main.java:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Controller.java

package sample;

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

public class Controller {

    @FXML
    Button v1;
    @FXML
    Button v2;
    @FXML
    Button v3;

    public void printButton(ActionEvent event){

        Button sourceButton = (Button) event.getSource();

        if(sourceButton.equals(v1)){
            System.out.print("v1");
        }

        else if(sourceButton == v2){
            System.out.print("v2");
        }

        else if(sourceButton == v3){
            System.out.print("v3");
        }

    }
}

sample.fxml

<?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" fx:controller="sample.Controller">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
    </padding>
    <HBox alignment="CENTER" spacing="10.0">
        <Button fx:id="v1" mnemonicParsing="false" onAction="#printButton" text="Button 1"/>
        <Button fx:id="v2" mnemonicParsing="false" onAction="#printButton" text="Button 2"/>
        <Button fx:id="v3" mnemonicParsing="false" onAction="#printButton" text="Button 3"/>
    </HBox>
    <Label text="Source:"/>
    <Label fx:id="lblSource"/>
</VBox>

make your life easy how about using isPressed() function ?

                 if( v1.isPressed() )  {
                 name2 = n1.getText(); 

               System.out.println("   V1  got called ");
                 }  

v1.isPressed(); means check whether v1 has been clicked or not it return true or false

i'm not sure about also v1.isfire(); I think this one can make auto click

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM