JavaFx and Springboot program isn’t switching scenes

I’ve been trying to learn settings up a program using javafx and springboot. I have primarily used github examples to get started with learning how to do this, if there’s an easier way please let me know.

I’ve been successful in getting the program to start up and launch the initial fxml page. However after clicking a button I want to be able to switch scenes. This is where I’m having a program. Essentially whenever I click the ‘Login’ button nothing happens. I’m not given an error or anything.

Launcher.java

public class Launcher {

    public static void main(String[] args) {
       FxApplication.main(args);
       // javafx.application.Application.launch(FxApplication.class, args);
    }


}

FxApplication.java

`@SpringBootApplication
@EnableJpaRepositories("com.example.gaitlabapp.repo")
@ComponentScan(basePackageClasses={FxApplication.class, UserLoginController.class})
public class FxApplication extends Application {

    private static Optional<ConfigurableApplicationContext> SPRING_CONTEXT = Optional.empty();

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

    @Override
    public void start(Stage stage) throws Exception {
        fireStartUpEvent(new StartUpEvent(stage));
    }

    @Override
    public void init() throws Exception{
        super.init();
        SPRING_CONTEXT = Optional.ofNullable(SpringApplication.run(FxApplication.class));
        start();

    }
    private void fireStartUpEvent(ApplicationEvent event){
        SPRING_CONTEXT.ifPresentOrElse(context -> context.publishEvent(event), () ->{
        });
    }

    private void start(){
        SPRING_CONTEXT = Optional.ofNullable(SpringApplication.run(FxApplication.class));
    }`

StartUpListener.java

@Component
public class StartUpListener implements ApplicationListener<StartUpEvent> {

    @Override
    public void onApplicationEvent(@NonNull StartUpEvent event) {
        try {
            new UserLoginController(event.getStage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

StartUpEvent.java


`public class StartUpEvent extends ApplicationEvent  {
    public StartUpEvent(Stage stage ) {
        super(stage);
    }
    public Stage getStage(){
        return (Stage) getSource();
    }
}`

UserLoginController.java

@FxmlView("UserLogin.fxml")
public class UserLoginController implements Initializable {

@Override
    public void initialize(URL location, ResourceBundle resources) {
        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    login(event);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

 public UserLoginController(Stage stage) throws IOException {
        try {
            Stage stage1 = new Stage();
            FXMLLoader fxmlLoader = new FXMLLoader(Launcher.class.getResource("/UserLogin.fxml"));

            fxmlLoader.setController(UserLoginController.class);
            Parent root = fxmlLoader.load();

            stage.setTitle("Login");
            stage.setWidth(450);
            stage.setHeight(250);

            stage.setScene(new Scene(root));
            stage.setResizable(false);
            stage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

@FXML
    public void login(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(Launcher.class.getResource("/PatientModule.fxml"));
        loader.setController(PatientModuleController.class);
        root = loader.load();
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setTitle("Patient Module");
        stage.setHeight(750);
        stage.setWidth(1050);
        stage.setScene(scene);
        stage.show();
        alertTimer = new Timeline(new KeyFrame(Duration.hours(1), event1 -> {
            showAlert();
        }));

    }
}

UserLogin.fxml

<AnchorPane prefHeight="196.0" prefWidth="475.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1"  >
            <children>
                <TextField fx:id="nameTextField" layoutX="234.0" layoutY="68.0" />
                <Button fx:id="loginButton" layoutX="133.0" layoutY="152.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="176.0" text="Login" />
                <Label layoutX="71.0" layoutY="68.0" prefHeight="25.0" prefWidth="110.0" text="Username">
                    <font>
                        <Font size="20.0" />
                    </font>
                </Label>
      <TextField layoutX="234.0" layoutY="116.0" />
      <Text layoutX="71.0" layoutY="36.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome to the Gait Lab!">
         <font>
            <Font size="20.0" />
         </font>
      </Text>
      <Label layoutX="71.0" layoutY="110.0" prefHeight="37.0" prefWidth="110.0" text="Password">
         <font>
            <Font size="20.0" />
         </font>
      </Label>
            </children>
</AnchorPane>

Before implementing Springboot, I had the fx:controller listed within the FXML document and had an onAction listed within the fxml tag. I set the controller within the UserLoginController class through the setController function. I also edited the loginButton.setOnAction to implement a login method to switch scenes. That doesn’t work either. So I found a stack overflow about using the @ComponentScan on the FxApplication.java file and that also hasn’t changed anything. I’m wondering if I possibly do not have the controller initialized properly. Does the @Component tag need to applied to each controller file that I’m using?

New contributor

Kameron Hazelwood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật