JFace Wizard 自定義 “Next” 按鈕事件

 

JFace 的 Wizard是很常用的UI,我們也很需要在點擊Next的時候做些動作,Wizard 本身沒有給我們提供一個很容易發現的接口,雖然有個 getNextPage() 方法,但是很難用 嘿嘿

 

得自己動動手啦。

 

首先呢,寫個類來繼承WizardDialog,並覆蓋他的buttonPressed方法,這樣就能在點擊Next的時候做動作了。

Code Snippet:

 

public class CreditSCWizardDialog extends WizardDialog {

    public CreditSCWizardDialog(Shell parentShell, IWizard newWizard) {
        super(parentShell, newWizard);
    }
    
    @Override
    protected void buttonPressed(int buttonId) {
        if (buttonId == IDialogConstants.NEXT_ID){

            // 點擊Next的時候 執行actionWhenNextButtonPressed()方法

            // 我這裏這個方法有個boolean的返回值。

            if (actionWhenNextButtonPressed()) {
                super.buttonPressed(buttonId);
            }
        }
        else {
            super.buttonPressed(buttonId);
        }
    }
    
    protected boolean actionWhenNextButtonPressed() {

       // 這裏填寫要做的動作

      // 我想很多情況下,你的Wizard會有很多頁面對吧

      // 而你呢,不同的頁面又想有不同的動作,怎麼辦?

      }
}

 

其實呢,很簡單了。

再寫一個類繼承繼承WizardPage,在這個類裏面弄個抽象方法,

比如這個抽象方法叫做:nextButtonClick().

 

public abstract class CreditSCWizardPage extends WizardPage {

    protected CreditSCWizardPage(String pageName) {
        super(pageName);
    }

    protected abstract boolean nextButtonClick();

}

 

嗯,我想你看明白了對吧,這裏有最關鍵的一步,we are almost there.

 

修改剛纔的那個WizardDialog類中的actionWhenNextButtonPressed()方法。

protected boolean actionWhenNextButtonPressed() {

        IWizardPage currentPage = getWizard().getContainer().getCurrentPage();
        return ((CreditSCWizardPage) currentPage).nextButtonClick();

}


這樣呢,你的每個WizardPage實現中都去實現自己的nextButtonClick()方法就行啦

發佈了10 篇原創文章 · 獲贊 13 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章