Im trying to embed a video in a website to a mediaview of a simple javafx application. Ive got a sample code which I used as my javafx code. It opens the scene and plays the audio but wont play the video. How can I make it play the audio? Im using netbeans IDE 8.0.2,JavaFx 8 and scene builder 2.0
The Code ive tried is below.
Thanx in advanced.
#FXML MediaView mdv;
Media media;
public static MediaPlayer mpl;
#Override
public void initialize(URL url, ResourceBundle rb) {
media=new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
mpl=new MediaPlayer(media);
mpl.setAutoPlay(true);
mdv=new MediaView(mpl);
mpl.play();
mpl.setOnError(new Runnable() {
#Override public void run() {
System.out.println("Current Error: " + mpl.getError());
}
});
}
This is how I load the children to the stage
public class Production extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Vdo.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
the FXML Ive got after creating the GUI from scene builder is below
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.media.*?>
<?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="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="production.VdoController">
<children>
<MediaView fx:id="mdv" fitHeight="300.0" fitWidth="300.0" layoutX="125.0" layoutY="85.0" />
<Button layoutX="235.0" layoutY="51.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
You are re-initializing the MediaView that has been initialized by the FXMLoader. Never do this, because you will loose the reference to the original node.
You should just set the MediaPlayer to the MediaView instead of re-initializing it by using the setMediaPlayer().
#Override
public void initialize(URL url, ResourceBundle rb) {
media=new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
mpl=new MediaPlayer(media);
mpl.setAutoPlay(true);
mdv.setMediaPlayer(mpl);
mpl.play();
}
Related
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);
}
Created custom design using FXML with two files CustomToggleSwitch.fxml and CustomToggleSwitch.java.
CustomToggleSwitch.fxml has following code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<fx:root type="Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" onAction="#click" text="Button" />
</children>
</fx:root>
CustomToggleSwitch.java has code
package com.custom;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
public class CustomToggleSwitch extends Pane{
int tick;
public CustomToggleSwitch() {
FXMLLoader loader=new FXMLLoader(getClass().getResource("CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
public void click(ActionEvent actionEvent){
System.out.println(tick++);
}
}
Created jar file from these and used this jar file inside application project. Application has test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.custom.*?>
<?import com.custom.CustomToggleSwitch?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<CustomToggleSwitch layoutX="29.0" layoutY="48.0" />
</children>
</AnchorPane>
Controller class(TestController.java) has following
public class TestController implements Initializable{
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
#FXML
public void click(){
System.out.println("Test");
}
}
Button is shown successfully on GUI and Button click is also get detected.Button is showing 0,1,2,3..etc on output console. But Test is not getting printed on screen.
How can i detect button press in application controller class? Can someone help me to resolve this issue.
Thanks a lot to all.
add listener property with getter and setter in your component
controller.
The example of Jai is correct, but inconsistent with the other components of javafx. For proper implementation, it is appropriate to use properties that can be manipulated in both FXML and manual mode.
This is the user component controller that has an onMyAction property added. This property is used for event notification.
public class CustomToggleSwitch extends Pane {
private ObjectProperty<EventHandler<ActionEvent>> onMyAction = new SimpleObjectProperty<EventHandler<ActionEvent>>();
public CustomToggleSwitch() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void initialize() {
}
#FXML
private void click(ActionEvent event) {
if(onMyAction.get() != null) {
onMyAction.get().handle(event);
}
}
public EventHandler<ActionEvent> getOnMyAction() {
return onMyAction.get();
}
public ObjectProperty<EventHandler<ActionEvent>> onMyActionProperty() {
return onMyAction;
}
public void setOnMyAction(EventHandler<ActionEvent> onMyAction) {
this.onMyAction.set(onMyAction);
}
}
With such a structured component, the onMyAction property can be added by FXML for example
<AnchorPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" >
<CustomToggleSwitch onMyAction="#testHandler"/>
</AnchorPane>
public class Controller {
#FXML
private void testHandler(ActionEvent event) {
}
}
or manually
<AnchorPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" >
<CustomToggleSwitch fx:id="customToggleSwitch"/>
</AnchorPane>
public class Controller {
#FXML
private CustomToggleSwitch customToggleSwitch;
#FXML
private void initialize() {
customToggleSwitch.setOnMyAction(event -> {
});
}
}
Update
You don't need to use a property. Making the getter and setter available should be sufficient to use it from fxml. (I didn't encountered a case where I would add a listerner to a event handler property yet.)
This is a conversion without the use of Property
public class CustomToggleSwitch extends Pane {
private EventHandler<ActionEvent> myEventHandler;
public CustomToggleSwitch() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void initialize() {
}
#FXML
private void click(ActionEvent event) {
if(myEventHandler != null) {
myEventHandler.handle(event);
}
}
public EventHandler<ActionEvent> getOnMyAction() {
return myEventHandler;
}
public void setOnMyAction(EventHandler<ActionEvent> onMyAction) {
myEventHandler = onMyAction;
}
}
This is happening because onAction="#click" is declared in CustomToggleSwitch.fxml, so FXMLLoader will look for the click() in CustomToggleSwitch.java.
Adding a click() in TestController class is not going to do anything, because you do not have any node with onAction="#click" in test.fxml.
One way for the Button in CustomToggleSwitch to communicate with TestController class is to relay the event out.
public class CustomToggleSwitch extends Pane {
// Same stuff
private List<Runnable> onClickRunnables = new ArrayList<>();
public final void addButtonOnClickRunnable(Runnable runnable) {
Objects.requireNonNull(runnable);
onClickRunnables.add(runnable);
}
#FXML
private void click(ActionEvent actionEvent){
System.out.println(tick++);
if (!onClickRunnables.isEmpty()) {
onClickRunnables.forEach(r -> r.run());
}
}
}
public class TestController implements Initializable {
// Give CustomToggleSwitch control an fx:id in FXML
#FXML private CustomToggleSwitch customSwitch;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
customSwitch.addButtonOnClickRunnable(this::click);
}
}
Another way is to expose the Button in CustomToggleSwitch out. Personally, I would suggest against this because it's neater to keep implementation of a "control" hidden.
I'm making a application where PuzzleBlocks are being constructed in a grid of 5x5 rectangles. I've moved away from declaring fx:controller in the .fxml file and instead did this in main:
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader baseViewLoader = new FXMLLoader(getClass().getResource("Views/BaseView.fxml"));
baseViewLoader.setLocation(getClass().getResource("Views/BaseView.fxml"));
baseViewLoader.setController(new DockViewController());
Parent root = baseViewLoader.load();
FXMLLoader dockLoader = new FXMLLoader(getClass().getResource("Views/DockView.fxml"));
dockLoader.setLocation(getClass().getResource("Views/DockView.fxml"));
dockLoader.setController(new DockViewController());
Parent dockView = dockLoader.load();
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.setTitle("Tiles 2059275");
Scene gameScene = new Scene(root);
primaryStage.setScene(gameScene);
primaryStage.show();
new GameController();
}
Now previously when it was done via having the Controller declared via the .fxml file this piece of code in the Initialize of my DockViewController would work properly:
#FXML
StackPane dockStackPane;
#Override
public void initialize(URL location, ResourceBundle resources) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
tile1 = createSquare(Color.GREY);
tile2 = createSquare(Color.GREY);
tile3 = createSquare(Color.GREY);
tile1.setTranslateX(32 * (i) - 176);
tile1.setTranslateY(32 * (j) - 48);
tile2.setTranslateX(32 * (i) - 16);
tile2.setTranslateY(32 * (j) - 48);
tile3.setTranslateX(32 * (i) + 144);
tile3.setTranslateY(32 * (j) - 48);
dockStackPane.getChildren().add(tile1);
dockStackPane.getChildren().add(tile2);
dockStackPane.getChildren().add(tile3);
puzzleBlock1.add(tile1);
puzzleBlock2.add(tile2);
puzzleBlock3.add(tile3);
}
}
System.out.println("Ini test");
}
The error occurs at dockStackPane.getChildren().add(tile1); because dockStackPane is null. If I remove manually adding the controllers in my Main and return to adding the controllers with fx:controller in the FXML file it works properly. But then I do not have a reference to my controller.
DockView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="128.0" prefWidth="384.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane fx:id="dockStackPane" prefHeight="128.0" prefWidth="384.0" />
</children>
</Pane>
And the DockView is included in the BaseView
Thanks to the comments to my question I was able to work it out the solution is to add a fx:id to the include in my BaseView.fxml:
<fx:include fx:id="includedDockView" source="DockView.fxml" />
Make sure the include element then has a fx:controller set as so:
<Pane fx:id="dockPane" fx:controller="tiles.Controllers.DockViewController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="128.0" prefWidth="384.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
Then in the BaseViewController you can do:
#FXML
private DockViewController includedDockViewController;
public DockViewController getDockViewController() {
return includedDockViewController;
}
And in the main you can pass the BaseView as such:
public void start(Stage primaryStage) throws Exception {
FXMLLoader baseViewLoader = new FXMLLoader(getClass().getResource("Views/BaseView.fxml"));
baseViewLoader.setLocation(getClass().getResource("Views/BaseView.fxml"));
baseViewLoader.setController(new BaseViewController());
Parent root = baseViewLoader.load();
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.setTitle("Tiles 2059275");
Scene gameScene = new Scene(root);
primaryStage.setScene(gameScene);
primaryStage.show();
new GameController(baseViewLoader);
}
This question already has answers here:
JavaFX FXML controller - constructor vs initialize method
(3 answers)
Closed 6 years ago.
This is likely pilot error, but the FXML attribute is not binding to the controller class on fx:id. I've whittled it down to a trivial example, but still "no joy". What am I overlooking?
FXML file...
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="mainFrame" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.BorderPaneCtrl">
<left>
<AnchorPane fx:id="anchorPaneLeft" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
The associated Java code is...
package sample.controller;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
public class BorderPaneCtrl {
#FXML private AnchorPane anchorPaneLeft;
public BorderPaneCtrl() {
/* so, #FXML-annotated variables are accessible, but not
* yet populated
*/
if (anchorPaneLeft == null) {
System.out.println("anchorPaneLeft is null");
}
}
/* this is what was missing...added for "completeness"
*/
#FXML
public void initialize() {
/* anchorPaneLeft has now been populated, so it's now
* usable
*/
if (anchorPaneLeft != null) {
// do cool stuff
}
}
Ego is not an issue here, I'm pretty sure I'm overlooking something simple.
FXML elements are not assigned yet in constuctor, but you can use Initializable interface where elements are already assigned.
public class Controller implements Initializable {
#FXML
AnchorPane anchorPaneLeft;
public Controller() {
System.out.println(anchorPaneLeft); //null
}
#Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println(anchorPaneLeft); //AnchorPane
}
}
I assume that you know that you should create controllers with FXML by using for example: FXMLLoader.load(getClass().getResource("sample.fxml");
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);