JavaFX图表-Java.lang.IllegalArgumentException:添加了重复的系列 [英] JavaFX Chart - Java.lang.IllegalArgumentException : Duplicate series added

查看:129
本文介绍了JavaFX图表-Java.lang.IllegalArgumentException:添加了重复的系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 clear()方法从图表中删除数据。

I'm using clear() method for removing data from chart.

@FXML
public void restartButtonClicked() {
    System.out.println("Restarting...");
    timeLine.getKeyFrames().clear();

    axesTest.getData().clear();
}

在此功能中,但是一旦单击按钮,它就会起作用-但第二个当我要单击它时,它会引发异常:

In this function, but once button get clicked - it works.. but second time while I am gonna click it, it throw exception :

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Duplicate series added
    at javafx.scene.chart.XYChart.lambda$new$550(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(Unknown Source)
    at javafx.collections.ObservableListBase.fireChange(Unknown Source)
    at javafx.collections.ListChangeBuilder.commit(Unknown Source)
    at javafx.collections.ListChangeBuilder.endChange(Unknown Source)
    at javafx.collections.ObservableListBase.endChange(Unknown Source)
    at javafx.collections.ModifiableObservableListBase.addAll(Unknown Source)
    at sample.Controller.startButtonClicked(Controller.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

所以问题出在方法的138行以及159行代码中:

So the problem is in 138 line as well as in 159 line of code from method :

@SuppressWarnings("unchecked")
@FXML
public void startButtonClicked(ActionEvent event) {
if (comboBox.getValue() == null) {
    System.out.print("Nie wybrano opcji.");
} else {
    if(comboBox.getValue().equals("Gaussian Wave : non-Dispersive")) {
        System.out.println("Gaussian Wave : non-Dispersive");
        axesTest.setTitle("Gaussian Wave : non-Dispersive");

        series.setName("bla");
        series1.setName("ssad");
        series2.setName("asf");

        seriesList.add(series);
        seriesList.add(series1);
        seriesList.add(series2);

        timeLine = new Timeline();
        gaussianWave = new GaussianWave( seriesList, axes, timeLine );
        seriesList = gaussianWave.draw();
        gaussianWave.update(axesTest);

        axesTest.getData().retainAll();
        axesTest.getData().addAll(seriesList);
    } else if(comboBox.getValue().equals("Gaussian Wave : Dispersive")) {
        System.out.println("Gaussian Wave : Dispersive");
        axesTest.setTitle("Gaussian Wave : Dispersive");

        series.setName("bla");
        series1.setName("ssad");
        series2.setName("asf");

        seriesList.add(series);
        seriesList.add(series1);
        seriesList.add(series2);

        timeLine = new Timeline();
        gaussianWaveDispersive = new GaussianWaveDispersive( seriesList, axes, timeLine );
        seriesList = gaussianWaveDispersive.draw();
        gaussianWaveDispersive.update(axesTest);

        axesTest.getData().retainAll();
        axesTest.getData().addAll(seriesList);
    }
}

}

并且问题出在:

axesTest.getData().addAll(seriesList);

在清除图表中的所有数据后,为什么会出现此异常?我该如何解决?

Why do I get this exception after clearing all of data from the chart ? How could I fix this?

推荐答案

您清除存储在ObservableList https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/XYChart.Series.html#dataProperty rel = nofollow noreferrer> dataProperty 系列的$ c> :

You clear the ObservableList stored in the dataProperty of the Series:

axesTest.getData().clear();

但是您使用的是称为 seriesList ,而不是直接添加 Data

but you use an intermediate list(?) called seriesList, rather than adding Data directly:

seriesList.add(series);
seriesList.add(series1);
seriesList.add(series2)

...

axesTest.getData().addAll(seriesList);

问题是您不清除 seriesList 在添加元素之前,因此在第二次单击时包含两次元素,这将导致 IllegalArgumentException

The problem is you don't clear seriesList before adding the elements, so it contains the elements on the second click twice, which will result in an IllegalArgumentException.

实际上我不知道

gaussianWave = new GaussianWave( seriesList, axes, timeLine );
seriesList = gaussianWave.draw();

seriesList 似乎包含前一个这些行之后的 Data 对象。

but it seems that seriesList contains the previous Data objects after these lines.

这篇关于JavaFX图表-Java.lang.IllegalArgumentException:添加了重复的系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆