简体   繁体   中英

connecting scenebuilder UI fxml with javafx application

I have designed a UI in javafx scene builder, which has a simple button in stackpane.And I have named the controller class as simplecclass. I have saved the fxml as simple.fxml. I have created a controller class in netbeans, which simply prints some msg on clicking the button.

In the NewFXBuilder java , I have loaded simple.fxml. Please find below the NewFXBuilder.java code.

package javafxapplication2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;

public class NewFXbuilder extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            StackPane page = (StackPane) FXMLLoader.load(NewFXbuilder.class.getResource("simple.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("FXML is Simple");
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(NewFXbuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) {
        Application.launch(NewFXbuilder.class, (java.lang.String[])null);
  }
}

My simple.fxml,simplecclass.java and NewFXbuilder.java all resides in the same folder javafxapplication2.

while running NewFXBuilder.java, but it gives me the following error.

javafxapplication2.NewFXbuilder start SEVERE: null javafx.fxml.LoadException: java.lang.ClassNotFoundException: simplecclass

javafxapplication2.NewFXbuilder start SEVERE: null javafx.fxml.LoadException: java.lang.ClassNotFoundException: simplecclass

Looks like a problem in the FXML file. Make sure you import simplecclass in the FXML file.

The mistake I did was forgotten to add java packagename in the controller class name field in scene builder. It should have been packagename.simplecclass but I gave simplecclass alone,which is a mistake.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM