简体   繁体   中英

How to refer to actual clicked button in event handler in java fx?

事实是,我有二维按钮阵列,我想向每个按钮动态添加setOnClick方法,这会改变它们的文本,这是一个问题,因为我不能在其中使用变量,例如myArray [i ] [j],因此实际上我无法引用我的按钮。

I used a bit from both of your ideas and I pretty much thank you both for that. I created buttons dynamically and also I used new class extending button class to add horizontal and vertical value to my buttons. It looks like this now:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

    primaryStage.setTitle("Hello World");
    GridPane root = new GridPane();
    primaryStage.setScene(new Scene(root, 1000, 600));
    MyButton[][] buttonArray = new MyButton[10][10];
    HBox[] hboxArray = new HBox[10];


    for(int i=0;i<10;i++)
    {
        hboxArray[i]=new HBox();
        for(int j=0;j<10;j++)
        {
            MyButton button =new MyButton(i,j);
            button.setMinSize(100,50);
            buttonArray[i][j]=button;
            button.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                    button.setText("button"+button.getH()+","+button.getV());
                    //where getH is get horizontal and getV is get vertical
                    button.setDisable(true);
                }
            });
            hboxArray[i].getChildren().add(buttonArray[i][j]);
        }
    }

    for(int i=0;i<10;i++)
    {
        root.add(hboxArray[i],1,i);
    }

    primaryStage.show();
}


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

}

If I read your question right, this little app shows an example of how to accomplish what you are trying to do. The app stores Button s into a 2D Array . It also creates an Action Listener for each button that changes the text when a Button is pressed.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button [][] buttonContainer = new Button[3][3];

        VBox vbox = new VBox();

        for(int i = 0; i < 3; i++)
        {
            for(int t = 0; t < 3; t++)
            {
                Button button = new Button();
                button.setText("button" +  i + " : " + t);
                //Create event handler
                button.setOnAction((event)->{
                    button.setText("Hello World!");
                });
                buttonContainer[i][t] = button;//Add the current button to the Button 2D array
                vbox.getChildren().add(buttonContainer[i][t]);//Add current button to the VBox
            }
        }

        StackPane root = new StackPane();
        root.getChildren().add(vbox);

        Scene scene = new Scene(root, 500, 500);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

One way: Add a different onAction handler for each button, rather than setting the same one on all buttons.

Another way: Override Button and add instance variables to save the row/column of the button: buttons[i][j] = new MyButton(i, j, ...); And also add getters for row and column.

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