Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way for Getting id of the clicked Object in JavaFX controller

I`m looking for a better way for getting the id of the clicked object inside the event handler for this object.

I already found this:

javafx pass fx:id to controller or parameter in fxml onAction method

But that did not work for me.

Now I'm using the getId() function of the node class like this:

Button btn = (Button) event.getSource();
String id = btn.getId();

But i want to use this method not only for buttons.

like image 546
Daniel R. Avatar asked Jun 19 '14 08:06

Daniel R.


2 Answers

I use this for getting the id of ImageView objects that all share the same event code. Here is a simple example using MouseEvent:

  @FXML
  private void selectImage(MouseEvent event)
    {
    String source1 = event.getSource().toString(); //yields complete string
    String source2 = event.getPickResult().getIntersectedNode().getId(); //returns JUST the id of the object that was clicked
    System.out.println("Full String: " + source1);
    System.out.println("Just the id: " + source2);
    System.out.println(" " + source2);
    }

Here is the output in my situation, where I used SceneBuilder to assign the selectImage method to the 'On Mouse Pressed' event, then running the code and randomly clicking on three different ImageView objects:

Full String: ImageView[id=iv1, styleClass=image-view] Just the id: iv1
Full String: ImageView[id=iv4, styleClass=image-view] Just the id: iv4
Full String: ImageView[id=iv6, styleClass=image-view] Just the id: iv6

I hope this helps someone. :-)

like image 125
Michael Sims Avatar answered Sep 30 '22 18:09

Michael Sims


Since fx:id is used to bind controls between FXML and Controller, this answer is taking into consideration that OP wants the id of the controls when clicked.

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class IdForControlsOnClick extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        BorderPane borderPane = new BorderPane();
        VBox vBox = new VBox(20);
        borderPane.setCenter(vBox);

        Button button = new Button("Hi");
        button.setId("Button");
        Label label = new Label("Label");
        label.setId("Label");
        CheckBox checkBox = new CheckBox();
        checkBox.setId("CheckBox");

        button.addEventHandler(MouseEvent.MOUSE_CLICKED, new MyEventHandler());
        label.addEventHandler(MouseEvent.MOUSE_CLICKED, new MyEventHandler());
        checkBox.addEventHandler(MouseEvent.MOUSE_CLICKED, new MyEventHandler());

        vBox.getChildren().addAll(button, label, checkBox);
        Scene scene = new Scene(borderPane, 200, 200);
        stage.setScene(scene);
        stage.show();

    }

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

    private class MyEventHandler implements EventHandler<Event>{
        @Override
        public void handle(Event evt) {
           System.out.println(((Control)evt.getSource()).getId());
        }
    }
}
like image 33
ItachiUchiha Avatar answered Sep 30 '22 17:09

ItachiUchiha