简体   繁体   中英

Javafx loading Image from jar

I'm using Scene Builder 2.0 and eclipse Luna. In fxml file i have code of my scene with Images. If I test this on the eclipse everything is allright, but if I export this to runnable jar and then run this I have window without images... Here is part of code:

<ImageView layoutX="63.0" layoutY="14.0">
                     <image>
                        <Image url="@../Images/MainPage/O.png" />
                     </image>
                  </ImageView>

Structure of files:

Graph:
    MainPage:
        MainPage.fxml
    Images:
        MainPage:
            O.png

I think the problem is in the path, but I don't know how make this path. Code of loader:

public class ScreensController extends StackPane{

    private HashMap<String, Node> screens = new HashMap<>();

    public ScreensController() {
        super();
    }

    public void addScreen(String name, Node screen){
        System.out.println("name: "+name);
        System.out.println("screen: "+name);
        screens.put(name, screen);
    }

    public Node getScreen(String name){
        return screens.get(name);
    }

    //name - nazwa Screenu
    //rescue - nazwa pliku FXML
    public boolean loadScreen(String name, String rescue){
        try {
            System.out.println("loading Screen "+name+" path: "+rescue);
            System.out.println("0001");
            FXMLLoader myLoader= new FXMLLoader(getClass().getResource(rescue));
            System.out.println("0002");
            Parent loadScreeen = (Parent) myLoader.load();
            System.out.println("0003");
            ScreenControlled myScreenControler = ((ScreenControlled) myLoader.getController());
            System.out.println("0004");
            myScreenControler.setScreenParent(this);
            System.out.println("0005");
            addScreen(name, loadScreeen);//Dodanie do HashMap
            System.out.println("0006");
            return true;
        } catch (IOException e) {
            System.out.println("IOException Error in ScreenController: "+e.getMessage().substring(1,e.getMessage().length())
                    +"\n Cause: "+e.getCause()
                    +"\n Localized Message: "+e.getLocalizedMessage().substring(1,e.getLocalizedMessage().length())
                    +"\n Class: "+e.getClass()
                    +"\n ST: ");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setScreen(final String name){
        System.out.println("Setting Screen: "+name);
        if(screens.get(name) != null) { //screen loaded
            final DoubleProperty opacity = opacityProperty();

            if(!getChildren().isEmpty()) { //if there is more than one screen
                Timeline fade = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
                    new KeyFrame(new Duration(1000), new EventHandler<ActionEvent>(){

                        @Override
                        public void handle(ActionEvent t){
                            getChildren().remove(0); //remove the displayed screen
                            getChildren().add(0, screens.get(name)); //add the screen
                            Timeline fadeIn = new Timeline(
                                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                                    new KeyFrame(new Duration(800), new KeyValue(opacity, 1.0)));
                            fadeIn.play();
                        }
                    }, new KeyValue(opacity, 0.0)));
                    fade.play();
                }else{
                    setOpacity(0.0);
                    getChildren().add(screens.get(name)); //no one else been displayed, then just show
                    Timeline fadeIn = new Timeline(
                            new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                            new KeyFrame(new Duration(1000), new KeyValue(opacity, 1.0)));
                    fadeIn.play();
                }
            return true;
            }else{
                System.out.println("screen hasn't been loaded!!!\n");
                return false;
        }
    }

    public boolean unloadSCreen(String name) {
        if(screens.remove(name)==null) {
            System.out.println("Screen " +name+ " didn't exist");
            return false;
        }else{
            return true;
        }
    }
}

initialize function:

public void start(Stage stage) throws Exception {

        new Fonts().initialize();

        this.stage=stage;
        ScreensController mainContainer = new ScreensController();
        mainContainer.loadScreen(ScreensFramework.screen2ID,  ScreensFramework.screen2File);

        mainContainer.setScreen(ScreensFramework.screen2ID);

        root = new StackPane();//Group root = new Group();
        root.getChildren().addAll(mainContainer);
        Scene scene = new Scene(root);
        this.stage.setScene(scene);
        this.stage.show();
}

You are missing a / at the start of your path. Please use :

FXMLLoader myLoader= new FXMLLoader(getClass()
 .getResource(/com/gmail/Slupik98/Project_Stangar/Graph/MainPage/MainPage.fxml));

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