Cześć,
Mam problem z Line Chart w javafx.
Dostaje jakiś dziwny wyjątek kiedy chcę dodać do lineChart dane.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chartslab4;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author student
*/
public class ChartsLab4 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(800);
stage.setHeight(600);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
```java
package chartslab4;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
public class FXMLDocumentController {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private AreaChart<String, Number> areaChart;
@FXML
private LineChart<String, Number> lineChart;
@FXML
private ScatterChart<String, Number> scatterChart;
@FXML
private RadioButton scatterChartRadioButton;
@FXML
private ToggleGroup chart;
@FXML
private RadioButton lineChartRadioButton;
@FXML
private RadioButton areaChartRadioButton;
@FXML
void radioButtonSelection(ActionEvent event) {
this.disableAllcharts();
if (lineChartRadioButton.isSelected()) {
lineChart.setVisible(true);
} else if (scatterChartRadioButton.isSelected()) {
scatterChart.setVisible(true);
} else if (areaChartRadioButton.isSelected()) {
areaChart.setVisible(true);
}
}
private List<Series<String, Number>> data;
private void disableAllcharts() {
lineChart.setVisible(false);
scatterChart.setVisible(false);
areaChart.setVisible(false);
}
@FXML
void initialize() {
assert scatterChart != null : "fx:id=\"scatterChart\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert lineChart != null : "fx:id=\"lineChart\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert areaChart != null : "fx:id=\"areaChart\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert lineChartRadioButton != null : "fx:id=\"lineChartRadioButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert chart != null : "fx:id=\"chart\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert scatterChartRadioButton != null : "fx:id=\"scatterChartRadioButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert areaChartRadioButton != null : "fx:id=\"areaChartRadioButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
try {
this.initcharts();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
System.exit(-1);
}
/*final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month");
lineChart = new LineChart<String,Number>(xAxis,yAxis);*/
this.disableAllcharts();
lineChart.setVisible(true);
areaChart.setTitle("Area chart");
lineChart.setTitle("Line chart");
scatterChart.setTitle("Scatter chart");
lineChart.setCreateSymbols(false);
areaChart.getData().addAll(data);
lineChart.getData().addAll(data);
scatterChart.getData().addAll(data);
}
private void initcharts() throws FileNotFoundException, IOException {
String[] files = {this.getClass().getResource("file1.txt").getPath(),
this.getClass().getResource("file2.txt").getPath(),
this.getClass().getResource("file3.txt").getPath()};
data = new ArrayList<>();
for (String f : files) {
System.out.println(f);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
Series<String, Number> seria = new Series<>();
ObservableList<Data<String, Number>> list = FXCollections.observableArrayList();
String line;
int y = 1;
while ((line = br.readLine()) != null) {
list.add(new Data<>(Integer.toString(y++), line.length()));
}
seria.setName(f);
seria.setData(list);
data.add(seria);
}
}
}
```java
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.AreaChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.ScatterChart?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="chartslab4.FXMLDocumentController">
<children>
<VBox layoutX="110.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<StackPane>
<children>
<ScatterChart fx:id="scatterChart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</ScatterChart>
<LineChart fx:id="lineChart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<AreaChart fx:id="areaChart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</AreaChart>
</children>
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
</StackPane>
<HBox spacing="15.0">
<children>
<RadioButton fx:id="lineChartRadioButton" mnemonicParsing="false" onAction="#radioButtonSelection" selected="true" text="Line chart">
<toggleGroup>
<ToggleGroup fx:id="chart" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="scatterChartRadioButton" mnemonicParsing="false" onAction="#radioButtonSelection" text="Scatter Chart" toggleGroup="$chart" />
<RadioButton fx:id="areaChartRadioButton" mnemonicParsing="false" onAction="#radioButtonSelection" text="Area Chart" toggleGroup="$chart" />
</children>
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassCastException: javafx.scene.shape.Path cannot be cast to javafx.scene.Group
at javafx.scene.chart.AreaChart.layoutPlotChildren(AreaChart.java:444)
at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:731)
at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:94)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene.preferredSize(Scene.java:1646)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
at javafx.stage.Window$9.invalidated(Window.java:846)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:922)
at javafx.stage.Window.show(Window.java:937)
at javafx.stage.Stage.show(Stage.java:259)
at chartslab4.ChartsLab4.start(ChartsLab4.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application chartslab4.ChartsLab4
Błąd jest kiedy zostaje wywoływana linijka: lineChart.getData().addAll(data); w drugim pliku
Proszę o pomoc w rozwiązaniu tego problemu.
Z góry dziękuje :)