javafx2.0 - force refresh the scene

自闭症网瘾萝莉.ら 提交于 2019-12-10 11:25:22

问题


In my app, one scene having the popup dialog which consists of some fields and buttons. If you click on the button then I want to dismiss the popup dialog as well as update the some fields in the scene. Indirectly I want to refresh scene. is it possible?

I used the following code.Here what I did is, I get the controller of that scene and then update the field using id. but it doesn't work.

 URL location = AdmincontrolController.class.getResource("admincontrol.fxml");

 FXMLLoader fxmlLoader = new FXMLLoader();
 fxmlLoader.setLocation(location);
 fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
 try {
     Parent root = (Parent) fxmlLoader.load(location.openStream());
     AdmincontrolController controller = fxmlLoader.getController();
     System.out.println("AdmincontrolController: "+controller);
     controller.setEmail(item.getEmail());
 } catch (IOException ex) {
     Logger.getLogger(Add_loginController.class.getName()).log(Level.SEVERE, null, ex);
 }

Scenario:

scene

Popup - If we clicks on the add then we need to dismiss that dialog and change the email text on the previous scene.


回答1:


As Alexander mentioned above, updating the underlying text property of the object you are using to display the email should Just Work. You need to make sure that you are working with the property (see Oracle Java FX Property Tutorial for more info). As a concrete example:

FXML

 <Text fx:id="email" />
 <TextField fx:id="emailInput" />
 <Button onAction="#doSetEmail" text="Set Email"/>

In your controller, use the @FXML annotation to inject concrete instances of objects and set the handler to adjust the text:

Controller

@FXML
Text email;

@FXML
TextField emailInput;

@FXML
public void doSetEmail(ActionEvent ae) {
    email.setText(emailInput.getText());
}

Alternatively, you could just bind the email text property to the email label property so that changes would automatically get propagated:

email.textProperty().bind(emailInput.textProperty());

You could do this in your controller initialize() method.

Now, the caveat to all this working depends on how you are handling the event and what you are doing in this. You still haven't posted the code for that as requested by the first answer, so you may be having issues there. Namely, if you are starting threads and then trying to update UI elements on the JavaFX thread from a worker thread, then you can get into trouble (potentially) with things not updating. This depends substantially on the structure of your objects, and you have not given enough information to comment in any meaningful way on that.

  • chooks



回答2:


Everytime you have the Feeling that you manually want to update a Scene you should probably use a backgroundWorker Thread to do the work. This way your UI Thread can use the time to update Labels etc.




回答3:


JavaFX is built so that you don't need to directly call the scene update routine. All you need - update properties of scene components, and they will be updated on the nearest pulse.

So, all you need is to update properties. Or, is there any real trouble?




回答4:


refreshing scene its not possible without closing...but if you can do class level declaration for control..i.e make them static its may work...

try this..

make a function in main file.

MainPanel.java

public static void SetMail(String email)
{
txtmail.setText(email);
}

LoginPanel.java

 btnclear.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {                      
            MainPanel.SetMail(txtEmail.getText());


    }          
    });


来源:https://stackoverflow.com/questions/19153036/javafx2-0-force-refresh-the-scene

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!