gpt4 book ai didi

java - 除非 ObjectProperty 的源发生更改,否则不会触发 ChangeListener

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:16 26 4
gpt4 key购买 nike

使用JavaFX8,如果只有 ObjectProperty 的一个属性,如何能够收到通知?发生变化,但引用保持不变?

下面是使用 ObjectProperty<KeyStore> 的具体示例。 java.security.KeyStore保存 key 和证书的列表。如果添加、更改或删除条目,我希望收到通知。

public abstract class EntityController {
protected ObjectProperty<KeyStore> trustStore = new SimpleObjectProperty<KeyStore>();

public EntityController() {

}

public KeyStore getTrustStore() {
return this.trustStore.getValue();
}

public void setTrustStore(KeyStore trustStore) {
this.trustStore.set(trustStore);
}

public ObjectProperty<KeyStore> trustStoreProperty() {
return this.trustStore;
}


}
public class CertificatesTabPresenter extends PKIPresenter implements Initializable {

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.getEntityController().trustStoreProperty().addListener((observableVal, oldTrustStore, newTrustStore) -> {
trustStoreChanged(oldTrustStore, newTrustStore);
});
}


@FXML
private void addTrustStore(MouseEvent e) {
File keyStoreFile = selectKeyStoreFile();
if (keyStoreFile != null) {
Optional<String> keyStoreType = selectKeyStoreType();
if(keyStoreType.isPresent()) {
Optional<char[]> password = insertPassword();
if(password.isPresent()) {
try {
KeyStore trustStore = KeyStore.getInstance(keyStoreType.get());
trustStore.load(null, null);

FileInputStream fis = new FileInputStream(keyStoreFile);
trustStore.load(fis, password.get());


//This causes the changeListener to be notified because the reference changed
this.getEntityController().setTrustStore(trustStore);


//The changeListener won't be notified, because only the KeyStore changes internally but the reference stays the same
trustStore.deleteEntry(trustStore.aliases().nextElement());
this.getEntityController().setTrustStore(trustStore);
//This also won't notify the changeListener
String a = this.getEntityController().getTrustStore().aliases().nextElement();
this.getEntityController().getTrustStore().deleteEntry(a);


} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (CertificateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
//TODO: password was wrong -> show error message
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
}

相关代码是ObjectProperty所在的位置s 值第一次被设置,然后一个元素被删除。仅当 ObjectProperty 时,changeListener 才会收到通知。 s 值已设置,但值更改时不设置。

我的问题是:如果 KeyStore 我怎样才能得到通知即使引用的对象没有改变也会更新?JavaFX中有内置的方法吗?

我的目的是显示 ListView 中包含的所有证书和 key 并更新ListView每次在KeyStore中添加或删除证书时从changeListener 中。也许我完全走错了路,我应该采取完全不同的做法?

其他信息:

this.getEntityController()

返回 EntityController 的实例它本身保存在 ObjectProperty<EntityController> 中.

最佳答案

我不知道处理此限制的更好方法,因此我所做的只是对修改后的对象执行 set() 操作,以便引用保持相同,但更改会传播。

关于java - 除非 ObjectProperty 的源发生更改,否则不会触发 ChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41257439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com