Bas
Bas

Reputation: 3

javafx piechart can't add eventhandler

I've been trying to show percentage of the selected piechart slice. I have searched on Google and everyone is adding a new eventhandler with a mouse event. Whenever I try to implement the code I get the following error:

method addEventHandler in class Node cannot be applied to given types;
  required: EventType<T>,EventHandler<? super T>
  found: int,<anonymous EventHandler<MouseEvent>>
  reason: cannot infer type-variable(s) T
    (argument mismatch; int cannot be converted to EventType<T>)
  where T is a type-variable:
    T extends Event declared in method <T>addEventHandler(EventType<T>,EventHandler<? super T>)

What am I doing wrong? I've tried searching google with the error, but nothing came of it.

The controller in question:

package com.hva.fastenyourseatbelt;

import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import utilities.DatabaseUtils;
import static utilities.StatisticsUtils.giveAirports;

/**
 * FXML Controller class
 *
 * @author Mourad A
 */
public class FXMLStatistiekenController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private ComboBox<String> CBStatisticsCountry;

    @FXML
    private ComboBox<String> CBStatisticsAirport;

    @FXML
    private ComboBox<String> CBStatistiekenYear;

    @FXML
    private ComboBox<String> CBStatistiekenMonth;

    @FXML
    private PieChart pieChart;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        ObservableList list = DatabaseUtils.getType();

        pieChart.setData(list);
        pieChart.setTitle("Type bagage");

        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
                    new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent e) {
                    caption.setTranslateX(e.getSceneX());
                    caption.setTranslateY(e.getSceneY());
                    caption.setText(String.valueOf(data.getPieValue()) + "%");
                }
            });
        }
    }

    @FXML
    private String receiveCountries(ActionEvent event) {
        String country = CBStatisticsCountry.getValue();
        return country;
    }


}

Thanks

Edit: added entire controller

Upvotes: 0

Views: 1103

Answers (1)

Itai
Itai

Reputation: 6911

The error is due to erroneously importing java.awt.event.MouseEvent instead of javafx.scene.input.MouseEvent. The biggest hint is that the actual type was int instead of EventType<T>, implying a mismatch in imports - the older APIs tend to use static integer constants whereas newer APIs prefer enums.

Upvotes: 1

Related Questions