事件在使用JavaFX和mvvmfx时未触发。

huangapple go评论68阅读模式
英文:

Event does not trigger using javafx and mvvmfx

问题

我正在尝试使用JavaFX和MVVMFX框架构建一个应用程序,用于计算和显示CVRP问题,但当我在一个Circle上添加事件监听器时,它从未被触发。

scope.subscribe("STOP_LOADED", (key, payload) -> {
    stepList.clear();
    stopList.clear();
    Stop depot = CVRPGraph.getDepot();
    stopList.add(new Circle(toUiUnit(depot.getX()), toUiUnit(depot.getY()), 3, Color.BLACK));
    stopList.addAll(CVRPGraph.getClientList().stream()
            .map(stop -> {
                Circle circle = new Circle(toUiUnit(stop.getX()), toUiUnit(stop.getY()), 3, Color.RED);
                circle.setOnMouseClicked(mouseEvent -> System.out.println(mouseEvent.getEventType().getName()));
                return circle;
            })
            .collect(Collectors.toList()));
});

stopList 是这样初始化的:

private final ObservableList<Circle> stopList = FXCollections.observableArrayList();

在视图模型中填充它,在视图中观察变化如下:

graphViewModel.stopList().addListener((ListChangeListener<? super Circle>) change -> {
    stopGroup.getChildren().clear();
    stopGroup.getChildren().addAll(change.getList());
});

其中 stopGroup 是 JavaFX 的 javafx.scene.Group。

圆圈被显示出来,但是当我点击它时什么都没有被打印出来。

程序截图
我做错了什么?

P.S. 您可以在此处找到整个代码 https://github.com/ptourneur/CapacitatedVehicleRoutingProblem,但如果您需要更多信息,请不要犹豫,感谢您。

英文:

I' trying to build an application using javafx and mvvmfx framework to compute and display CVRP Problem but when I add an event listener on a Circle, it is never triggered.

scope.subscribe(&quot;STOP_LOADED&quot;, (key, payload) -&gt; {
        stepList.clear();
        stopList.clear();
        Stop depot = CVRPGraph.getDepot();
        stopList.add(new Circle(toUiUnit(depot.getX()), toUiUnit(depot.getY()), 3, Color.BLACK));
        stopList.addAll(CVRPGraph.getClientList().stream()
                .map(stop -&gt; {
                    Circle circle = new Circle(toUiUnit(stop.getX()), toUiUnit(stop.getY()), 3, Color.RED);
                    circle.setOnMouseClicked(mouseEvent -&gt; System.out.println(mouseEvent.getEventType().getName()));
                    return circle;
                })
                .collect(Collectors.toList()));
    });

stopList is initialize like this

private final ObservableList&lt;Circle&gt; stopList = FXCollections.observableArrayList();

which I fullfill in the viewmodel and in the view I observe an change like this

graphViewModel.stopList().addListener((ListChangeListener&lt;? super Circle&gt;) change -&gt; {
            stopGroup.getChildren().clear();
            stopGroup.getChildren().addAll(change.getList());
        });

where stopGroup is javafx.scene.Group

@FXML
private Group stopGroup;

Circle are displayed but when I click on it nothing is print

Program screenshot
What I am doing wrong ?

P.S. You can find the entire code here https://github.com/ptourneur/CapacitatedVehicleRoutingProblem but do not hesitate if you need more information thanks

答案1

得分: 1

你示例应用程序中的问题是布局配置错误。你对 AnchorPane 的使用有误。你的 "ParamView.fxml" 覆盖了整个应用程序窗口,即使它只在右侧可见。因此,它还会消耗所有的鼠标事件。如果你移除 paramsview,点击处理程序将会按预期工作。为了调试,你可以为容器组件添加可视边框,以查看它们实际使用了多少空间。
如果你使用 AnchorPane,通常还应该使用 AnchorPane.bottomAnchor、topAnchor 等属性。

英文:

The problem in your example app is a misconfigured layout. Your usage of the AnchorPane is wrong. Your "ParamView.fxml" is covering the whole app window even though it's only visible on the right side. And therefor it is also consuming all mouse events. If you remove the paramsview the clickhandlers are working as expected. For debugging you could add visual borders to your container components to see how much space they are really using.
If you use AnchorPane your should normally also use AnchorPane.bottomAnchor, topAnchor and so on.

huangapple
  • 本文由 发表于 2020年4月6日 02:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61047439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定