I'm looking at some javafx tutorials using scene builder. I created my fxml file but when I try to load it I get an error:
Jan 27, 2017 10:38:39 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.101
java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.GridPane
at application.Main.start(Main.java:16)
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)
at java.lang.Thread.run(Unknown Source)
I checked on SO and found this thread but I still don't get what I'm doing wrong as I'm not even using an Anchor pane. Anyway, here is the main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
GridPane grid = FXMLLoader.load(getClass().getResource("ui.fxml"));
Scene scene = new Scene(grid,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I tried to use the BorderPane, but same results. If I remove the loading fxml functionality it all works and display an empty stage, which is what I was expecting. Any idea what is wrong with it? EDIT: OK I've added the FXML file and now yes I can see the anchorPane there (sorry but I've never worked with JavaFX so I'm still learning.):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
<children>
<GridPane prefHeight="396.0" prefWidth="425.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="295.0" minHeight="10.0" prefHeight="295.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="124.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="75.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TableView prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="3">
<columns>
<TableColumn prefWidth="75.0" text="Column X" />
<TableColumn prefWidth="75.0" text="Column X" />
<TableColumn prefWidth="75.0" text="Column X" />
</columns>
</TableView>
</children>
</GridPane>
</children>
</AnchorPane>
Not entirely sure why this has been marked as duplicate as the question indicated covers ClassCastException in general whereas I'm referring to a specific problem that is causing a ClassCastException. In any case I've changed the offending line to this:
AnchorPane pane = FXMLLoader.load(getClass().getResource("ui.fxml"));
and it works. When I was following the tutorial I din't even know that I was supposed to be looking at an AnchorPane.