Reputation: 371
i have 2 controllers. I want to set the value of tf_fileName from my GlobalOptionsFileNameController. The value is set and i can work with it, but the TextField dont Update the View (i dont see the new value).
Here is my Code. Sry it could be a duplicate, but i dont found a working way for me at the other posts.
package configHelp;
import burninbuilder.GlobalOptionsController;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author Sandro
*/
public class GlobalOptionsFileNameController implements Initializable {
@FXML Button btn_help;
@FXML RadioButton rb_local;
@FXML RadioButton rb_server;
@FXML TextField txt_filename;
@FXML TextField txt_serverName;
@FXML TextField txt_path;
@FXML Button btn_addNr;
@FXML Button btn_addDriveSN;
@FXML Button btn_ok;
Stage test = new Stage();
GlobalOptionsController optionsController;
public void getController() throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/burninbuilder/GlobalOptions.fxml"));
Parent root = (Parent) loader.load();
optionsController = loader.getController();
System.out.println(this.optionsController);
}
public void setFileName(ActionEvent ev){
if(rb_local.isSelected()){
optionsController.setNameLocal(txt_filename.getText());
System.out.println(optionsController.tf_fileName.getText());
}else if(rb_server.isSelected()){
String fileNameServer="//"+txt_serverName.getText()+"/"+txt_path.getText();
optionsController.setNameLocal(fileNameServer);
}
}
public void test(){
txt_filename.setText("test");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
btn_ok.setOnAction(this::setFileName);
try {
getController();
} catch (IOException ex) {
Logger.getLogger(GlobalOptionsFileNameController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Class where i want to set and refresh the TextField.
package burninbuilder;
import configHelp.GlobalOptionsFileNameController;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import logic.GlobalOptions;
public class GlobalOptionsController implements Initializable {
//
GlobalOptions globalOptionsSingleton = GlobalOptions.getInstance();
Stage fileNameOptions = new Stage();
@FXML public TextField tf_fileName;
}
}
GlobalOptionsFileNameController fileAssistentController;
public void fileNameOptions(ActionEvent ev){
try {
if(fileNameOptions.getScene()==null){
FXMLLoader loader = new FXMLLoader(getClass().getResource("/configHelp/globalOptionsFileName.fxml"));
Parent root = (Parent) loader.load();
fileAssistentController = loader.getController();
Scene scene = new Scene(root);
fileNameOptions.setTitle("Assistent zum festlegen der Speicherorte und Dateinamen");
fileNameOptions.getIcons().add(new javafx.scene.image.Image("pictures/app-icon.png"));
fileNameOptions.setScene(scene);
fileNameOptions.setResizable(false);
fileNameOptions.setAlwaysOnTop(true);
fileNameOptions.initModality(Modality.APPLICATION_MODAL);
fileNameOptions.show();
}else{
fileNameOptions.show();
}
} catch (IOException ex) {
System.out.println("AddWindow: "+ex);
}
}
@FXML
public void setFileName(String name){
Platform.runLater(new Runnable() {
@Override
public void run() {
tf_fileName.setText("test");
System.out.println("TextField:"+tf_fileName.getText());
}
});
}
@FXML
public void setNameLocal(String name){
Platform.runLater(new Runnable() {
public void run() {
tf_fileName.setText(null);
tf_fileName.setText(name);
}
});
}
@Override
public void initialize(URL url, ResourceBundle rb) {
filename_options.setOnAction(this::fileNameOptions);
tabpane.setTabMinWidth(94);
btn_ok.setOnAction(this::getGlobalOptions);
cb_html.setOnMousePressed(this::setFileEx);
tf_fileName.setOnMouseExited(this::addExTextField);
tf_hour.setOnKeyTyped(this::onlyNumber);
tf_min.setOnKeyTyped(this::onlyNumber);
tf_duration.setOnKeyTyped(this::onlyNumber);
tf_fail.setOnKeyTyped(this::onlyNumber);
}
}
Upvotes: 1
Views: 1077
Reputation: 209340
The controller you retrieve in getController()
is associated with a UI element (root
, declared locally in the getController()
method) that is never displayed. So calling methods on that controller instance cannot have any visible effect in your UI. I assume you are loading the GlobalOptions.fxml
file elsewhere and displaying its contents. You need a reference to the controller you get from that loader (not some arbitrary loader whose loaded contents you discard).
Since the code in your fileNameOptions(...)
method in GlobalOptionsController
already has a reference to the GlobalOptionsFileNameController
, you should probably just use that relationship to transfer the information.
Create a read only StringProperty
in the GlobalOptionsFileNameController
, and set it from the setFileName(...)
method:
public class GlobalOptionsFileNameController {
// ...
private ReadOnlyStringWrapper filename = new ReadOnlyStringWrapper();
// ...
public String getFilename() {
return filename.get() ;
}
public ReadOnlyStringProperty filenameProperty() {
return filename.getReadOnlyProperty();
}
public void setFileName(ActionEvent ev){
if(rb_local.isSelected()){
filename.set(txt_filename.getText());
}else if(rb_server.isSelected()){
String fileNameServer="//"+txt_serverName.getText()+"/"+txt_path.getText();
filename.set(fileNameServer);
}
}
// ...
}
You can also get rid of the GlobalOptionsController
reference and the getController()
method from GlobalOptionsFileNameController
.
Now in GlobalOptionsController
just observe the property and update the text field when it changes:
public class GlobalOptionsController {
// ...
public void fileNameOptions(ActionEvent ev){
try {
if(fileNameOptions.getScene()==null){
FXMLLoader loader = new FXMLLoader(getClass().getResource("/configHelp/globalOptionsFileName.fxml"));
Parent root = (Parent) loader.load();
GlobalOptionsFileNameController fileAssistentController = loader.getController();
fileAssistentController.filenameProperty().addListener((obs, oldFilename, newFilename) ->
tf_fileName.setText(newFilename));
Scene scene = new Scene(root);
fileNameOptions.setTitle("Assistent zum festlegen der Speicherorte und Dateinamen");
fileNameOptions.getIcons().add(new javafx.scene.image.Image("pictures/app-icon.png"));
fileNameOptions.setScene(scene);
fileNameOptions.setResizable(false);
fileNameOptions.setAlwaysOnTop(true);
fileNameOptions.initModality(Modality.APPLICATION_MODAL);
fileNameOptions.show();
}else{
fileNameOptions.show();
}
} catch (IOException ex) {
System.out.println("AddWindow: "+ex);
}
}
// ...
}
Upvotes: 1