To make your UserAndManagerController functional, we'll follow these main steps:
- Update the FXML: Assign
fx:id to the VBox elements representing "User" and "Manager" options and add onMouseClicked event handlers.
- Implement the Controller: Define the necessary event handling methods to open new windows when "User" or "Manager" is clicked. Also, handle the close button functionality.
Let's walk through each step in detail.
1. Update the FXML
First, modify your FXML to assign fx:id to the "User" and "Manager" VBox elements. Additionally, add onMouseClicked attributes to specify the methods that should be called when these elements are clicked.
Updated userAndManager.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="userAndManagerPane" prefHeight="642.0" prefWidth="935.0" styleClass="background" stylesheets="@userAndManager.css" xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.clinic.userandmanager.UserAndManagerController">
<!-- Background vector image -->
<ImageView fitHeight="413.0" fitWidth="863.0" layoutX="42.0" layoutY="128.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Images/design_element.png" />
</image>
</ImageView>
<!-- Container for User and Manager Options -->
<HBox alignment="CENTER" layoutX="211.0" layoutY="182.0" prefHeight="342.0" prefWidth="518.0" spacing="30" styleClass="selection-container">
<!-- User Option -->
<VBox fx:id="userOption" alignment="CENTER" prefHeight="168.0" prefWidth="135.0" spacing="10" styleClass="option-box" onMouseClicked="#handleUserClick">
<Label styleClass="option-label" text="User" />
<ImageView fitHeight="50" fitWidth="50">
<image>
<Image url="@../Icons/user.png" />
</image>
</ImageView>
</VBox>
<!-- Manager Option -->
<VBox fx:id="managerOption" alignment="CENTER" prefHeight="239.0" prefWidth="181.0" spacing="10" styleClass="option-box" onMouseClicked="#handleManagerClick">
<Label styleClass="option-label" text="Manager" />
<ImageView fitHeight="50" fitWidth="50">
<image>
<Image url="@../Icons/manager.png" />
</image>
</ImageView>
</VBox>
</HBox>
<!-- Close Button (Top-Right) -->
<Button fx:id="closeButton" layoutX="897.0" layoutY="14.0" prefHeight="40.0" prefWidth="43.0" styleClass="close-button" text="X" onAction="#handleClose"/>
</AnchorPane>
Changes Made:
2. Implement the Controller
Now, let's implement the UserAndManagerController with the necessary event handling methods.
Updated UserAndManagerController.java:
package com.example.clinic.userandmanager;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.Node;
import java.io.IOException;
public class UserAndManagerController {
@FXML
private VBox userOption;
@FXML
private VBox managerOption;
@FXML
private Button closeButton;
/**
* Handles the click event on the User option.
* Opens the User window.
*
* @param event the mouse click event
*/
@FXML
private void handleUserClick(MouseEvent event) {
try {
// Load the User window FXML
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/example/clinic/userandmanager/UserWindow.fxml"));
Parent userRoot = loader.load();
// Create a new stage for the User window
Stage userStage = new Stage();
userStage.setTitle("User Window");
userStage.setScene(new Scene(userRoot));
userStage.show();
} catch (IOException e) {
e.printStackTrace();
// Optionally, show an alert to the user
}
}
/**
* Handles the click event on the Manager option.
* Opens the Manager window.
*
* @param event the mouse click event
*/
@FXML
private void handleManagerClick(MouseEvent event) {
try {
// Load the Manager window FXML
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/example/clinic/userandmanager/ManagerWindow.fxml"));
Parent managerRoot = loader.load();
// Create a new stage for the Manager window
Stage managerStage = new Stage();
managerStage.setTitle("Manager Window");
managerStage.setScene(new Scene(managerRoot));
managerStage.show();
} catch (IOException e) {
e.printStackTrace();
// Optionally, show an alert to the user
}
}
/**
* Handles the action event on the Close button.
* Closes the current window.
*
* @param event the action event
*/
@FXML
private void handleClose(ActionEvent event) {
// Get the current stage using the close button
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.close();
}
}
Explanation of the Controller:
-
FXML Annotations:
- The
@FXML annotations link the UI components from the FXML to the controller.
VBox userOption and VBox managerOption represent the clickable options.
Button closeButton represents the close button.
-
Event Handling Methods:
-
handleUserClick:
- Triggered when the User
VBox is clicked.
- Loads
UserWindow.fxml (you'll need to create this FXML file for the User window).
- Creates and shows a new stage for the User window.
-
handleManagerClick:
- Triggered when the Manager
VBox is clicked.
- Loads
ManagerWindow.fxml (you'll need to create this FXML file for the Manager window).
- Creates and shows a new stage for the Manager window.
-
handleClose:
- Triggered when the Close button is clicked.
- Closes the current window.
-
Error Handling:
- If the FXML files for the User or Manager windows are not found or fail to load, the exceptions are caught, and stack traces are printed.
- For a production application, consider displaying user-friendly error messages using alerts.
3. Create Placeholder FXML Files for User and Manager Windows
To complete the setup, you'll need to create the FXML files (UserWindow.fxml and ManagerWindow.fxml) that will be loaded when the respective options are clicked. Here's how you can create simple placeholders:
Example UserWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.clinic.userandmanager.UserWindowController">
<children>
<Label text="User Window" layoutX="200" layoutY="200" />
</children>
</AnchorPane>
Example ManagerWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.clinic.userandmanager.ManagerWindowController">
<children>
<Label text="Manager Window" layoutX="200" layoutY="200" />
</children>
</AnchorPane>
Note: Don't forget to create corresponding controllers (UserWindowController.java and ManagerWindowController.java) even if they're empty for now.
4. Directory Structure and Resource Paths
Ensure that your directory structure matches the resource paths used in the FXMLLoader. For example, if your FXML files are located in src/main/resources/com/example/clinic/userandmanager/, the FXMLLoader paths in the controller should reflect that:
getClass().getResource("/com/example/clinic/userandmanager/UserWindow.fxml")
Similarly, ensure that the images and other resources are placed correctly relative to the FXML files.
5. Final Thoughts
With these changes:
- Clicking on the User option will open the User Window.
- Clicking on the Manager option will open the Manager Window.
- Clicking the Close button will close the current window.
You can now proceed to implement the specific functionalities within the UserWindowController and ManagerWindowController as needed.
Additional Tips:
-
Styling: Ensure that your CSS file (userAndManager.css) properly styles the new windows if needed.
-
Modality: If you want the new windows to be modal (i.e., block interaction with other windows until closed), set the modality in the controller:
userStage.initModality(Modality.APPLICATION_MODAL);
-
Passing Data: If you need to pass data between controllers or windows, consider using dependency injection or other design patterns suitable for JavaFX.
Feel free to ask if you need further assistance with implementing specific functionalities within the new windows!