Hannah Duncan
Hannah Duncan

Reputation: 249

How to implement EventHandler<ActionEvent> in separate class for more that one button?

I'm trying to have a separate class file for my Event Handlers because right now I have the same 6 lines of code with slight number changes. It's a lot of repetitive code and I am not sure how to handle it.

public class GUIDemo extends Application
{
    private Button btn1 = new Button("Button 1");
    private Button btn2 = new Button("Button 2");
    private Button btn3 = new Button("Button 3");
    private Button btn4 = new Button("Button 4");
    private Button btn5 = new Button("Button 5");
    private Button btn6 = new Button("Button 6");
    private Label lbl1 = new Label();
    private Label lbl2 = new Label();
    private Label lbl3 = new Label();
    private Label lbl4 = new Label();
    private Label lbl5 = new Label();
    private Label lbl6 = new Label();

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        GridPane myPane = new GridPane();
        myPane.setHgap(10);
        myPane.setVgap(10);
        Scene myScene = new Scene(myPane, 500, 300);
        primaryStage.setScene(myScene);
        primaryStage.show();

        primaryStage.setTitle("GUI Demo");

        myPane.setAlignment(Pos.CENTER);

        myPane.add(btn1, 0, 1);
        myPane.add(btn2, 1, 1);
        myPane.add(btn3, 2, 1);
        myPane.add(btn4, 0, 3);
        myPane.add(btn5, 1, 3);
        myPane.add(btn6, 2, 3);

        myPane.add(lbl1, 0, 2);
        myPane.add(lbl2, 1, 2);
        myPane.add(lbl3, 2, 2);
        myPane.add(lbl4, 0, 4);
        myPane.add(lbl5, 1, 4);
        myPane.add(lbl6, 2, 4);

        btn1.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 1 clicked");
                lbl1.setText("Button 1 clicked");
            }
        });
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 2 clicked");
                lbl2.setText("Button 2 clicked");
            }
        });
        btn3.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 3 clicked");
                lbl3.setText("Button 3 clicked");
            }
        });
        btn4.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 4 clicked");
                lbl4.setText("Button 4 clicked");
            }
        });
        btn5.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 5 clicked");
                lbl5.setText("Button 5 clicked");
            }
        });
        btn6.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 6 clicked");
                lbl6.setText("Button 6 clicked");
            }
        });
    }

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

I have tried the following (not implemented in code above) but it doesn't work. It doesn't print "Button clicked" to the console. I also need to be able to print "Button # clicked" because the result needs to be different for all six buttons.

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ButtonHandler implements EventHandler<ActionEvent>
{
    private final Stage window;
    private final Scene scene;

    ButtonHandler(Stage window, Scene scene)
    {
        this.window = window;
        this.scene = scene;
    }

    @Override
    public void handle(ActionEvent e) {
        System.out.println("Button clicked");
        window.setScene(scene);
    }   
}

Any help would be appreciated.

Upvotes: 1

Views: 5297

Answers (2)

fabian
fabian

Reputation: 82451

Analyze first, which data the event handler needs access to.

In this case this is:

  • the button number
  • the label

This means you could simply create a class containing a int field:

public class ButtonHandler implements EventHandler<ActionEvent> {
    private final String text;
    private final Label label;

    public ButtonHandler(int number, Label label) {
        this.text = "Button " + number + " clicked";
        this.label = label;
    }

    @Override
    public void handle(ActionEvent e) {
        System.out.println(text);
        label.setText(text);
    }
}
btn1.setOnAction(new ButtonHandler(1, lbl1));

Upvotes: 2

a lovely person
a lovely person

Reputation: 13

You can make a method that returns a handler object and takes String (the text or the just button N.O)

private  EventHandler <ActionEvent> getHandler (String str)
{
    return new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event) {
            Button b = ((Button)event.getSource());
            b.setText(b.getText()+"have been clicked");
            System.out.print(str);
        }

    };
}

Upvotes: 0

Related Questions