来自另一个Controller的Java setText,但UI不刷新

时间:2015-02-28 17:32:39

标签: java javafx

我有2个控制器。我想从我的GlobalOptionsFileNameController设置tf_fileName的值。该值已设置,我可以使用它,但TextField不更新视图(我没有看到新的值)。

这是我的代码。 Sry它可能是重复的,但我没有在其他帖子找到适合我的工作方式。

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);
        }
    }    

}

我要设置和刷新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);
    }
}

1 个答案:

答案 0 :(得分:1)

您在getController()中检索的控制器与从未显示的UI元素(root方法在本地声明的getController()方法)相关联。因此,在该控制器实例上调用方法不会在UI中产生任何可见效果。我假设您正在将GlobalOptions.fxml文件加载到其他位置并显示其内容。您需要引用从该加载器获得的控制器(而不是您丢弃其加载内容的任何加载器)。

由于fileNameOptions(...)中的GlobalOptionsController方法中的代码已经引用了GlobalOptionsFileNameController,因此您应该只使用该关系来传输信息。

StringProperty中创建只读GlobalOptionsFileNameController,并使用setFileName(...)方法进行设置:

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);
        }   
    }

    // ...
}

您还可以从GlobalOptionsController中删除getController()引用和GlobalOptionsFileNameController方法。

现在在GlobalOptionsController中只需观察属性并在文本字段更改时更新:

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);
            }
    }

    // ...

}