I'm trying to write my first application with javaFX and FXML files, but I got stuck with accesing variables of the FXMLs.
I am able to start the program with the first scene (first FXML). There is a button I'm able to interact with, and the first scene hides and the second scene appears. In that scene, the user has to insert a value in a text field. I'm able to save this into a variable by confirming this input over another button.
This button click hides the second scene and opens the first scene again. Now I want to set the value of a label in that first scene to the value of the variable I saved the user's input in. But this throws an error message.
It seems like this label value is not accessable, even if this label is part of the currently loaded FXML, but I am able to get the value of a label of the second FXML (user interaction field), which isn't visible anymore.
Can anyone help me, how to make this Label of the first FXML readable and changable?
My code is quite long, but I tired to give you the most important parts:
#FXML
private Label RollsMax;
#FXML
private TextField roll_input;
#FXML
private String amountRolls;
... // several more declarations
#FXML
void btn_confirmInput_onClick(ActionEvent event){ //btn_confirmInput is part of scene2.fxml
event.consume();
amountRolls=roll_input.getText();
System.out.println(amountRolls); // here I get the result of the user's input
try{
root=FXMLLoader.load(getClass().getResource("project/scene1.fxml"));
stage=(Stage)((Node)event.getSource()).getScene().getWindow();
scene=new Scene(root);
stage.setScene(scene);
stage.show();
} //end try
catch (IOException e){
e.printStackTrace();
} //end catch
RollsMax.setText(amountRolls); // THIS LINE IS NOT WORKING AS RollsMax SEEMS NOT AVAILABLE, EVEN IF IT IS PART OF SCENE1.fxml
} // end btn_confirmInput_onClick()
Any ideas? The error I get is 'java.lang.RuntimeException: java.lang.reflect.InvocationtargetException' as well as a'java.lang.NullPointerException'.
Thanks a lot!
Endcoder
To share data between the scenes, we'll introduce a Model class that holds the shared information:
public class Model {
private SimpleStringProperty valueProperty = new SimpleStringProperty("N/A");
public SimpleStringProperty getValueProperty() {
return valueProperty;
}
}
And an interface to be implemented by the controllers of the two scenes (actually the controllers of the two roots of the two scenes).
The interface adds the functionality of injection a Model into the controller:
public interface Controller {
void setModel(Model model);
}
sceneOne.fxml with a button to switch scene and a label to display user's input:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/16"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneOneController">
<children>
<Label layoutX="159.0" layoutY="40.0" text="Scene 1">
<font>
<Font size="24.0" />
</font>
</Label>
<Button layoutX="264.0" layoutY="246.0" mnemonicParsing="false" onAction="#changeScene" prefHeight="26.0" prefWidth="102.0" text="Get Input" />
<Label fx:id="inputValue" layoutX="187.0" layoutY="142.0" text="Label" />
</children>
</AnchorPane>
And its controller which implements the Controller interface:
public class SceneOneController implements Controller {
#FXML Label inputValue;
private Model model;
public void changeScene(ActionEvent e) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneTwo.fxml"));
Parent root = loader.load();
Controller controller = loader.getController(); //get a reference to sceneTwo controller
controller.setModel(model);
Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public void setModel(Model model) {
this.model = model;
if(model != null){
inputValue.textProperty().unbind();
inputValue.textProperty().bind(model.valueProperty);
}
}
}
sceneTwo.fxml with a button to switch scene and a TextField for the user's input:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneTwoController">
<children>
<Label layoutX="150.0" layoutY="41.0" text="Scene 2">
<font>
<Font size="24.0" />
</font>
</Label>
<Button layoutX="264.0" layoutY="246.0" mnemonicParsing="false" onAction="#changeScene" prefHeight="26.0" prefWidth="102.0" text="Update" />
<TextField fx:id="inputValue" layoutX="121.0" layoutY="138.0" prefHeight="25.0" prefWidth="162.0" />
</children>
</AnchorPane>
And its controller:
public class SceneTwoController implements Controller {
#FXML TextField inputValue;
private Model model;
public void changeScene(ActionEvent e) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneOne.fxml"));
Parent root = loader.load();
Controller controller = loader.getController(); //get a reference to sceneOne controller
controller.setModel(model);
Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public void setModel(Model model) {
this.model = model;
if(model != null){
inputValue.textProperty().unbind();
model.valueProperty.bind(inputValue.textProperty());
}
}
}
And finally the application to test it all:
public class SwitchSceneMVC extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneOne.fxml"));
Parent root = loader.load();
Model model = new Model();
Controller controller = loader.getController();
controller.setModel(model);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(null);
}
}
Use a controller class for every FXML. Then you should be able to interact between them with public methods.
Especially when just hidinng the first scene you should then be able to just write from one scene to the other one.
Alternatively: while starting the second scene make a parameter where you bring the instance of the first scene. Than you should be able to use the needed methods on the first scenes instance.
Like
Code Scene one
#FXML
private void startSceneTwoButton(){
new SceneTwo(this);
}
Code Scene two
FirstScene scene;
public SecondScene(FirstScene scene){
this.scene = scene;
//some code
}
#FXML
private void button(){
scene.enterValue(value);
}
Related
Somehow, I cannot add items to my ComboBox variable from another class as it always says either the value of variable is null or the return value of its getter is null.
I use Scene Builder to build Sample.fxml, with controller as Controller class, luanch the whole thing form UserInterface class
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<center>
<ComboBox fx:id="myBox" prefWidth="150.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Controller
public class Controller {
#FXML
private ComboBox<String> myBox;
public ComboBox<String> getMyBox() {
return myBox;
}
public void initialize() {
ObservableList<String> defaultTicker = FXCollections.observableArrayList("Something");
myBox.getItems().addAll(defaultTicker);
}
}
UserInterface
public class UserInterface extends Application {
#Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = loader.load();
Controller controller = new Controller();
primaryStage.setTitle("Title");
primaryStage.setScene(new Scene(root));
primaryStage.show();
List<String> addStuff = new ArrayList<String>();
addStuff.add("a");
addStuff.add("b");
addStuff.add("c");
controller.getMyBox().getItems().addAll(addStuff);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
After running, the interface appears, with only the option "Something" in it. So I guess it initialize fine?
The interface after launch
What did I do wrong? I couldn't seem to find any solutions so far.
Thanks in advance.
I am working on a project involves RFID technology, what i am trying to do at the moment, once the RFIDTagListener method runs, I basically pass a tag to a reader which its serial number will be sent to a server to get some relevant data back and pop them on a GUI screen. what I have done so far is getting the data upon sending reader's data manually without passing a tag becasue I don't know how to do it otherwise and here is the problem. where I am working on a javafx project and when I tried to put the RFIDTagListener method within the MainController class and on compiling, the taglistner method wouldn't be triggered and just ignored, it's only the GUI screen will be opened.However, I also tried to have RFIDTagListener within the main class but on compiling, the taglistner method would be run first and when it's finished in 5 seconds, my GUI window will be opened next. SO I Don't know where this method should be located exactly. Essentially What I want is to have them both running at the same time, the taglistener running in the background with the GUI window opened simulitniously.
Any recommendation guys would be much appricated.
MainController class :
public class MainController {
RFID rfid = new RFID();
String ReaderNo = null;
String walletJson = new String();
Gson gson = new Gson();
public static String sensorServerURL = "http://localhost:8080/PhidgetServer2019/SensorServerRFIDdata";
walletDAO dao = new walletDAO();
ArrayList<wallet> allwallets = new ArrayList<wallet>();
#FXML VBox ConsultHR;
#FXML private Label message;
#FXML private Label WalletName;
#FXML private ListView<ArrayList<wallet>> list;
#FXML private ListView<ArrayList<wallet>> RoomAList;
#FXML private TableView<wallet> tableViewData;
#FXML private TableColumn<wallet, String> NameColumn;
#FXML private TableColumn<wallet, String> LocationColumn;
#FXML private TableColumn<wallet, String> TagColumn;
public void getTags(ActionEvent event) throws SQLException {
allwallets = dao.getWalletTag();
try {
allwallets = dao.getWalletTag();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(allwallets);
list.getItems().add(allwallets);
}
public MainController() throws PhidgetException {
// Make the RFID Phidget able to detect loss or gain of an rfid card
rfid.addTagListener(new RFIDTagListener() {
// What to do when a tag is found
public void onTag(RFIDTagEvent e) {
try {
ReaderNo = String.valueOf(rfid.getDeviceSerialNumber());
} catch (PhidgetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Reader serial number is " + ' '+ReaderNo);
wallet walletData = new wallet("385055");
walletJson = gson.toJson(walletData);
String resultfromserver = sendToServer(walletJson);
System.out.println("DEBUG: data in json : " +resultfromserver);
wallet walletObject = gson.fromJson(resultfromserver, wallet.class);
System.out.println("DEBUG: The wallet's Data: "+' '+ walletObject);
WalletName.setText(walletObject.getWalletName());
}
});
rfid.addTagLostListener(new RFIDTagLostListener() {
// What to do when a tag is lost
public void onTagLost(RFIDTagLostEvent e) {
// optional print, used as debug here
System.out.println("DEBUG: Tag lost: " + e.getTag());
}
});
}
}
Main class :
public class Main extends Application {
//RFID rfid = new RFID();
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Wallet locator !");
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
public static void main(String[] args) throws PhidgetException {
new MainController();
launch(args);
}
}
Main.FXML :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<Button layoutX="149.0" layoutY="251.0" mnemonicParsing="false" onAction="#getTags" prefHeight="46.0" prefWidth="82.0" text="tags" />
<Label fx:id="message" layoutX="139.0" layoutY="209.0" prefHeight="35.0" prefWidth="101.0" />
<ListView id="studentObservableList" fx:id="list" layoutY="209.0" prefHeight="131.0" prefWidth="139.0" />
<TableView fx:id="tableViewData" prefHeight="200.0" prefWidth="231.0" style="-fx-border-color: red;">
<columns>
<TableColumn fx:id="NameColumn" prefWidth="75.0" text="Name"/>
<TableColumn fx:id="LocationColumn" prefWidth="75.0" text="Location" />
<TableColumn fx:id="TagColumn" prefWidth="75.0" text="Tag" />
</columns>
</TableView>
<Button layoutX="251.0" layoutY="14.0" mnemonicParsing="false" onAction="#getWallets" prefHeight="56.0" prefWidth="82.0" text="wallets" />
</AnchorPane>
1st point
The main method is inside the MainController class and it's instantiating itself, which is possible but not conventional.
2nd point
Never put your main method into a controller! Put it into your Main class instead.
3rd point
You must define the application launch inside your main method, by calling Application.launch(). This method will call your overriden start method (among other things) and display the GUI:
public static void main(String[] args) throws PhidgetException {
launch(args);
new MainController();
}
4th point
You don't need to instantiate directly a controller in JavaFX. With your current code you're creating a MainController instance which is unlinked to your GUI; that's why you can see your GUI without interacting with it. You need to remove the new MainController(); line into your main method, then check if the fx:controller attribute is defined in the root of your FXML file.
You can call this controller instance using root.getController() in your start method.
I am trying to update the center node of a BorderPane using FXML. I have created a VBox on the left with Buttons and when clicked they are supposed to update the center node however the center node does not update. Can someone shed some light on what I am doing incorrectly?
I know the buttons events work (they print to the screen usiong println) but it will not update the UI.
The root nodes .fxml (the BorderPane):
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:id="rootPane">
<left>
</left>
<center>
</center>
</BorderPane>
The menu
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox fx:controller="" xmlns:fx="http://javafx.com/fxml" >
<children>
<Button text="Dashboard" fx:id="dashboardButton" onAction="#dashboardButtonEvent" />
<Button text="Sales" fx:id="salesButton" onAction="#salesButtonEvent" />
</children>
</VBox>
The MenuController
public class MenuController implements Initializable {
#FXML
private BorderPane rootPane;
#FXML
private Button dashboardButton; //same name as in the menu.fxml file
#FXML
private Button salesButton; //same name as in the menu.fxml file
#FXML
private void dashboardButtonEvent(ActionEvent event) {
try {
VBox dashboard = FXMLLoader.load(getClass().getResource("../../../fxml/dashboard.fxml"));
rootPane = FXMLLoader.load(getClass().getResource("../../../fxml/root.fxml"));
rootPane.setCenter(dashboard);
} catch (IOException e) { System.out.println(e); }
}
#FXML
private void salesButtonEvent(ActionEvent event) {
try {
VBox sales = FXMLLoader.load(getClass().getResource("../../../fxml/sales.fxml"));
rootPane = FXMLLoader.load(getClass().getResource("../../../fxml/root.fxml"));
rootPane.setCenter(sales);
} catch (IOException e) { System.out.println(e); }
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
I assume where I am going wrong is the way I am going about assigning the root pane using rootPane = FXMLLoader.load(getClass().getResource("../../../fxml/root.fxml"));. If thats the case, how do I get the current rootPane from the existing scene?
How i can do button action for editing TableView. I need to put text from TextArea to table when i touch button. And if put System.out.println in inputToTable() it is work.
public class InputController {
public TextArea inputArea;
public Button inputButton;
private TableController tableController;
public void initialize() {
tableControllerInit();
}
public void inputToTable() {
if(inputArea.getText() != "") {
tableController.tableInfo.setItems(FXCollections.observableArrayList(new InputObject(inputArea.getText())));
}
}
private void tableControllerInit() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("table.fxml"));
fxmlLoader.load();
tableController = fxmlLoader.getController();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TableController {
#FXML TableView<InputObject> tableInfo;
#FXML TableColumn<InputObject, String> col1;
public void initialize() {
col1.setCellValueFactory(new PropertyValueFactory<>("text"));
}
}
public class Controller implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
public class InputObject {
String text;
public InputObject(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<left>
<fx:include source="table.fxml"/>
</left>
<center>
<fx:include source="input.fxml"/>
</center>
</BorderPane>
<TableView fx:controller="sample.TableController" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:id="tableInfo" prefHeight="400.0" prefWidth="330.0">
<columns>
<TableColumn fx:id="col1" prefWidth="75.0" text="Output" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<VBox fx:controller="sample.InputController" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<TextArea fx:id="inputArea" prefHeight="188.0" prefWidth="270.0" />
<Button fx:id="inputButton" onAction="#inputToTable" mnemonicParsing="false" text="Input">
<VBox.margin>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</VBox.margin>
</Button>
</children>
</VBox>
You load table.fxml twice: once via the fx:include in the main FXML file, and once in InputController, via the FXMLLoader you create in the tableControllerInit() method. Consequently, two instances of TableController are created, one associated with the first UI you load from table.fxml, and one associated with the second UI you load from table.fxml.
The UI you load via the fx:include is displayed in the VBox defined in the main FXML file. The UI you load with the FXMLLoader is never displayed (in fact, you never even keep a reference to it, you just call loader.load() and discard the result). When you try to update the table's items (do you really intend to replace all the existing items, by the way?), you refer to the second controller instance, which is associated with the UI which is never displayed. Consequently, you are updating a table that is not displayed, and you never see any results.
What you really need to do is share the same data between the two controllers associated with the two fx:includes. You can do this simply by injecting those two controllers into the main controller, as described in the "Nested Controllers" section in the documentation.
First, give the fx:include elements fx:id attributes:
<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<left>
<fx:include fx:id="table" source="table.fxml"/>
</left>
<center>
<fx:include fx:id="input" source="input.fxml"/>
</center>
</BorderPane>
Then you can inject the controllers into the main controller by creating fields with the word "Controller" appended to the fx:id. Create a single observable list, which will represent the list of items displayed in the table, and pass it to each controller:
public class Controller implements Initializable {
#FXML
private TableController tableController ;
#FXML
private InputController inputController ;
#Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<InputObject> items = FXCollections.observableArrayList();
tableController.setTableItems(items);
inputController.setTableItems(items);
}
}
Finally, just define the obvious methods in each of the other two controllers:
public class TableController {
#FXML
private TableView<InputObject> tableInfo;
#FXML
private TableColumn<InputObject, String> col1;
public void initialize() {
col1.setCellValueFactory(new PropertyValueFactory<>("text"));
}
public void setTableItems(ObservableList<InputObject> tableItems) {
tableInfo.setItems(tableItems);
}
}
Now the table is displaying the contents of the items list created in the main controller's initalize() method, and the InputController has a reference to the same list. So all you need to do is update that list in the InputController. I assume you just want to add items to the table (not replace them all):
public class InputController {
#FXML
private TextArea inputArea;
#FXML
private Button inputButton;
private ObservableList<InputObject> tableItems ;
public void setTableItems(ObservableList<InputObject> tableItems) {
this.tableItems = tableItems ;
}
public void inputToTable() {
if(! inputArea.getText().isEmpty()) {
tableItems.add(new InputObject(inputArea.getText()));
}
}
}
More generally, if you have more data to share among the different controllers, you would create one or more "model" classes and share a model instance with the controllers. Then you can observe the properties of the model and update them. See Applying MVC With JavaFx for a more comprehensive example.
In my Java Fx application I create a two stages. The first stage is the default in the main controller class, HomeController. The second, AddNewEmailController, is created by calling a method, showNewComposeNewEmail(), in the AddNewEmailController class.
The new stage gets created fine, but none of the methods I try to call, like closing the AddNewEmailController stage, will work on the AddNewEmailController stage.
How can I get the methods to work?
I'd also like to have the HomeController stage inaccessible once the AddNewEmailController stage gets open, like how pop-up windows do.
I can't even get the AddNewEmailController stage to iconify?
Thank you all in advance.
The HomeController class:
public class HomeController implements Initializable {
// Initializes the controller class.
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
public void windowClose() {
Platform.exit();
}
#FXML
public void showNewComposeNewEmail() throws Exception {
new AddNewEmailController().newnewcomposeNewEmailStage();
}
}
The AddNewEmailController class:
public class AddNewEmailController implements Initializable {
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
// Initializes the controller class.
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public Stage newComposeNewEmail;
public void newnewcomposeNewEmailStage() throws IOException {
newComposeNewEmail = new Stage();
newComposeNewEmail.initStyle(StageStyle.UNDECORATED);
newComposeNewEmail.initStyle(StageStyle.TRANSPARENT);
Parent newComposeNewEmailRoot = FXMLLoader.load(getClass().getResource("/wakiliproject/Forms/AddNew/NewEmail/NewEmail.fxml"));
StageDraggable.stageDraggable(newComposeNewEmailRoot, newComposeNewEmail);
Scene newComposeNewEmailScene = new Scene(newComposeNewEmailRoot, 590, 670);
newComposeNewEmail.setScene(newComposeNewEmailScene);
newComposeNewEmail.show();
}
#FXML
private void newComposeNewEmailClose() {
newComposeNewEmail.close();
}
#FXML
private void newComposeNewEmailIconify() {
newComposeNewEmail.setIconified(true);
}
}
UPDATE:
The NewEmail FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="wakiliproject.Forms.AddNew.NewEmail.TryEMailController">
<children>
<Label layoutX="190.0" layoutY="61.0" onMouseClicked="#newComposeNewEmailStageIconify" text="Minimise Window" />
<Label layoutX="300.0" layoutY="61.0" onMouseClicked="#newComposeNewEmailStageClose" text="Close Window" />
</children>
</AnchorPane>
In your fxml, your controller is
fx:controller="wakiliproject.Forms.AddNew.NewEmail.TryEMailController"
while in your code, the class name is
AddNewEmailController
Please make them same and recheck, your method will be called.
For making the new Stage as child of the Parent, please use
newStage.initModality(Modality.WINDOW_MODAL);
newStage.initOwner(PrimaryStage);