隨筆 - 3, 文章 - 0, 評論 - 9, 引用 - 0
          數據加載中……

          java.beans.PropertyChangeSupport小解!

          大家好!好不容易學了一段時間的rcp,可是為了考試,幾個星期沒有寫代碼,學的一點也忘的差不多了。趁著暑假有時間,好好學一學。早就聽說里gef 的大名,看樣子我也不能免俗,也搞起gef起來!不知道是不是以前的基礎沒學好,第一個問題就是關于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的構造方法,是將此類本身綁定到該屬性,以便他的子類也能夠使用此工具類。主要看他的第2個方法,觸發屬性變化事件(prop為綁定的屬性名,在后面的事件處理中可以根據此屬性名獲得事件處理源)
          再看看它的子類:

          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的值時,會觸發PropertyChangeEvent事件。接下來我們看看事件監聽極其處理:

          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)來確定事件源的。運行結果如下:

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

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

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

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

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

          評論

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

          學習了,3Q
          2007-10-15 21:12 | 步行者

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

          public static final String TEST = "test";

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

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

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

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


          網站導航:
           
          主站蜘蛛池模板: 屯门区| 阿坝| 洛南县| 沛县| 奈曼旗| 蓝田县| 连云港市| 全州县| 嘉义市| 龙泉市| 改则县| 定襄县| 锦州市| 永泰县| 汪清县| 黑河市| 高安市| 海宁市| 静海县| 兴隆县| 凤翔县| 武冈市| 瑞安市| 红桥区| 资兴市| 泗洪县| 宁南县| 中山市| 洪洞县| 德昌县| 怀柔区| 平原县| 湘西| 龙南县| 彭水| 延庆县| 鞍山市| 贺州市| 攀枝花市| 景洪市| 元阳县|