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.
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);
}
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 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?
My program is refusing to run because the "anchorpane" is refusing to support my controller on all my fxml files, hence my fxml page is not loading. Also, it is also giving me error message that my main class could not be found. I would welcome any help or suggestion on the stated.
My fxml file
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" pickOnBounds="false" prefHeight="614.0" prefWidth="1015.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="winnaccs.WinSecHomeController">
The Controller
public class WinSecHomeController implements Initializable, ControlledScreen {
ScreensController myController;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setScreenParent (ScreensController screenParent){
myController = screenParent;
}
#FXML
private void goToLogin(ActionEvent event) {
myController.setScreen(Winnaccs.screen2);
}
}
just try to change this one
fx:controller="winnaccs.WinSecHomeController"
with this other
fx:controller="winnaccs/WinSecHomeController"
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);