0

I have two packages p1 and p2.

P1 contains the main class, controller class and one fxml file. P2 contains the controller class and one fxml file.

I want to switch from p1 fxml to p2 fxml file.

here is the code I tried. this is in P1 package.

public void btncontinue(ActionEvent event)throws IOException {
        String filepath = "file:///D:/Programs/InteliJProjects/C/src/p1/sample2.fxml";
        Parent nextScene = FXMLLoader.load(getClass().getClassLoader().getResource(filepath));
        Scene scene = new Scene(nextScene);
        Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
        stage.setScene(scene);
        stage.show();
    }

The error i am getting is Location is required.

1 Answers1

0

Well I've checked this is the code from scene1 to scene2

 package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;


import java.io.IOException;

public class sample
{

    @FXML
    RadioButton male;

    @FXML
    AnchorPane rootPane;


    public void add() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("two.fxml"));
        rootPane.getChildren().setAll(pane);
    }

}

And this is how to return from scene2 to scene1

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;

public class Two
{
    @FXML
    Button back;

    @FXML
    AnchorPane secPane;

    public void returnBack() throws IOException
    {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
        secPane.getChildren().setAll(pane);
    }
}

I've tried this and it is working fine hope it will help you

YUSOF KHAN
  • 141
  • 11