简体   繁体   中英

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

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

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.

您将必须在代码周围加上try / catch块

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM