JavaFX - Need to pre-fill field through initialize but it won't allow exceptions. Any way around this?

心已入冬 提交于 2020-01-16 18:08:55

问题


I'm trying to code a simple log-in screen that will lead to another program.

The user can 1) Login or 2) Sign-up - two different scenes.

When the user signs-up the username/encrypted password is saved in a database.

When the user is logging-in, he has an option to have the program remember his login details for next time by saving them temporarily on his computer in an XML file.

My idea was, to have the program check if an XML file exists on scene load, and if it exists, then pre-fill the fields with the data from the XML file.

I've gotten the XML reader to work (just not in this specific case) and I've gathered that the best way to do this, is by running it through the initialize option, as I understand this is completed before any action is triggered?

Here's my code:

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    File file = new File("C:\\Users\\konta\\IdeaProjects\\project\\files\\rememberme.xml");
    boolean exists = file.exists();
    if (exists) {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(file);
            document.getDocumentElement().normalize();
            savedUsername = document.getElementsByTagName("Username").item(0).getTextContent();
            savedPassword = document.getElementsByTagName("Password").item(0).getTextContent();

        if (savedUsername.length() >= 1 && savedPassword.length() == 44) {
            usernameInput.setText(username);
            passwordInput.setText(password);
        }
    }
}

Problem:

This code needs to throw three exceptions, which aren't allowed in a initialize.

Is there any way around this or another way of reading the XML input and running it in the initialize method that you could lead me to?

Thanks - Kim Andre Langholz


回答1:


As Haroldo_OK's answer says, one way is to surround your code in a try/catch block. But there is another way. You don't have to implement the javafx.fxml.Initializable interface to take advantage of its functionality. From the documentation:

NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.

With this you can change your code to the following:

public class Controller {

    // If you still need access to the URL or ResourceBundle
    @FXML private URL location;
    @FXML private ResourceBundle resources;

    @FXML
    private void initialize() throws Exception { // can now add throws clause
        File file = new File("C:\\Users\\konta\\IdeaProjects\\project\\files\\rememberme.xml");
        boolean exists = file.exists();
        if (exists) {
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(file);
                document.getDocumentElement().normalize();
                savedUsername = document.getElementsByTagName("Username").item(0).getTextContent();
                savedPassword = document.getElementsByTagName("Password").item(0).getTextContent();

            if (savedUsername.length() >= 1 && savedPassword.length() == 44) {
                usernameInput.setText(username);
                passwordInput.setText(password);
            }
        }
    }

}

Note that this setup will propagate any thrown exception out to the caller of FXMLLoader.load; it will be wrapped in an InvocationTargetException. If you can recover from the error inside the initilaize method then you should go with the try/catch block, as suggested by Haroldo_OK.




回答2:


You will have to surround your code with a try/catch block



来源:https://stackoverflow.com/questions/53948592/javafx-need-to-pre-fill-field-through-initialize-but-it-wont-allow-exceptions

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