I'm creating a signUp page and once the user clicks signUp button I want the scene to switch to my budgetView layout. (Which is an FXML file).
I have tried extending my sign up controller with the Application class, and overrode the start method, but it kept giving me an error. This is the route my teacher tried to get me to try. I have also created another primaryStage in my controller and that worked, but my previous scene didn't close it just created another AnchorPane on top of the existing SignUp Scene. I want to just switch from one FXML view to another, when the event handler is initiated and successful.
Event Handler
try {
SignUpDAO.insertUser(txtFieldEmail.getText(), txtFieldFirst.getText(), txtFieldLast.getText(),
passFieldPassword.getText());
resultArea.setText("User inserted! \n");
// SUPPOSED TO OPEN NEW SCENE THROWS
// Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:
// java.lang.reflect.InvocationTargetException
// start(primaryStage);
Overridden Start Method
// TODO Auto-generated method stub
try {
this.primaryStage = primaryStage;
AnchorPane budgetLayout = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));
// SignUp Layout
Scene scene1 = new Scene(budgetLayout);
primaryStage.setScene(scene1);
primaryStage.show();
Related
I have tried searching for this many times with no luck. I am writing a software where I want the user to input their resolution before moving on to the main UI, as the main UI will change size based on the resolution given. How would I open a popup window without a button event handler and then proceed to the main application?
You can just open the popup window in your start() method:
public class MyApp extends Application {
#Override
public void start(Stage primaryStage) {
// make sure application doesn't exit when we close the dialog window:
Platform.setImplicitExit(false);
Stage popup = new Stage();
TextField resolutionField = new TextField();
// ... populate popup, etc...
popup.setOnHidden(() -> {
int resolution = Integer.parseInt(resolutionField.getText());
// now create main UI...
primaryStage.show();
});
popup.show();
}
}
I am very new to using JavaFX and have having some trouble using JavaFX with FXML. I am creating a program with a "Setup" button that when clicked, opens a new window with the connection (to an Arduino) settings. On the menu is another button ("Connect") that connects to the board. This closes the window. I'm looking to have this change the text of the original "setup" button to "disconnect", however, I don't seem to be able to access the button from the "setup window". Whenever I click "connect" I get the following error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
I read online that this is a wrapper for a null pointer exception. I assume that the "setup" button is null and that's why I can't change it, but I can't work out why.
Here is an excerpt from MainController.java:
#FXML
protected void setUpConnection(ActionEvent e) {
SetupController setupController = new SetupController();
setupController.init(this);
}
The above method gets called when the "setup" button is clicked (set in the file: setupMenu.fxml). This then opens up the separate window. Here is the code in SetupController.java that opens the window:
private void openSetupWindow() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("setupMenu.fxml"));
Parent root1 = (Parent)loader.load();
Stage stage = new Stage();
stage.setTitle("Setup Connection");
stage.setScene(new Scene(root1));
stage.show();
} catch(Exception exc) {
exc.printStackTrace();
}
}
When the connect button is clicked, the following method (in SetupController.java) is called:
private void changeButtonText(ConnectionEventType e) {
Button b = main.getSetupButton();
if(e == ConnectionEventType.CONNECT) {
b.setText("Disconnect");
}
else {
b.setText("Setup Connection...");
}
}
(main is the MainController object that was passed in to setupController.init() )
The above code is where I am getting the error.
Just to clarify, I have 2 separate fxml files, one for the main window and one for the pop up. sample.fxml(the main window) has its controller set to MainController and is set up in Main.java (below):
#Override
public void start(Stage primaryStage) throws Exception{
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
GridPane root = loader.load();
Scene scene = new Scene(root, 1200, 900);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Nest Control");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
Am I trying to access the button incorrectly? Is anyone able to help? Like I said, I don't have much experience with using JavaFX or FXML.
I think the answer you are looking for is to store the controllers for each window you open so you can access the variables within the controllers, however without the rest of your code would be hard to advise you, but heres an example of what i mean:
private SetupController yourController;
#Override
public void start(Stage primaryStage) throws Exception{
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
GridPane root = loader.load();
this.yourController=loader.<SetupController>getController();
Scene scene = new Scene(root, 1200, 900);
}
}
You could then pass the variable yourController to other instances in a Model-view-controller type way and access its methods.
or something like this in your case :
private void changeButtonText(ConnectionEventType e) {
Button b = this.yourController.getButton(); //a method that returns your #FXML button object in your controller
if(e == ConnectionEventType.CONNECT) {
b.setText("Disconnect");
}
else {
b.setText("Setup Connection...");
}
}
Or alternatively have a specific method within the controller that will set the text of the button without having to return the button object.
See the examples here and here
However please note the error you get seems to typically attributed to missing #FXML annotations so maybe make sure in this instance that you have annotated all the variables in any controllers also. See here for more details.
In my javafx application I have a dialog box which shows the results from a previous action. When first displayed the window just shows as white without the contents. After resizing the contents display, and on the subsequent times the dialog is brought up it behaves normally. I'm sure it's something simple, but it's been bugging me for days while I move to work on other parts. My initialize method and Constructor are empty and something tells me this may be the issue.
In MainApp extends Application:
called from another dialog stage controller after calling close.
public void showEDResult(List<String> path) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/EDResultLayout.fxml"));
VBox page = (VBox) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Distance Result");
//dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set reference to stage in controller
//BUG -- when first displayed results don't show up until resize window
EDResultController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setMainApp(this);
// give controller reference to result
controller.setResult(path);
// give controller reference to scene (cursor)
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
In class ResultController:
public void setResult(List<String> result) {
numStepsLabel.setText(Integer.toString(result.size()-2));
//TODO -- work on layout
String str = buildResultString(result);
pathLabel.setText(str);
}
I want to do something ,after the controller's initialize() method done,but before the scene show.Is there any method will be invoked before the scene show?I want to put some code into the method.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
AnchorPane pane = loader.load();
Scene gameScene = new Scene(pane);
//I load a secne above,the I will get the controller,set some properties,then,use the properties to read a file before the secene show.
GameUIController controller = loader.getController();
controller.setGameFileLoacation("game1.txt");//I set a property here.I want to use it to read game file,and load game,set some necessary UI.
primaryStage.setScene(gameScene);//this tow statement will show the scene.
primaryStage.show();
I can't put code into initialize() method,because it will invoked when the fxml file loads(when I not yet to get the controller).So,how can I do?
Thanks verymuch !
One solution that I find
primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWING, new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent window)
{
//Your code
}
});
This event occurs on window just before it is shown.doc link
This is the function which called on clicking save button in save wizard FXML.
#FXML
public void Onsave() {
// Handle Button event.
System.out.println(myButton9.getId());
myButton9.setOnAction((event) - > {
String text1 = textfield.getText();
System.out.println(text1);
System.out.println("Save Clicked....");
pane.setVisible(false);
{
Node node = (Node) event.getSource();
// pane.setVisible(false);
Stage stage = (Stage) node.getScene().getWindow();
stage.close();
System.out.println(flag4);
// Call Create Tree View Node function(args1)
//args1 = Test Suite Name
}
});
On clicking the save button an FXML is closed which leads to a main screen where i have to hide a pane. Now when i click on save there is a null pointer exception. The Pane ID in scene builder is "pane". Is there is way somehow that the main FXML and SAVE dialogue FXML can be linked and the on clicking the save button i can get the refrence of the pane and it does not show null value. I tried to use the flag but after the stage is closed the flag changes to its default value. Also if anyone can suggest do help me in creating the tree view in main FXML with the arguements of text1.