views:

140

answers:

1

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?

+4  A: 

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 object

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

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)

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:

public class MyInteger {
  private int x;
  private final PropertyChangeSupport pcs = new PropertyChangeSupport();
  public void setX(int i) {
     int old = x;
     x = i;
     pcs.firePropertyChanged("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();
  }
});
oxbow_lakes
I think i am confused too :) Actually, i have no idea what you are talking about. ALL i want is just to see if an Integer changed? So i wrote some article and http://www.j2ee.me/j2se/1.3/docs/api/javax/swing/event/ChangeEvent.html . Maybe i am in the wrong way
Iguramu
See my updated answer - you just want a `PropertyChangeListener`
oxbow_lakes
Do i have to use JNDI to understand when integer changes?
Iguramu
No - forget about JNDI - it is **nothing whatsoever** to do with your problem
oxbow_lakes
My computer says something about your code. It says "can not find symbol pcs" :) you can fix it. and thanks. I'll try
Iguramu
I've made a small syntax error. It should be PropertyChangeSupport pcs
oxbow_lakes
It's now fixed - hope it works for you this time
oxbow_lakes
it worked. Thanks a lot
Iguramu