我可以使用 ObjectChangeListener 监听任何对象的更改吗?
我有一个名为 x 的Integer
;如果它发生变化,那么我想在 GUI 中更新我的表。为了听“x”,我尝试
ChangeEvent y = new javax.swing.event.ChangeEvent(x);
并实现了javax.naming.event.ObjectChangeListener
:
class HDIManagementView extends FrameView
implements ObjectChangeListener, ActionListener, TableModelListener {
并重写了objectChanged
方法来更新我的表。 不会发生
public void objectChanged(javax.naming.event.NamingEvent name){
//gets which status
Object y=name.getChangeInfo();
String m=y.toString();
tableModel.setValueAt(y, 0, 0);
}`
如果我更改“x”,则什么也 ,那么我的表中也不会发生任何变化。我做错了什么?
第二个问题是,x只能按值调用。我只能从我的数据库或属性文件访问 x。当数据库改变时,即使listener监听,x也无法理解它是否改变。我所做的就是听 y 等于 x。当 x 改变时 y 不理解,因为 x 没有通过引用调用。我能做些什么?
I've got an Integer
called x; if it changes then i would like to update my table in a GUI. To listen to "x" I have tried
ChangeEvent y = new javax.swing.event.ChangeEvent(x);
and I implement javax.naming.event.ObjectChangeListener
:
class HDIManagementView extends FrameView
implements ObjectChangeListener, ActionListener, TableModelListener {
and I override the objectChanged
method to update my table. Nothing happened
public void objectChanged(javax.naming.event.NamingEvent name){
//gets which status
Object y=name.getChangeInfo();
String m=y.toString();
tableModel.setValueAt(y, 0, 0);
}`
if i change "x" then nothing changes in my table. What have I done wrong?
Second question is, x can only be called by value. i can only reach x from my database or my properties file. When database changes, x can't understand if it changes or not Even if listener listens. All i do is listen y which equals x. When x changes y doesn't understand because x is not calling by referens. What can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题的答案是“不 - 你不能”和“JNDI 和
javax.naming
与你的问题无关”我认为您可能会将 Swing/JavaBeans 侦听器框架与 Java 命名和目录接口 JNDI 混淆。
ObjectChangeListener
仅用于侦听在 JNDI 上下文中绑定和重新绑定的对象。 您不能使用 ObjectChangeListener 侦听任意对象的更改为此,您需要一个 JNDI 实现。为了监听更改,您需要监听
EventContext
:如果您尝试此操作,将会失败,因为您尚未定义 JNDI 提供程序 。通常,这些将由应用程序服务器供应商提供,例如 IBM WebSphere 或 JBoss。应用程序服务器为应用程序提供 JNDI 来查找数据源或配置信息等资源。
为了让你做你真正想做的事情,你需要实现一些类来包装你的整数并使用 Java 中的属性更改机制:
然后,这个可以由您的代码使用:
The answer to the question is "no - you can't" and "JNDI and
javax.naming
is nothing to do with your problem"I think you may be confusing the Swing/JavaBeans listener framework with JNDI, the Java naming and Directory interface. An
ObjectChangeListener
is only useful for listening to objects which are bound and re-bound in a JNDI context. You cannot use an ObjectChangeListener to listen for changes on an arbitrary objectIn order to do this, you need a JNDI implementation. In order to listen to the change, you listen on an
EventContext
:If you try this it will fail because you have not defined a JNDI provider. Typically these would be provided by an application-server vendor, e.g. IBM WebSphere or JBoss. The application server provides JNDI for applications to lookup resources like data sources, or configuration information.
In order for you to do what you actually want, you'll want to implement some class which wraps your integer and uses the property-change mechanism in Java:
Then, this can be used by your code: