━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 27. Event handler for a Pie Chart


Pie Chart 의 node(각 영역)를 클릭하였을 때 label 에 퍼센테이지를 표시하는 이벤트 핸들러를 작성해 보았습니다.


1. MainController.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package application;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
 
public class MainController {
 
    @FXML PieChart pieChart;
    
    @FXML Label status;
    
    public void btn(ActionEvent event){
        ObservableList<Data> list = FXCollections.observableArrayList(
            new PieChart.Data("Java"50),    
            new PieChart.Data("c++"20),
            new PieChart.Data("python"30),
            new PieChart.Data("c#"10),
            new PieChart.Data("c"15)
            );
        pieChart.setData(list);
        
        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 
                @Override
                public void handle(MouseEvent event) {
                    status.setText(String.valueOf(data.getPieValue()/(125.00/100)) + "%");
                    
                }
            });
        }
    }
    
}
 
cs


'Java > JavaFx' 카테고리의 다른 글

JavaFX 29. Multiple Line Chart  (0) 2016.08.29
JavaFX 28. Line Chart  (0) 2016.08.29
JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
COMMENT