PieChart切片的MouseEvent在ControlsFX PopOver(JavaFX)上不起作用

时间:2018-12-23 10:07:17

标签: javafx controlsfx

我正在从ControlsFX库实现PopOver。我已经根据包括PieChart在内的设计在PopOver上成功实现了自定义布局。我在饼图中填充了数据,没有问题,并且饼图切片看起来很好,但是在将鼠标事件设置为其切片时,例如输入鼠标,单击鼠标等,事件不会被触发。

为了给大家提供代码参考,我制作了一个简单的FXML应用程序。

Sample.fxml

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.BorderPane?>

    <BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.121" fx:controller="sample.Controller">
    <center>
      <Button fx:id="btn" mnemonicParsing="false" text="Button" BorderPane.alignment="CENTER" />
    </center>
</BorderPane>

Controller.java

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import org.controlsfx.control.PopOver;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    public Button btn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

            PieChart pieChart = new PieChart();
            pieChart.setData(getChartData());
            pieChart.setPrefSize(200, 200);
            pieChart.setMinSize(200, 200);
            pieChart.setMaxSize(200, 200);

            for (final PieChart.Data data : pieChart.getData()) {
                Node node = data.getNode();
                System.out.println("Check1"); // its working.
                node.setOnMouseClicked(event -> {
                    System.out.println("Mouse Clicked!"); // not triggered.
                });
                node.setOnMouseEntered(event -> {
                    System.out.println("Mouse Entered!"); // not triggered.
                });
                node.setOnMouseExited(event -> {
                    System.out.println("Mouse Exited!"); // not triggered.
                });
            }

            PopOver popOver = new PopOver(pieChart);
            popOver.setPrefSize(500, 500);

        btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                popOver.show(btn);
            }
        });
    }

    private ObservableList<PieChart.Data> getChartData() {
        ObservableList<PieChart.Data> data = FXCollections.observableArrayList();
        data.add(new PieChart.Data("A", 36));
        data.add(new PieChart.Data("B", 36));
        data.add(new PieChart.Data("C", 36));
        data.add(new PieChart.Data("D", 36));
        data.add(new PieChart.Data("E", 36));
        data.add(new PieChart.Data("F", 36));
        return data;
    }

}

和往常一样是主班。

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 500, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在此代码中,单击该按钮将显示弹出窗口。我希望你们对此有一些解决方案。

谢谢。

0 个答案:

没有答案