隨筆 - 3, 文章 - 0, 評論 - 9, 引用 - 0
          數(shù)據(jù)加載中……

          java.beans.PropertyChangeSupport小解!

          大家好!好不容易學(xué)了一段時間的rcp,可是為了考試,幾個星期沒有寫代碼,學(xué)的一點也忘的差不多了。趁著暑假有時間,好好學(xué)一學(xué)。早就聽說里gef 的大名,看樣子我也不能免俗,也搞起gef起來!不知道是不是以前的基礎(chǔ)沒學(xué)好,第一個問題就是關(guān)于PropertyChangeSupport支持類的。

          簡要介紹一下,java.beans.PropertyChangeSupport是一個實用工具類,支持綁定該屬性的bean能夠使用該類。還是不多廢話了,用代碼說話。

          import ?java.beans.PropertyChangeListener;
          import ?java.beans.PropertyChangeSupport;

          public ? class ?PropertyChangeSupportTest?{

          ????
          transient ? protected ?PropertyChangeSupport?listeners? = ? new ?PropertyChangeSupport( this );

          ????
          public ? void ?addPropertyChangeListener(PropertyChangeListener?listener)?{
          ????????listeners.addPropertyChangeListener(listener);
          ????}

          ????
          protected ? void ?firePropertyChange(String?prop,?Object?old,?Object?newValue)?{
          ????????listeners.firePropertyChange(prop,?old,?newValue);
          ????}

          ????
          public ? void ?removePropertyChangeListener(PropertyChangeListener?l)?{
          ????????listeners.removePropertyChangeListener(l);
          ????}
          }
          可以看見PropertyChangeSupport的構(gòu)造方法,是將此類本身綁定到該屬性,以便他的子類也能夠使用此工具類。主要看他的第2個方法,觸發(fā)屬性變化事件(prop為綁定的屬性名,在后面的事件處理中可以根據(jù)此屬性名獲得事件處理源)
          再看看它的子類:

          public?class?BeanTest?extends?PropertyChangeSupportTest?{

          ????
          public?static?final?String?TEST?=?"test";

          ????
          private?String?name?=?"";

          ????
          public?void?setName(String?name)?{
          ????????
          if?(this.name.equals(name))?{
          ????????????System.out.println(
          "BeanTest?的?name?屬性沒有變化!");
          ????????????
          return;
          ????????}
          ????????
          this.name?=?name;
          ????????firePropertyChange(TEST,?
          null,?name);
          ????}

          ????
          public?String?getName()?{
          ????????
          return?this.name;
          ????}
          }
          BeanTest類有一個name屬性,在改變name的值時,會觸發(fā)PropertyChangeEvent事件。接下來我們看看事件監(jiān)聽極其處理:

          import?java.beans.PropertyChangeEvent;
          import?java.beans.PropertyChangeListener;

          public?class?BeanTestListener?implements?PropertyChangeListener{

          ????
          public?void?propertyChange(PropertyChangeEvent?evt)?{
          ????????
          if(evt.getPropertyName().equals(BeanTest.TEST))
          ????????????System.out.println(
          "BeanTest?的?name?屬性變化!");
          ????????
          ????}
          ????
          public?static?void?main(String[]?args){
          ????????BeanTest?test?
          =?new?BeanTest();
          ????????test.addPropertyChangeListener(
          new?BeanTestListener());
          ????????test.setName(
          "fuxueliang");
          ????????test.setName(
          "fuxueliang");
          ????????System.out.println(test.getName());
          ????}

          }
          可以看到是通過evt.getPropertyName().equals(BeanTest.TEST)來確定事件源的。運行結(jié)果如下:

          BeanTest?的?name?屬性變化!
          BeanTest?的?name?屬性沒有變化!
          fuxueliang
          /**
          ?* 當(dāng)執(zhí)行setName操作時,會觸發(fā)firePropertyChange事件,
          ?* 因為PropertyChangeSupportTest和PropertyChangeSupport綁定
          ?* 那么在BeanTestListener中,因為添加里支持類的監(jiān)聽器,所以應(yīng)該觸發(fā)propertyChange事件,
          ?* 并且會根據(jù)evt.getPropertyName()來獲得你在BeanTest中觸發(fā)事件是綁定的屬性名稱
          ?*/

          這只是分析了PropertyChangeSupport的一個方法,其余的方法大家可以一同探討。

          至于監(jiān)聽器的機制大家可以參看陳剛的《eclipse從入門到精通》,里面有一個登錄監(jiān)聽的實現(xiàn)。

          以上只是本人的一點粗略見解,有不對的地方還請指正。由于這是本人第一次寫這種文章,在表達(dá)幾措辭方面可能有所欠缺,在以后的帖子中一定盡力改正,故請原諒! *? _ *

          posted on 2006-07-18 14:02 nick 閱讀(3866) 評論(4)  編輯  收藏 所屬分類: RCP & GEF

          評論

          # re: java.beans.PropertyChangeSupport小解!  回復(fù)  更多評論   

          學(xué)習(xí)了,3Q
          2007-10-15 21:12 | 步行者

          # re: java.beans.PropertyChangeSupport小解!  回復(fù)  更多評論   

          public static final String TEST = "test";

          應(yīng)該是
          public static final String TEST = "name";
          2008-03-10 11:08 |

          # re: java.beans.PropertyChangeSupport小解!  回復(fù)  更多評論   

          觀察者模式
          2008-08-21 17:03 | jadcoon

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 小金县| 怀集县| 元江| 昂仁县| 革吉县| 泸溪县| 彰化县| 雷波县| 曲阳县| 西充县| 保定市| 绥宁县| 梁平县| 泾川县| 嘉善县| 长丰县| 邵武市| 慈溪市| 宝应县| 庆安县| 永济市| 石楼县| 报价| 阿坝| 南康市| 龙南县| 黄骅市| 大竹县| 拜泉县| 曲周县| 勐海县| 开封市| 吉木萨尔县| 策勒县| 和平县| 秦皇岛市| 萍乡市| 天全县| 同心县| 阳泉市| 江山市|