I am having trouble understanding what to do in order to get a string value to another controller. I've searched other questions and cannot apply a solution as some answers seem to be quite confusing to me.
I want a combo box value found in one controller to transfer over to a text field in another controller.
I made attempts to create objects from the controller class to set the text value but it fails.
here is the code in one of the controller classes (the other controller just has the variable names):
String name_val = item.getValue().toString(); //controller1 item value
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("contoller2.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
//set value to a textfield in controller2.fxml
Stage stage_completed = new Stage();
stage_completed.setScene(new Scene(root1));
stage_completed.show();
appreciate any help!
thanks!
Solution:
String name_val = item.getValue().toString(); //controller1 item value
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("contoller2.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
//solution start
CompleteformController myControllerHandle = fxmlLoader.getController();
myControllerHandle.complete_title.setText(name_val);
//solution end
Stage stage_completed = new Stage();
stage_completed.setScene(new Scene(root1));
stage_completed.show();
Related
I got an Application or a screen where I can send E-Mails. I also got a template screen as a whole new window. For e.g. I already write 3 email addresses in the TO TextField. I want them to stay, even if I change the whole stage because when I then go to the TemplatePicker Screen and pick a template, the initialize will be called again in the EmailController and all recipicients in the TextField are gone.
Code E-Mail:
public void buttonPickTemplate(ActionEvent event) throws IOException {
Main.fxmlval.add("/application/controller/MailController/SendMail.fxml");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/controller/TemplateController/TemplateAuswahlFenster.fxml"));
Parent parent = loader.load();
TemplateController templateController = loader.<TemplateController>getController();
templateController.setEmailVariablen(empfaengerField.getText(), ccField.getText(), bccField.getText(), betreffField.getText());
Scene homeScreenScene = new Scene(parent);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(homeScreenScene);
}
As you can see I tried to give the variables with a setter to the next screen and set them then back, but that would be horrible. Normally the method to change a screen always looks like this, if you have to throw no variables:
Parent detailsScreen = FXMLLoader.load(getClass().getResource("/application/controller/DetailsController/Details.fxml"));
Scene detailsScene = new Scene(detailsScreen);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(detailsScene);
So and this methode in the TemplateController calls again the EmailController and FXML:
public void auswaehlen(ActionEvent event)throws IOException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("../MailController/SendMail.fxml"));
Parent homeScreen = loader.load();
MailSender wpc=loader.<MailSender>getController();
wpc.setTemplate(this.settemplate.getDatei());
Scene homeScreenScene = new Scene(homeScreen);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(homeScreenScene);
}
I have a project in school where I have to develop a program where you first can choose whether you want to save/read to/from a SQL DB or to save/read to/from XML.
I've made a GUI where you can choose between both methods.
The GUI closes after the user clicks on one of the buttons and the MainMenu GUI opens.
Now I need to know in the MainMenuController what the user choose.
I found online a Method to call the MainMenuController inside the first controller, with FXMLLoader.getController().
try {
Stage stage = new Stage();
FXMLLoader Loader = new FXMLLoader();
Parent root = Loader.load(getClass().getResource("MainMenu.fxml"));
MainMenuController mc = Loader.getController();
mc.setSave("sql");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
e.printStackTrace();
}
MainMenuController
public class MainMenuController {
private String save = null;
public void setSave(String save) {
this.save=save;
}
public String getSave() {
return save;
}
}
But when i try to access a method in MainMenuController I get a NullPointerException for
mc.setSave("sql")
First ,to understand this issue ,you should make some tricks to detect where is your problem.When you do :
System.out.println(mc);
You will find the result is null.So you can not call setSave("sql") with null object,you got a null controller because your did not specify the location of your file,but you can change some lines to resolve your problem :
try {
Stage stage = new Stage();
FXMLLoader fxm = new FXMLLoader(getClass().getResource("MainMenu.fxml"));
Parent parent = (Parent) fxm.load();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
FirstController mc = fxm.getController();
System.out.println(mc);
mc.setSave("sql");
} catch (Exception e) {
e.printStackTrace();
}
You are calling the static method FXMLLoader.load(URL). Because this is a static method, it has no effect on the state of the FXMLLoader instance you created; specifically the controller field is not set.
Instead, set the location on the FXMLLoader instance, and call the instance method load():
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainMenu.fxml"));
// or just FXMLLoader loader = new FXMLLoader(getClass().getResource("MainMenu.fxml"));
Parent root = loader.load();
Class: DbUserController.cs
btnTamam.setOnAction((ActionEvent event) -> {
......
........
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/View/PerformancePage.fxml"));
AnchorPane frame = fxmlLoader.load();
PerformancePageController c = (PerformancePageController) fxmlLoader.getController();
c.txtUrl.setText("TEST TEST");
});
txtUrl = PerformancePage.fxml TextField
c.txtUrl.setText("TEST TEST");
The code I'm pointing to is not working.
It does not write "TEST TEST" into TextBox.
You are loading a new pane:
AnchorPane frame = fxmlLoader.load();
But you aren't adding that pane to the displayed SceneGraph.
I need to open a fxml window from a javafx controler
Stage graphStage = new Stage();
FXMLLoader loader = new FXMLLoader();
fenetre = new File("../graph/graph.fxml");
if (!fenetre.exists()) {
System.out.println("fichier inexistant");
} else {
Pane rootGraph = loader.load(getClass().getResource(fenetre.getPath()).openStream());
GraphController controller = (GraphController) loader.getController();
controller.getSystemChoice(id);
Scene sceneGraph = new Scene(rootGraph);
sceneGraph.getStylesheets().add(getClass().getResource("myStyle.css").toExternalForm());
graphStage.setTitle("Graphe");
graphStage.setScene(sceneGraph);
graphStage.show();
}
the code doesn't fincd the file, how to reach this file ?
controler is on: cloud/composant/controler.java
my fxml is on: cloud/graph/graph.fxml
Either you get the data from a file or you use a resource.
Do not try to combine both approaches. They are often incompatible.
// assuming here the file path is correct
FXMLLoader loader = new FXMLLoader(fenetre.toURI().toURL());
...
Pane rootGraph = loader.load();
If the file is a resource, you should prefer to load it as resource though:
// assuming cloud is positioned in the "classpath root"
FXMLLoader loader = new FXMLLoader(getClass().getResource("/cloud/graph/graph.fxml"));
I'm creating a new modal dialog from the main controller class. How do I set some textfield values in the dialog before it is displayed?
URL url = getClass().getResource("SeedNodeForm.fxml");
Stage stage = new Stage();
stage.setTitle("Seed Node Information");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.initStyle(StageStyle.UTILITY);
Parent root = FXMLLoader.load(url);
stage.setScene(new Scene(root));
stage.centerOnScreen();
textfield1.setValue("foo!");
textfield2.setValue("foo2");
stage.showAndWait();
Thank you Uluk Biy - your link led me to the answer which is:
// get the controller from the loader
SeedNodeFormController c = (SeedNodeFormController) fxmlLoader.getController();
// call setter in controller routine to set needed values
c.setSeedNode(value);