-1

While I was trying to load two different scenes, it doesn't open the image FXML, basically I want the image FXML to be opened first followed by welcome screen. Some how Image scene is not showing.

Main.java

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
    System.out.println("im in main");
    Parent root = FXMLLoader.load(getClass().getResource("ImageScreen.fxml"));


    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.setTitle("test");
    stage.show();
}
public static void main(String[] args) {
    launch(args);
}

}

ImageScreen.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="imagepane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"        xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.ImageScreenController">
<children>
    <ImageView fitHeight="438.0" fitWidth="603.0" pickOnBounds="true"
        preserveRatio="true">
        <image>
            <Image url="@bamboo-fountain-and-zen-stone.jpg" />
        </image>
    </ImageView>
    <Pane prefHeight="200.0" prefWidth="200.0" />
</children>

ImageScreenController

public class ImageScreenController implements Initializable {

@FXML
private StackPane imagepane;

public static AnchorPane welcomepane;

@Override
public void initialize(URL url, ResourceBundle rb) {

    AnchorPane pane;
    try {
        System.out.println("im in controller");
        pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml"));
        imagepane.getChildren().setAll(pane);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

WelcomeFXMLDoc.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane fx:id="welcomepane" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WelcomeFXMLController">
<children>
    <Text layoutX="212.0" layoutY="100.0" strokeType="OUTSIDE"
        strokeWidth="0.0" text="Welcome" wrappingWidth="176.13671875">
        <font>
            <Font size="35.0" />
        </font>
    </Text>
</children>

WelcomeFXMLController.java

public class WelcomeFXMLController implements Initializable {

/**
 * Initializes the controller class.
 *
 */

@FXML
private AnchorPane welcomepane;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

}

user2459816
  • 85
  • 2
  • 11

1 Answers1

1

In ImageScreenController you use imagepane.getChildren().setAll(pane). setAll() clear collection and then add new elements. In this case you remove ImageView from scene graph and show only welcomepane.

Use imagepane.getChildren().add(pane); instead and then remove welcomepane when you don't need is anymore.

EDIT

Based on your comment.

Use PauseTransition to wait for WelcomeFXMLController:

@Override
public void initialize(URL url, ResourceBundle rb) {
    PauseTransition pt = new PauseTransition(Duration.seconds(10));
    pt.setOnFinished(e -> {
        AnchorPane pane;
        try {
            System.out.println("im in controller");
            pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml"));
            imagepane.getChildren().setAll(pane);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
    pt.play();
}
MBec
  • 2,172
  • 1
  • 12
  • 15
  • If I follow this approach, welcome scene will overwrite on top of image scene which i don't want, I want once the image scene is loaded completely, Image scene should go away and should load the welcome scene alone, also if I add any button on image scene and if i click on this it will open welcome scene but I don't want to put a button on image scene. – user2459816 Feb 17 '17 at 12:31
  • Do you have some long running process while loading image scene? Because in provided code, your image screen will show immediately so there is no point of showing it. – MBec Feb 17 '17 at 12:44
  • I want to load image first wait for 10 sec and then load welcome scene..waiting code is not provided above.. if you could it will be help full – user2459816 Feb 17 '17 at 12:50
  • @user2459816 Don't you think you should put information such as "what I want the code to actually do" into the question? – James_D Feb 17 '17 at 12:55
  • @James_D : Please read the question description carefully, "basically I want the image FXML to be opened first followed by welcome screen. Some how Image scene is not showing" , are you expecting any thing else ? – user2459816 Feb 17 '17 at 12:59
  • @MBec, worked like charm, thanks but one question as a best practice should I keep the code in 'ImageScreenController' OR 'WelcomeFXMLController' since I'm new to Java FX, Please clarify it because your comment mentioned as WelcomeFXMLController – user2459816 Feb 17 '17 at 13:22
  • I would keep code in `Main` class. First load image view, put it into `Scene` then use `PauseTransition`. On transition finish, load welcome view and then add it to `Scene` as a root `Node`. – MBec Feb 17 '17 at 13:35
  • @user2459816 You said nothing at all about wanting a pause/delay in there, which is actually the entire point of your question. The title of your question is misleading because it is simply not a true statement; the image FXML is opening fine, you just immediately replace it with something else. Because of that, your question is essentially useless to anyone else. Make the question describe properly what you want to do. – James_D Feb 17 '17 at 13:51