javaFX:自定义控件(由场景构建器创建)包含一个带有onAction的按钮

时间:2014-11-19 13:47:59

标签: javafx controls action

javaFX:自定义控件(由场景构建器制作)包含一个带有onAction(和其他组件)的按钮。当我在另一个屏幕中使用controll时,如何实现该方法?

2 个答案:

答案 0 :(得分:1)

我通常用自定义事件包装事件,例如“onMyCustomAction”。

您的控件需要自定义的eventHandler属性并触发事件:

public class MyCustomControl extends ... {


private final ObjectProperty<EventHandler<MyCustomActionEvent>> onMyCustomAction = new ObjectPropertyBase<EventHandler<MyCustomActionEvent>>() {
    @Override
    protected void invalidated() {
        setEventHandler(MyCustomActionEvent.MY_CUSTOM_ACTION, get());
    }

    @Override
    public Object getBean() {
        return MyCustomControl.this;
    }

    @Override
    public String getName() {
        return "onMyCustomAction";
    }
};

...

public MyCustomControl () {
    <load fx:root based FXML>
}

...

public final EventHandler<MyCustomActionEvent> getOnMyCustomAction() {
    return onMyCustomActionProperty().get();
}

public final void setOnMyCustomAction(EventHandler<MyCustomActionEvent> onMyCustomAction) {
    onMyCustomActionProperty().set(onMyCustomAction);
}

public ObjectProperty<EventHandler<MyCustomActionEvent>> onMyCustomActionProperty() {
    return onMyCustomAction;
}

...

@FXML
private void handleMyButtonOnAction(ActionEvent event) {
    fireEvent(new MyCustomActionEvent(<someData>);
}

自定义事件:

import javafx.event.Event;
import javafx.event.EventType;


public class MyCustomActionEvent extends Event {

    private static final long serialVersionUID = <some id>;
    public static final EventType<MyCustomActionEvent> MY_CUSTOM_ACTION = new EventType<>("MY_CUSTOM_ACTION");
    private final Foo foo;

    public MyCustomActionEvent(Foo foo) {
        super(MY_CUSTOM_ACTION);
        this.foo = foo;
    }


    public Foo getFoo() {
        return foo;
    }
}

然后客户端代码可以为“onMyCustomAction”注册一个EventHandler(这甚至可以通过FXML与标准控件一样工作):

myCustomControl.setOnMyCustomAction(event -> doSomething(event.getFoo()));

...

private void doSomething(Foo foo) {
...
}

或使用FXML:

<MyCustomControl onMyCustomAction="#doSomething" />

并提供一种方法:

@FXML
private void doSomething(MyCustomActionEvent event) {
...
}

2016年9月15日更新

我创建了一个实用程序类SimpleEventHandlerProperty来简化自定义事件处理程序属性:

private final ObjectProperty<EventHandler<SomeEvent>> onMyCustomAction = new SimpleEventHandlerProperty<>(this,
            "onMyCustomAction ", MyCustomActionEvent.MY_CUSTOM_ACTION, this::setEventHandler);

使用此实用程序类,不再需要onMyCustomAction的匿名类。

的Maven:

<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.7</version>
</dependency>

答案 1 :(得分:1)

您可以提供可以实施的回调。 例如:

public class myControl {
    private Button button;
    private Callback<Void, Void> buttonCallback;
    public myControl() {
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                if(getButtonCallback() !=  null){
                    getButtonCallback().call(null);
                }

            }
        });


    }
    public Callback<Void, Void> getButtonCallback() {
        return buttonCallback;
    }
    public void setButtonCallback(Callback<Void, Void> buttonCallback) {
        this.buttonCallback = buttonCallback;
    }

}

现在,您可以使用setter提供不同的回调实现。