我可以使用ObjectChangeListener侦听任何对象上的更改吗? [英] Can I use ObjectChangeListener to listen for changes on any object?

查看:75
本文介绍了我可以使用ObjectChangeListener侦听任何对象上的更改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为x的Integer;如果它改变了,那么我想在GUI中更新我的表.听过我尝试过的"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);

,我实现了javax.naming.event.ObjectChangeListener:

class HDIManagementView extends FrameView 
         implements ObjectChangeListener, ActionListener, TableModelListener  {

,并且我覆盖了objectChanged方法来更新我的表.什么都没发生

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

}`

如果我更改"x",则表中没有任何变化.我做错了什么?

if i change "x" then nothing changes in my table. What have I done wrong?

第二个问题是,x只能按值调用.我只能从我的数据库或属性文件访问x.当数据库更改时,即使侦听器侦听,x也无法理解它是否更改.我所要做的就是听y,它等于x.当x发生变化时,y无法理解,因为x未被引用调用.我该怎么办?

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?

推荐答案

问题的答案是不-您不能" " JNDI和javax.naming无关紧要与您的问题有关"

The answer to the question is "no - you can't" and "JNDI and javax.naming is nothing to do with your problem"

我认为您可能会将Swing/JavaBeans侦听器框架与 JNDI (Java命名和目录接口)相混淆. ObjectChangeListener仅用于侦听在JNDI上下文中绑定和重新绑定的对象. 您不能使用ObjectChangeListener侦听任意对象上的更改

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 object

InitialContext ctx = new InitialContext();
ctx.rebind("path/to/x", new Integer(4));

为此,您需要一个JNDI实现.为了收听更改,您收听EventContext:

In order to do this, you need a JNDI implementation. In order to listen to the change, you listen on an EventContext:

InitialContext ctx = new InitialContext();
EventContext ec = (EventContext) ctx.lookup("");
ec.addNamingListener("path/to/x", myListener)

如果尝试执行此操作,它将失败,因为尚未定义JNDI提供程序.通常,这些将由应用服务器供应商提供,例如. IBM WebSphere或JBoss.应用程序服务器为应用程序提供JNDI,以查找诸如数据源或配置信息之类的资源.

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.

为了让您做自己想做的事,您将需要实现一些类,该类包装您的整数并在Java中使用 property-change 机制:

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:

public class MyInteger {
  private int x;
  private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  public void setX(int i) {
     int old = x;
     x = i;
     pcs.firePropertyChange("x", old, x); 
  }

  public void addListener(PropertyChangeListener l) {
      pcs.addListener("x", l);
  }
}

然后,您的代码可以使用它:

Then, this can be used by your code:

MyInteger i = new MyInteger(9);
i.addListener(new PropertyChangeListener() {
  public void propertyChanged(PropertyChangedEvent e) {
      //implement to process the change - e.g. update your table
      Integer i = (Integer) e.getNewValue();
  }
});

这篇关于我可以使用ObjectChangeListener侦听任何对象上的更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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