0
所以我試圖創建一個在圖表上有值的應用程序,它通常顯示出來。但是,我也試圖添加一個鼠標事件,當我點擊一個特定的條形圖條時被調用。更具體地說,它用來自數據庫的更多細節更新狀態欄。JavaFX添加一個MouseEvent到BarChart裏面的一個條形圖
Group root = new Group();
String[] years = {"Opened", "Closed", "Still Opened"};
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.observableArrayList(years));
NumberAxis yAxis = new NumberAxis("Jobs", 0.0d, 1, .2);
BarChart.Series<String, ObservableList> s1 = new BarChart.Series("51 ", FXCollections.observableArrayList(
new BarChart.Data(years[0], Math.random()),
new BarChart.Data(years[1], Math.random()),
new BarChart.Data(years[2], Math.random())));;
BarChart.Series<String, ObservableList> s2 = new BarChart.Series("52 ", FXCollections.observableArrayList(
new BarChart.Data(years[0], Math.random()),
new BarChart.Data(years[1], Math.random()),
new BarChart.Data(years[2], Math.random())));
BarChart.Series<String, ObservableList> s3 = new BarChart.Series("53 ", FXCollections.observableArrayList(
new BarChart.Data(years[0], Math.random()),
new BarChart.Data(years[1], Math.random()),
new BarChart.Data(years[2], Math.random())));
BarChart.Series<String, ObservableList> s4 = new BarChart.Series("54 ", FXCollections.observableArrayList(
new BarChart.Data(years[0], Math.random()),
new BarChart.Data(years[1], Math.random()),
new BarChart.Data(years[2], Math.random())));
ObservableList barChartData = FXCollections.observableArrayList(s1,s2,s3,s4);
this.statusMessage.setText("Setting listener");
for (XYChart.Data<String, ObservableList> dt : s1.getData())
{
statusMessage.setText("Acting on: " + dt.getXValue());
dt.getNode().setOnMouseClicked((MouseEvent event) -> {
statusMessage.setText("Clicked");
});
}
BarChart chart = new BarChart(xAxis, yAxis, barChartData, 25.0d);
這個應用程序將使用和不使用的情況下進行編譯,但與事件,當我創建失敗,無法顯示它的圖表。我認爲它的,因爲事件沒有被加載到酒吧
我得到的錯誤:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1768) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:204) at javafx.scene.Node.fireEvent(Node.java:8175) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:204) at javafx.scene.Scene$MouseHandler.process(Scene.java:3746) at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345) at com.sun.glass.ui.View.handleMouseEvent(View.java:526) at com.sun.glass.ui.View.notifyMouse(View.java:898)
只是一個猜測我沒有試過,但是文檔說使用[節點](http://docs.oracle.com/javafx/2/api/javafx/scene/chart/XYChart.Data。 html#nodeProperty)用於鼠標監聽器。 – brian
JavaFx利用節點作爲應用程序內部的組件。 – Michael
我覺得你只需要在BarChart圖表= new BarChart ...之後添加你的getNode() – brian