Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

javafx - Using custom classes in FXML without packages

There are several other question regarding this topic, but they all use files placed in packages unlike mine. I have three simple files:

  1. FXMLtest.java - The main class extending Application
  2. Bean.java - extends Circle. Has an extra property which follows java bean conventions
  3. view2.fxml

(all files are in the same folder)
I infer (from the stackrace) that the error arises since FXMLLoader is unable to locate Bean, which is on the scene graph in view2.fxml. Perhaps it has something to do with not using a package. If so, I would just like to know why packages are necessary. Given below are the three files and the stack trace.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
public class FXMLtest extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("view2.fxml")), 300,100);
        stage.setTitle("JavaFX Example");
        stage.setScene(scene);
        stage.show();
    }
    public static void main (String[] args){
        launch(args);
    }
}
import javafx.scene.control.Button;
import javafx.beans.property.SimpleDoubleProperty;
public class Bean extends Button{
    private SimpleDoubleProperty foo;
    Bean(){
        super();
    }
    void setFoo(double x){
        foo.set(x);
    }
    double getFoo(){
        return foo.get();
    }
    SimpleDoubleProperty fooProperty(){
        return foo;
    }
}
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.control.Button?>
<?import Bean?>
<Pane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" >
   <children>
      <Button text="testButton" />
      <Bean foo="4.4" text="button2" />
   </children>
</Pane>
Exception in Application start method
Exception in thread "JavaFX BlueJ Helper" 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: javafx.fxml.LoadException: 
/C:/java/view2.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
    at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
    at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)

    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at FXMLtest.start(FXMLtest.java:10)
    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
Caused by: java.lang.ClassNotFoundException
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    ... 20 more

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...