I am experimenting with FXML. Here is the java file. It resides in /Users/morrison/teaching/java/xml and all files have read permission.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
import java.io.File;
import java.net.URL;
public class Example extends Application
{
private Stage primary;
@Override
public void start(Stage primary)
{
this.primary = primary;
Parent root = null;
try
{
File f = new File("/Users/morrison/teaching/java/xml/");
FXMLLoader loader = new FXMLLoader(f.toURI().toURL());
System.out.println(loader.getLocation());
root = loader.load(getClass().getResource("Example.fxml"));
primary.setScene(new Scene(root, 500, 500));
primary.setTitle("FXML Example");
primary.show();
}
catch(IOException ex){System.err.println("file cant be found");}
}
}
Here is the .fxml
file, which resides in the same directory.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.canvas.Canvas?>
<BorderPane xmlns:fx="http://javafx.com/fxml">
<top>
<HBox>
<Button text="Go!" onAction="#handleGo" fx:id="goButton"/>
<Button text="Stop!" onAction="#handleStop" fx:id="stopButton"/>
</HBox>
</top>
<center>
<canvas fx:id="colorField">
</canvas>
</center>
</BorderPane>
When I run this, I am being told the FXML file can't be found.
Is there some other piece I am missing?