English 中文(简体)
与 JavaFX PieChaart 发行
原标题:Issue with JavaFX PieChart

I have an application with checkBox and 2 animated charts: Pie and Bar charts. When checkBox is checked, method which adds new data to a chart is fired. When checkBox is *un*checked, method which removes data from a chart is fired.

当我点击一个复选框不非常快时,一切都很好。但如果在动画完成之前再点击复选框,我就会重新发现这个错误:

 java.lang.NullPointerException
at javafx.scene.chart.PieChart$7.handle(PieChart.java:370)
at javafx.scene.chart.PieChart$7.handle(PieChart.java:367)
at   com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:217)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:158)
at javafx.animation.Timeline.impl_playTo(Timeline.java:182)
at com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(SingleLoopClipEnvelope.java:119)
at javafx.animation.Animation.impl_timePulse(Animation.java:953)
at com.sun.scenario.animation.shared.AnimationPulseReceiver.timePulse(AnimationPulseReceiver.java:117)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:289)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:454)
at com.sun.javafx.tk.quantum.QuantumToolkit$8.run(QuantumToolkit.java:325)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)

这一切都只发生在PieChart。BarChart正常工作。

以下是在动作事件上检查框的方法 :

  public void OnCheckFired(ActionEvent event) {
    Boolean b = checkBox.isSelected();
    chart.modifyChart(currentChart,b);
}

Thant是用来修改馅饼图表的代码:

@Override
public void makeModifications(Chart chart, Boolean b) {
    PieChart pie = (PieChart) chart;
    ObservableList<PieChart.Data> pieChartData = pie.getData();
    pieChartData = modify(b, pieChartData);
    pie.setData(pieChartData);
}

private ObservableList<Data> modify(Boolean b, ObservableList<PieChart.Data> pieChartData)  {
    if (b) {
        pieChartData.add(new PieChart.Data("newValue", 34));
    } else {
        int size = pieChartData.size() - 1;
        pieChartData.remove(size);
    }
    return pieChartData;
}
最佳回答

我修改了错误问题的代码, 也就是Sergey在回答中提及的错误问题。 将按钮替换为在动画播放时禁用的复选框, 动画完成时加半秒。 每次选择改变时, 它都会这样做 。

public class Tdesst extends Application {

    private void init(Stage primaryStage) {
        VBox root = new VBox();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("Sun", 20),
                new PieChart.Data("IBM", 12),
                new PieChart.Data("HP", 25),
                new PieChart.Data("Dell", 22),
                new PieChart.Data("Apple", 30));
        final PieChart chart = new PieChart(pieChartData);

        final CheckBox chkbox = new CheckBox("add/remove data");

        final Timeline animation = new Timeline(
                new KeyFrame(Duration.seconds(.5),
                new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent actionEvent) {
                        chkbox.setDisable(false);
                    }
                }));
        animation.setCycleCount(1);

        chkbox.selectedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldVal, Boolean newVal) {
                chkbox.setDisable(true);
                if (newVal) {
                    pieChartData.add(new PieChart.Data("newValue", 34));
                } else {
                    pieChartData.remove(pieChartData.size() - 1);
                }
                animation.play();
            }
        });

        root.getChildren().add(chart);
        root.getChildren().add(chkbox);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
        primaryStage.setTitle(VersionInfo.getRuntimeVersion());
    }

    public static void main(String[] args) {
        launch(args);
    }
}

或者您可以完全禁用动动

chart.setAnimated(false);
问题回答

不幸的是,你发现了一个虫子。我已经以 rel=“nofollow'>http://javafx-jira.kenai.com/browse/RT-21783提交。

与此同时,您可以在每次点击后禁用一秒钟的复选框。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签