1
votes

I am new to JavaFX 2.0 and to do practice I have followed the Oracle example about Line Chart (Stock Monitoring) http://docs.oracle.com/javafx/2.0/charts/line-chart.htm#CIHGBCFI

Now I would like to ad a CSS class in the same package to modify Stroke, color, etc but I can't succed.

Searching here on StackOverflow I have found

scene.getStylesheets().add(this.getClass().getResource("linechart.css").toExternalForm());

but I still get errors

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)

My java class name is JavaFXApplication2 and CSS class name is linechart.css

Would someone be so kind to explain me where I am wrong?

There is another nice tutorial about the CSS use with Stock Monitoring example here http://docs.oracle.com/javafx/2.0/charts/css-styles.htm

but I do not know how to add the CSS examples to the java class

Thanks

2
Stack:Caused by: java.lang.NullPointerException at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:75) Where line 75 is the scene.getStylesheets(). etc statement - Alberto acepsut
At that line NullPointerException may occur if the scene is not yet initialized or if the linechart.css file does not exist. Double check the CSS file name and its location in a package. Alternatively you can post your start(Stage primaryStage) method in JavaFXApplication2 here. - Uluk Biy
public class JavaFXApplication2 extends Application { @Override public void start(Stage stage) { stage.setTitle("Lines plot"); final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 23)); series1.getData().add(new XYChart.Data("Feb", 14)); series1.getData().add(new XYChart.Data("Mar", 15)); - Alberto acepsut
Scene scene = new Scene(lineChart, 800, 600); lineChart.getData().addAll(series1, series2); stage.setScene(scene); stage.show(); - Alberto acepsut
It seems ok. I suspect that the css file is not being copied to the folder where the java files are compiled. Are you using an IDE? Can you add this line to your app System.out.println(this.getClass().getResource(".").toExternalForm()); and copy the css file to the folder path printed by this line. - Uluk Biy

2 Answers

1
votes

I suspect that the css file is not being copied to the folder where the java files are compiled. Are you using an IDE? Can you add this line to your app System.out.println(this.getClass().getResource(".").toExternalForm());
and copy the css file to the folder path printed by this line.

0
votes

You can build the project or copy the css file to the build path.If you using netbeans,you must build the project once after new resource file added.