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?
Kameron Hazelwood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.