简体   繁体   English

确定按下按钮的 fx:id

[英]Determine the fx:id of a pressed button

I am trying to set up a method used by three different buttons.我正在尝试设置由三个不同按钮使用的方法。 I want to load one of three different views depending on which of them is pressed.我想根据按下哪个视图加载三个不同的视图之一。 To do so, I thought about passing as an argument the button itself.为此,我考虑将按钮本身作为参数传递。 But I was unable, due to javaFX not recognising the method with the new argument.但是我无法,因为 javaFX 无法识别带有新参数的方法。

How can I get the fx:id of the caller button so I can make the distinction between each of them?如何获取来电按钮的fx:id以便我可以区分它们中的每一个? Here's what I got this far.这就是我到目前为止所做的。 I just need to initialize pressed variable so it will do its thing.我只需要初始化pressed变量,这样它就会做它的事情。

if (pressed == edgeModify){
    loader = new FXMLLoader(getClass().getResource("/digitalia/view/edgeView.fxml"));
} else if (pressed == sectionModify){
    loader = new FXMLLoader(getClass().getResource("/digitalia/view/sectionView.fxml"));
} else {
    loader = new FXMLLoader(getClass().getResource("/digitalia/view/LayerView.fxml"));
}

You can get the id of the button as below:您可以获取按钮的 id,如下所示:

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

An easier approach would just be to use different handlers for the buttons.一种更简单的方法是为按钮使用不同的处理程序。

Use a different handler for each button.为每个按钮使用不同的处理程序。 It looks like this would be as simple as:看起来这很简单:

@FXML
private void loadEdgeView(ActionEvent event) {
    load("edgeView");
}

@FXML
private void loadSectionView(ActionEvent event) {
    load("sectionView");
}

@FXML
private void loadLayerView(ActionEvent event) {
    load("layerView");
}

private void load(String view) {
    FXMLLoader loader = new FXMLLoader("/digitalia/view/" + view + ".fxml");
    // ...
}

If you really want to do this with a single method, you could leverage the userData of the button;如果你真的想用一个方法来做到这一点,你可以利用按钮的userData ie: IE:

<Button text="Load Edge View" onAction="#loadView" userData="edgeView" />
<Button text="Load Section View" onAction="#loadView" userData="sectionView" />
<Button text="Load Layer View" onAction="#loadView" userData="layerView" />

and then in the controller:然后在控制器中:

@FXML
private void loadView(ActionEvent event) {
    Node node = (Node) event.getSource();
    String view = (String) node.getUserData();
    FXMLLoader loader = new FXMLLoader("/digitalia/view/" + view + ".fxml");
    // ...
}

I think using separate handlers is more maintainable, for a small price in verbosity.我认为使用单独的处理程序更易于维护,而且冗长的代价很小。

You can use the source of the event object like this:您可以像这样使用事件对象的源:

button.setOnAction(e -> {if (e.getSource() == button) System.out.println("button was pressed.");});

In your above example you only have to exchange pressed with e.getSource() .在你上面的例子中,你只需要与e.getSource()交换pressed

@FXML void handleButtonAction(ActionEvent event) {
        Expression e = new Expression("");

        String eventString = event.toString();
        System.out.println(eventString);
        String[] cutAtId = eventString.split("id=");
        cutAtId = cutAtId[1].split(", ");
        eventString = cutAtId[0];
        System.out.println("eventString ===>>>" + eventString + "<<<<====");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM