首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在JavaFX控制器中获取被单击对象的id的更好方法

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

Stack Overflow用户
提问于 2014-06-19 16:57:33
回答 2查看 39.3K关注 0票数 9

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

我已经找到了这个:

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

但这对我不起作用。

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

代码语言:javascript
运行
复制
Button btn = (Button) event.getSource();
String id = btn.getId();

但是我想使用这个方法,而不仅仅是按钮。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-19 18:57:02

由于fx:id被用来在FXML和控制器之间绑定控件,所以这个答案是考虑到当被点击时OP想要控件的id

代码语言:javascript
运行
复制
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());
        }
    }
}
票数 14
EN

Stack Overflow用户

发布于 2017-02-24 11:26:26

我使用它来获取所有共享相同事件代码的ImageView对象的id。下面是一个使用MouseEvent的简单示例:

代码语言:javascript
运行
复制
  @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);
    }

下面是我的输出,我使用SceneBuilder将selectImage方法分配给‘按下鼠标时’事件,然后运行代码并随机单击三个不同的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

我希望这对某些人有帮助。:-)

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24302636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档