我目前正在使用饼图和java fx场景Builder 1.1开发Java fx。
我的问题如下:
如何从饼图中获取鼠标点击事件?
这是我的代码:
screen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
- &GT;
ScreenController
package screensframework;
import java.awt.Button;
import java.io.File;
import java.io.IOException;
import java.net.URL;
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.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
/**
* FXML Controller class
*
* @author Kenny
*/
public class Screen2Controller implements Initializable , ControlledScreen {
ScreensController myController;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
@FXML
private void goToScreen1(ActionEvent event){
myController.setScreen(ScreensFramework.screen1ID);
}
@FXML
private void goToScreen3(ActionEvent event){
myController.setScreen(ScreensFramework.screen3ID);
}
/*________________________________________________________________________*/
@FXML
private GridPane gridPane;
@FXML
private Text detailsTitle;
@FXML
private ObservableList<PieChart.Data> pieChartData;
@FXML
private PieChart piechart = new PieChart(pieChartData);
@FXML
private AnchorPane content;
@FXML
private Button button2;
@FXML
private void handleButton1Action(ActionEvent event) {
System.out.println("test");
}
@FXML public void handleMouseClick(MouseEvent arg0)
{
myController.setScreen(ScreensFramework.screen3ID);
}
@FXML
private void handleButton2Action(ActionEvent event) {
gridPane.setGridLinesVisible(true);
ReadExcelFile r = new ReadExcelFile();
File f = new File("C:\\Users\\k.vanmeenen\\Desktop\\ExportDefectsGraph.xlsx");
float[][] tab = r.ReadExcelGrid(f);
pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Blocking", tab[0][0]),
new PieChart.Data("Non Blocking, disturbing", tab[1][0]),
new PieChart.Data("Partial Blocking", tab[2][0]),
new PieChart.Data("Cosmetic", tab[3][0]));
piechart.setTitle("Defects per severity >");
piechart.setData(pieChartData);
String cssChart = ScreensFramework.class.getResource("ChartPieCss.css").toExternalForm();
piechart.getStylesheets().add(cssChart);
detailsTitle.setText("E-Contracting");
Text rankTitle1 = new Text(Double.toString(Math.floor(tab[0][1])));
gridPane.add(rankTitle1,1,1);
Text rankTitle2 = new Text(Double.toString(Math.floor(tab[0][2])));
gridPane.add(rankTitle2,2,1);
Text rankTitle3 = new Text(Double.toString(Math.floor(tab[0][3])));
gridPane.add(rankTitle3,3,1);
Text rankTitle4 = new Text(Double.toString(Math.floor(tab[0][4])));
gridPane.add(rankTitle4,4,1);
Text rankTitle5 = new Text(Double.toString(Math.floor(tab[0][5])));
gridPane.add(rankTitle5,5,1);
Text rankTitle6 = new Text(Double.toString(Math.floor(tab[0][6])));
gridPane.add(rankTitle6,6,1);
Text rankTitle7 = new Text(Double.toString(Math.floor(tab[0][7])));
gridPane.add(rankTitle7,7,1);
Text rankTitle8 = new Text(Double.toString(Math.floor(tab[0][8])));
gridPane.add(rankTitle8,8,1);
Text rankTitle9 = new Text(Double.toString(Math.floor(tab[0][9])));
gridPane.add(rankTitle9,9,1);
Text rankTitle10 = new Text(Double.toString(Math.floor(tab[0][10])));
gridPane.add(rankTitle10,10,1);
Text rankTitle11 = new Text(Double.toString(Math.floor(tab[0][0])));
gridPane.add(rankTitle11,11,1);
Text rankTitle21 = new Text(Double.toString(Math.floor(tab[1][1])));
gridPane.add(rankTitle21,1,2);
Text rankTitle22 = new Text(Double.toString(Math.floor(tab[1][2])));
gridPane.add(rankTitle22,2,2);
Text rankTitle23 = new Text(Double.toString(Math.floor(tab[1][3])));
gridPane.add(rankTitle23,3,2);
Text rankTitle24 = new Text(Double.toString(Math.floor(tab[1][4])));
gridPane.add(rankTitle24,4,2);
Text rankTitle25 = new Text(Double.toString(Math.floor(tab[1][5])));
gridPane.add(rankTitle25,5,2);
Text rankTitle26 = new Text(Double.toString(Math.floor(tab[1][6])));
gridPane.add(rankTitle26,6,2);
Text rankTitle27 = new Text(Double.toString(Math.floor(tab[1][7])));
gridPane.add(rankTitle27,7,2);
Text rankTitle28 = new Text(Double.toString(Math.floor(tab[1][8])));
gridPane.add(rankTitle28,8,2);
Text rankTitle29 = new Text(Double.toString(Math.floor(tab[1][9])));
gridPane.add(rankTitle29,9,2);
Text rankTitle30 = new Text(Double.toString(Math.floor(tab[1][10])));
gridPane.add(rankTitle30,10,2);
Text rankTitle31 = new Text(Double.toString(Math.floor(tab[1][0])));
gridPane.add(rankTitle31,11,2);
Text rankTitle32 = new Text(Double.toString(Math.floor(tab[2][1])));
gridPane.add(rankTitle32,1,3);
Text rankTitle33 = new Text(Double.toString(Math.floor(tab[2][2])));
gridPane.add(rankTitle33,2,3);
Text rankTitle34 = new Text(Double.toString(Math.floor(tab[2][3])));
gridPane.add(rankTitle34,3,3);
Text rankTitle35 = new Text(Double.toString(Math.floor(tab[2][4])));
gridPane.add(rankTitle35,4,3);
Text rankTitle36 = new Text(Double.toString(Math.floor(tab[2][5])));
gridPane.add(rankTitle36,5,3);
Text rankTitle37 = new Text(Double.toString(Math.floor(tab[2][6])));
gridPane.add(rankTitle37,6,3);
Text rankTitle38 = new Text(Double.toString(Math.floor(tab[2][7])));
gridPane.add(rankTitle38,7,3);
Text rankTitle39 = new Text(Double.toString(Math.floor(tab[2][8])));
gridPane.add(rankTitle39,8,3);
Text rankTitle40 = new Text(Double.toString(Math.floor(tab[2][9])));
gridPane.add(rankTitle40,9,3);
Text rankTitle41 = new Text(Double.toString(Math.floor(tab[2][10])));
gridPane.add(rankTitle41,10,3);
Text rankTitle42 = new Text(Double.toString(Math.floor(tab[2][0])));
gridPane.add(rankTitle42,11,3);
Text rankTitle42b = new Text(Double.toString(Math.floor(tab[3][1])));
gridPane.add(rankTitle42b,1,4);
Text rankTitle43 = new Text(Double.toString(Math.floor(tab[3][2])));
gridPane.add(rankTitle43,2,4);
Text rankTitle44 = new Text(Double.toString(Math.floor(tab[3][3])));
gridPane.add(rankTitle44,3,4);
Text rankTitle45 = new Text(Double.toString(Math.floor(tab[3][4])));
gridPane.add(rankTitle45,4,4);
Text rankTitle46 = new Text(Double.toString(Math.floor(tab[3][5])));
gridPane.add(rankTitle46,5,4);
Text rankTitle47 = new Text(Double.toString(Math.floor(tab[3][6])));
gridPane.add(rankTitle47,6,4);
Text rankTitle48 = new Text(Double.toString(Math.floor(tab[3][7])));
gridPane.add(rankTitle48,7,4);
Text rankTitle49 = new Text(Double.toString(Math.floor(tab[3][8])));
gridPane.add(rankTitle49,8,4);
Text rankTitle50 = new Text(Double.toString(Math.floor(tab[3][9])));
gridPane.add(rankTitle50,9,4);
Text rankTitle51 = new Text(Double.toString(Math.floor(tab[3][10])));
gridPane.add(rankTitle51,10,4);
Text rankTitle52 = new Text(Double.toString(Math.floor(tab[3][0])));
gridPane.add(rankTitle52,11,4);
}
@FXML
private void handleButtonClearAction(ActionEvent event) {
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList();
piechart.setTitle("");
piechart.setData(pieChartData);
} }
'直到现在,我可以点击饼图的每个部分然后转到另一个屏幕但是我想要的是当我按下饼图的一部分时它识别出我点击了哪一个。
谢谢 肯尼
答案 0 :(得分:0)
这就是答案。
将正常标签和鼠标单击操作添加到PieChart,然后添加以下代码:
@FXML
private void mouseClick(MouseEvent mouseevent ) {
for (final PieChart.Data data : piechart.getData()) {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
label.setTranslateX(e.getSceneX());
label.setTranslateY(e.getSceneY());
label.setText(String.valueOf(data.getPieValue()) + "%");
}
});
}
}