Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布尔变量有不同的值,然后查询填充它?_Java_User Interface_Boolean_Vaadin - Fatal编程技术网

Java 布尔变量有不同的值,然后查询填充它?

Java 布尔变量有不同的值,然后查询填充它?,java,user-interface,boolean,vaadin,Java,User Interface,Boolean,Vaadin,好了,伙计们,我简直说不出话来。我正在用vaadin框架构建一个GUI,但这对问题不重要。这是我的密码: private final boolean readOnly = GenericAdapterFrontendApplication .getInstance().getUserRole().equals(UserRole.READ); public InstitutionDetailMasterData(Institution i) { super(i); }

好了,伙计们,我简直说不出话来。我正在用vaadin框架构建一个GUI,但这对问题不重要。这是我的密码:

private final boolean readOnly = GenericAdapterFrontendApplication
        .getInstance().getUserRole().equals(UserRole.READ);

public InstitutionDetailMasterData(Institution i) {
    super(i);
}

protected void createSectionContent(Institution i, Panel p) {
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata." + PROP_NAME),
            i.getName()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_DEUBA_ID), i.getDeubaId()));
    p.addComponent(createRow(ViewUtils
            .getMessage("view.institut.masterdata." + PROP_VARIANT), i
            .getVariant().toString()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_FIELD_SEP), i.getFieldSeparator()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_RECORD_SEP), i.getRecordSeparator()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_DATE_FORMAT), i.getDateFormat()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_DECIMAL_SEP), i.getDecimalSeparator()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_THOUSAND_SEP), i.getThousandSeparator()));
    p.addComponent(createRow(ViewUtils
            .getMessage("view.institut.masterdata." + PROP_QUOTING), i
            .getQuoting()));
    p.addComponent(createRow(
            ViewUtils.getMessage("view.institut.masterdata."
                    + PROP_FREQUENCY), i.getFrequency()));
    System.out.println(GenericAdapterFrontendApplication
            .getInstance().getUserRole().equals(UserRole.READ));
    System.out.println(this.readOnly);
    editButton.setVisible(!this.readOnly);
现在重要的部分是开头的只读声明和结尾的打印,因为您的打印:

true
false
这怎么可能呢?我的意思是,我可以使用直接查询来设置按钮的可见性,但这些值对我来说似乎不可能,这让我感到困扰。如果有人有解释的想法,请让我知道。
谢谢

我的第一个猜测是执行命令。readOnly在较早时分配,之后状态可能已更改。

在实例化对象时分配class属性
readOnly

如果这是应用程序初始化的一部分(在用户登录之前),则在调用
createSectionContent
时,它可能具有不同的值

假设访问用户状态的成本不高,我建议您不要将其存储为属性,而是创建一个按需调用该方法的
isReadOnly()
方法,例如

protected boolean isReadOnly(){
    return GenericAdapterFrontendApplication.getInstance().getUserRole().equals(UserRole.READ);
}

如果成本很高,请对其进行初始化。

该值是否会在运行时更改?成员变量声明将在实例化类时赋值,而println语句中的值将在每次调用该方法时求值。根据此框架中的对象生命周期,可能在正确设置GenericadAdapterFrontEndApplication以供使用之前实例化了类。是的,我想就是这样。所以我要像你说的那样将其改为isReadOnly()-方法,或者将UserRole作为参数传递,我会看到的。谢谢,eyerybody!