java.util.Observable类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中java.util.Observable类的一些代码示例,展示了Observable类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable类的具体详情如下:
包路径:java.util.Observable
类名称:Observable

Observable介绍

[英]Observable is used to notify a group of Observer objects when a change occurs. On creation, the set of observers is empty. After a change occurred, the application can call the #notifyObservers() method. This will cause the invocation of the update() method of all registered Observers. The order of invocation is not specified. This implementation will call the Observers in the order they registered. Subclasses are completely free in what order they call the update methods.
[中]Observable用于在发生更改时通知一组观察者对象。创建时,观察者集为空。发生更改后,应用程序可以调用#notifyObservators()方法。这将导致对所有注册的观察者调用update()方法。没有指定调用顺序。此实现将按照观察员注册的顺序调用他们。子类完全不受它们调用更新方法的顺序限制。

代码示例

代码示例来源:origin: stackoverflow.com

import java.util.Observable;
import java.util.Observer;

public class Heraclitus implements Observer {
 public void observe(Observable o) {
  o.addObserver(this);
 }

 @Override
 public void update(Observable o, Object arg) {
  int someVariable = ((Flux) o).getSomeVariable();
  System.out.println("All is flux!  Some variable is now " + someVariable);
 }
}

代码示例来源:origin: stackoverflow.com

import java.util.Observable;

public class Flux extends Observable {
 private int someVariable = 0;

 public void setSomeVariable(int someVariable) {
  synchronized (this) {
   this.someVariable = someVariable;
  }
  setChanged();
  notifyObservers();
 }

 public synchronized int getSomeVariable() {
  return someVariable;
 }
}

代码示例来源:origin: sd4324530/fastweixin

/**
 * 移除配置变化监听器
 *
 * @param handle 监听器
 */
public void removeHandle(final ApiConfigChangeHandle handle) {
  super.deleteObserver(handle);
}

代码示例来源:origin: robovm/robovm

/**
 * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
 * method for every Observer in the list of observers using the specified
 * argument. Afterwards calls {@code clearChanged()}.
 *
 * @param data
 *            the argument passed to {@code update()}.
 */
@SuppressWarnings("unchecked")
public void notifyObservers(Object data) {
  int size = 0;
  Observer[] arrays = null;
  synchronized (this) {
    if (hasChanged()) {
      clearChanged();
      size = observers.size();
      arrays = new Observer[size];
      observers.toArray(arrays);
    }
  }
  if (arrays != null) {
    for (Observer observer : arrays) {
      observer.update(this, data);
    }
  }
}

代码示例来源:origin: stackoverflow.com

model.addObserver(this);
this.notifyObservers(...);

代码示例来源:origin: stackoverflow.com

Observable observable = observables.get(notification);
if (observable==null) {
  observable = new Observable();
  observables.put(notification, observable);
observable.addObserver(observer);
Observable observable = observables.get(notification);
if (observable!=null) {         
  observable.deleteObserver(observer);
Observable observable = observables.get(notification);
if (observable!=null) {
  observable.setChanged();
  observable.notifyObservers(object);

代码示例来源:origin: stackoverflow.com

this.add(label, BorderLayout.CENTER);
this.add(genButtonPanel(), BorderLayout.SOUTH);
model.addObserver(new ModelObserver());
setChanged();
notifyObservers();
setChanged();
notifyObservers(guess.equals(hidden));

代码示例来源:origin: stackoverflow.com

public class Connect extends Observable {

  Connection conn;

  public Connect(Observer o) {
    addObserver(o);
  }

  public void getConnection() {
    // TODO getConnection
    hasChanged();
    notifyObservers();
  }

  public void closeConnection() {
    // TODO closeConnection
    hasChanged();
    notifyObservers();
  }
}

代码示例来源:origin: stackoverflow.com

private Observable observable = new Observable();

public void addObserver(Observer o) {
  observable.addObserver(o);
}

public void notifyObservers() {
  observable.notifyObservers();
}

代码示例来源:origin: triplea-game/triplea

private void notifyObs() {
 super.setChanged();
 super.notifyObservers(gameData);
 super.clearChanged();
}

代码示例来源:origin: stackoverflow.com

public class Student extends Observable {

  private int age;

  Student(int age) {
    this.age = age;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    if (this.age != age) {
      setChanged();
    }

    this.age = age;

    if (hasChanged()) {
      notifyObservers();
    }
  }

  @Override
  public String toString() {
    return "Student [age=" + age + "]";
  }
}

代码示例来源:origin: robovm/robovm

/**
 * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
 * method for every observer in the list of observers using null as the
 * argument. Afterwards, calls {@code clearChanged()}.
 * <p>
 * Equivalent to calling {@code notifyObservers(null)}.
 */
public void notifyObservers() {
  notifyObservers(null);
}

代码示例来源:origin: org.fujion/fujion-core

@Override
public void setModel(M model) {
  if (this.model instanceof Observable) {
    ((Observable) this.model).deleteObserver(this);
  }
  this.model = model;
  modelChanged(null);
  targetChanged(null);
  if (this.model instanceof Observable) {
    ((Observable) this.model).addObserver(this);
  }
}

代码示例来源:origin: org.apache.poi/poi

void checkForLocaleChange(Locale newLocale) {
    if (!localeIsAdapting) return;
    if (newLocale.equals(locale)) return;
    super.setChanged();
    notifyObservers(newLocale);
  }
}

代码示例来源:origin: io.github.oliviercailloux.jmcda/utils

@Override
  public synchronized void clearChanged() {
    super.clearChanged();
  }
}

代码示例来源:origin: stackoverflow.com

public void update(Observable o, Object arg)
{
  if(o.hasChanged())
  {
    // do something
  }
}

代码示例来源:origin: stackoverflow.com

public class MyLoader extends AsyncTaskLoader<Something> {
  private Observable mObservable = new Observable();
  synchronized void addObserver(Observer observer) {
    mObservable.addObserver(observer);
  }
  synchronized void deleteObserver(Observer observer) {
    mObservable.deleteObserver(observer);
  }

  @Override
  public void loadInBackground(CancellationSignal signal)
  {
    for (int i = 0;i < 100;++i)
      mObservable.notifyObservers(new Integer(i));
  }
}

代码示例来源:origin: stackoverflow.com

import java.util.Observable;
  public class Achievement extends Observable {
  public static class AchievementDetails {}
public Achievement() {
    addObserver(EventsListener.getInstance());
  }
  public void achievementReached() {
    AchievementDetails achievemetDetails = null;
    setChanged();
    notifyObservers(achievemetDetails);
  }
}

代码示例来源:origin: BruceEckel/OnJava8-Examples

OCBox(int x, int y, Observable notifier) {
 this.x = x;
 this.y = y;
 notifier.addObserver(this);
 this.notifier = notifier;
 addMouseListener((MouseClick)
  e -> notifier.notifyObservers(OCBox.this));
}
@Override

代码示例来源:origin: stackoverflow.com

if(!changing && hasChanged()){
 notifyObservers();
setChanged();
if(!changing){
 notifyObservers();

相关文章