So as I'm trying to do the tutorials for JavaFX and am working on the FXML example. But whenever I add something to the GridPane in the .fxml file the program crashes. It opens a normal GridPane if nothing else is put in it.
Code For the FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="fxmlexample.FXMLExampleController"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
<Text text="Welcome"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2"/>
<Label text="User Name:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Password:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<PasswordField fx:id="passwordField"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
</GridPane>
Code for the main class:
package fxmlexample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLExample extends Application {
public static void main(String[] args) {
Application.launch(FXMLExample.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
}
What exactly is causing it to crash?
I was attempting the same thing and tracked an issue down to the "Insets" entry. There is an "Insets is not a valid type" error at the top of my output (after I straightened out my naming.) This might depend on the version of NetBeans in use. (I am using 7.2.1 but hope to get upgraded to 7.4 this afternoon.)
To fix it, I simply commented out the "Insets" entry:
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
Above mentioned line you have given wrong name of FXML file name( fxml_example.fxml
).
<GridPane fx:controller="fxmlexample.FXMLExampleController"
Here I can see that fxmlexample
is the name of your FXML file. Just fix it by removing _
and your code is fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.