简体   繁体   中英

[JavaFX-Scenebuilder][Custom Controller] Load error in Scenebuilder

I'm new to stackoverflow so if I do some mistakes, don't hesitate and tell me!

I've been working on a personnal project recently and wanted to create a GUI with specific widgets. I am working in Java using JavaFX with its GUI: scenebuilder. So far, I've been able to overcome all my issues by myself thanks to other questions asked here. This one is about adding my own controller to my project using scenebuilder. I have created my controller (a simple Button with an overlapping ImageView github link ) using the model found here . As you can test, this sample app works fine. After that, I export as jar and try to add it in scenebuilder.

Here is my issue: if I don't load (line 32 of IconedButton.java) I can see my control in scenebuilder (without any display but it appears on the list) but if i do load it it disappears and I can't see it on scenebuilder. I assume my issue comes from my fxml file but it loads correctly outside scenebuilder and I have tried everything I have found about adding custom controllers in scenebuilder. I'd like to use it after on another project and maybe adding it as a jar isn't the right way to do it but I try to do things step after step in order to understand everything.

I hope I haven't done anything wrong with this post and believe someone out there can help me!

Find here my code: github link and here the tutorial I've followed

EDIT:

App.java:

package iconbutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        var javaVersion = SystemInfo.javaVersion();
        var javafxVersion = SystemInfo.javafxVersion();

        IconedButton iconedbutton = new IconedButton();
        iconedbutton.setText("Hello!");
        
        stage.setScene(new Scene(iconedbutton));
        stage.setTitle("Iconed Button");
        stage.setWidth(300);
        stage.setHeight(200);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

IconedButton.java

package iconbutton;



import java.io.IOException;

import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;

/**
 * Sample custom control hosting a text field and a button.
 */
public class IconedButton extends AnchorPane {
    @FXML protected Button button;
    @FXML protected ImageView icon;

    public IconedButton() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/IconedButton.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        
        try {
            fxmlLoader.load();            
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
    
    public String getText() {
        return button.getText();
    }
    
    public void setText(String value) {
        button.setText(value);
    }
    
    public void setIcon(Image img) {
        icon.setImage(img);
    }
    
    @FXML
    protected void buttonClicked() {
        System.out.println("The button was clicked!");
    }
}

module-info.java:

module iconbutton {
    requires javafx.controls;
    requires javafx.fxml;
    opens iconbutton to javafx.fxml;
    exports iconbutton;
}

IconedButton.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.ImageView?>

<fx:root prefHeight="106.0" prefWidth="364.0" type="AnchorPane" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <javafx.scene.layout.StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <children>
            <ImageView fx:id="icon" fitHeight="30.0" fitWidth="30.0" pickOnBounds="true" preserveRatio="true" StackPane.alignment="TOP_RIGHT" />
          </children>
      </javafx.scene.layout.StackPane>
       <Button fx:id="button" onAction="#buttonClicked" mnemonicParsing="false" prefWidth="285.0" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="15.0" />
   </children>
</fx:root>

EDIT2: I hava absolutely done nothing but sleep and the problem resolved itself. If you encounter the same issue just restart and pray I'd say!

Issue solved by itself. Restart your PC, do something else and come back later would be my best advice!

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