2017-05-14 2 views
0

私はこのチュートリアル(「円グラフのための処理マウスイベント」と呼ばれる下部のセクション)は、次のよ:イベントを各円のスライスに追加した後で、JavaFX PieChartラベルが表示されないのはなぜですか? (Javaの8)

https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pie-chart.htm

をし、次のように私のコードは次のとおりです。

@FXML 
public void initialize() { 
    pieChart.setTitle("Breakdown of Customers by City"); 
    pieChart.setLegendSide(Side.LEFT); 

    final Task<ObservableList<PieChart.Data>> task = new NumClientsAtLocationTask(new CustomerAccountDaoSelect()); 
    new Thread(task).start(); 
    task.setOnSucceeded(ae -> { 

     pieChart.setData(task.getValue()); 

     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, 
        ae2 -> { 
         caption.setTranslateX(ae2.getSceneX()); 
         caption.setTranslateY(ae2.getSceneY()); 
         caption.setText(String.valueOf(data.getPieValue())); 
        }); 
     } 
    }); 

} 


class NumClientsAtLocationTask extends Task<ObservableList<PieChart.Data>> { 
    private DaoSelect<CustomerAccount> customerAccountDaoSelect; 

    NumClientsAtLocationTask(DaoSelect<CustomerAccount> customerAccountDaoSelect) { 
     this.customerAccountDaoSelect = customerAccountDaoSelect; 
    } 

    @Override 
    protected ObservableList<PieChart.Data> call() throws Exception { 
     List<CustomerAccount> customerAccounts = customerAccountDaoSelect.select(RunListeners.FALSE); 

     Map<String, List<CustomerAccount>> customerMap = 
       customerAccounts 
         .stream() 
         .collect(Collectors.groupingBy(CustomerAccount::getCity)); 

     int london = customerMap.get("London").size(); 
     int phoenix = customerMap.get("Phoenix").size(); 
     int newYork = customerMap.get("New York").size(); 

     ObservableList<PieChart.Data> results = FXCollections.observableArrayList(
       new PieChart.Data("London", london), 
       new PieChart.Data("Phoenix", phoenix), 
       new PieChart.Data("New York", newYork)); 

     updateValue(results); 
     return results; 
    } 
} 

グラフはデフォルトの形式で問題なく表示されますが、スライス上でマウスをクリックするとラベルは表示されません。コンソールにラベルを印刷すると、正しい値が表示され、チュートリアルのように画面に表示されません。何か案は?

答えて

1

このサンプル/チュートリアルはかなり貧弱であり、重要な詳細は省略しています。最初にSceneLabel 'caption'を追加する必要があります。

あなたが純粋にそのチュートリアルのサンプルコードを使用していた場合、あなたは((Group) scene.getRoot()).getChildren().add(caption);を含めるたい:

final Label caption = new Label(""); 
((Group) scene.getRoot()).getChildren().add(caption); // Add me 
caption.setTextFill(Color.DARKORANGE); 
caption.setStyle("-fx-font: 24 arial;"); 

for (final PieChart.Data data : chart.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()) + "%"); 
       } 
      }); 
} 
+0

おやおや、私はラベルがシーンノードグラフの一部であることが必要であることを見落とし。私はFXML注入で追加しました。 – DayTripperID

関連する問題