JavaFX-需要通过初始化预先填充字段,但不允许例外.可以解决吗? [英] JavaFX - Need to pre-fill field through initialize but it won't allow exceptions. Any way around this?

查看:51
本文介绍了JavaFX-需要通过初始化预先填充字段,但不允许例外.可以解决吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的登录屏幕,该屏幕将导致另一个程序.

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

用户可以1)登录或2)注册-两个不同的场景.

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.

用户登录时,可以选择将程序暂时保存在计算机上的XML文件中,以使程序下次记住其登录详细信息.

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.

我的想法是,让程序检查场景加载时是否存在XML文件,如果存在,则用XML文件中的数据预填充字段.

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.

我已经使XML阅读器工作了(只是在这种情况下不是),并且我已经收集到做到这一点的最佳方法是通过initialize选项运行它,因为我知道这是在任何情况下完成的.动作被触发了吗?

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?

@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.

是否有其他方法可以读取XML输入并以您可能会导致我执行的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?

谢谢 -Kim Andre Langholz

Thanks - Kim Andre Langholz

推荐答案

Haroldo_OK的回答说,一种方法是将您的代码放在try/catch块中.但是还有另一种方式.您不必实现

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:

注意该接口已通过将locationresources属性自动注入到控制器中而被取代. FXMLLoader 现在将自动调用任何适当注释的no-控制器定义的arg initialize()方法.建议尽可能使用注射方法.

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

}

请注意,此设置会将所有抛出的异常传播到FXMLLoader.load的调用方;它将被包装在InvocationTargetException中.如果您可以从initilaize方法中的错误中恢复,则应按照Haroldo_OK的建议使用try/catch块.

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.

这篇关于JavaFX-需要通过初始化预先填充字段,但不允许例外.可以解决吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆