在 JavaFX 控制器中获取被点击对象的 id 的更好方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24302636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:22:47  来源:igfitidea点击:

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

javajavafxjavafx-2

提问by Daniel R.

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

我正在寻找一种更好的方法来在此对象的事件处理程序中获取单击对象的 id。

I already found this:

我已经找到了这个:

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

javafx 将 fx:id 传递给 fxml onAction 方法中的控制器或参数

But that did not work for me.

但这对我不起作用。

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

现在我正在使用节点类的 getId() 函数,如下所示:

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

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

但我想不仅将这种方法用于按钮。

采纳答案by ItachiUchiha

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

由于 fx:id 用于在 FXML 和 Controller 之间绑定控件,因此此答案考虑到 OPid在单击时需要控件的 。

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());
        }
    }
}

回答by Michael Sims

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

我使用它来获取共享相同事件代码的 ImageView 对象的 id。这是一个使用 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:

这是我的情况下的输出,我使用 SceneBuilder 将 selectImage 方法分配给 'On Mouse Pressed' 事件,然后运行代码并随机单击三个不同的 ImageView 对象:

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

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. :-)

我希望这可以帮助别人。:-)