2009-06-26 39 views
3

我創建了一個對話框類,用於填充一些常見的小部件,如文本,組合和樹。當按下輸入/返回時,無論按下什麼窗口小部件,它都有默認行爲來消除此對話框(與按下默認的「確定」按鈕相同)。全局禁用SWT對話框中的輸入/返回鍵

爲了防止這種行爲,我必須添加遍歷每個小部件的聆聽者過濾遍歷鍵:

if (SWT::TRAVERSE_RETURN == event.detail) { 
    event.doit = false 
} 

這有點煩人。有沒有一種方法可以在對話層面進行全球性的工作?

+0

@bryantsai感謝您的反饋意見:如果你確實解決了這個問題,不要忘記在這裏發佈答案(你可以選擇它作爲正式答案) – VonC 2009-06-29 03:51:25

回答

0

根據this thread,這似乎是唯一的出路,也Snippet127所示(「防止Tab從穿越了控制」)

可能是這樣Dialog class說明了他們的keyTraversed()全球使用TraverseListener的方法。

+0

剛剛在對話框類中實現了TraverseListener,但事實證明事件會先到個別小部件,然後'可能'進入包含對話框(不確定,因爲我無法觀察到這種行爲)。無論如何,全球定義對話類似乎並不是一個解決方案。 – bryantsai 2009-06-29 02:54:53

0

您可以將默認按鈕設置爲null(Shell#setDefaultButton(null))以防止完整對話框的行爲,否則我想您必須單擊希望壓縮Enter鍵的小部件。

0

我對這個問題的解決方案似乎目前工作,是創建一個不可見的禁用按鈕,並將其設置爲shell的默認按鈕。爲了避免弄亂佈局的其餘部分,我使用傳遞給createButtonsForButtonBar()的父代在對話框的按鈕欄中創建了按鈕。

final Button hiddenButton = new Button(parent, SWT.PUSH); 
    hiddenButton.setVisible(false); 
    hiddenButton.setEnabled(false); 
    parent.getShell().setDefaultButton(hiddenButton); 

現在,所有默認選擇事件都會按下該按鈕。根據需要,它什麼都不做。

+1

這是一個非常糟糕的解決方案 – iberbeu 2014-04-21 13:56:03

3

我發現在JFace對話框中,通過重寫方法createButtonsForButtonBar可以很容易地禁用默認的Enter鍵。

和的最後一個參數:

createButton(父,IDialogConstants.OK_ID,IDialogConstants.OK_LABEL, 假);

需要是假的:

@Override 
protected void createButtonsForButtonBar(Composite parent) { 
Button button = createButton(parent, IDialogConstants.OK_ID, 
     IDialogConstants.OK_LABEL, false); 
} 
0

它看起來像我的回答是爲時已晚,也許我的解決方案,在2009年是不可能的......但無論如何,我將它張貼。

這個問題可以用Display.addFilter來解決。您必須在程序開始的某個地方爲遍歷事件添加過濾器。

Display.getDefault().addFilter(SWT.Traverse, new Listener() { 
    @Override 
    public void handleEvent(Event event) { 
     if(SWT.TRAVERSE_RETURN == event.detail) { 
      System.out.println("Global SWT.TRAVERSE_RETURN"); 
      event.doit = false; 
     } 
    }; 
}); 

後來創建對話框,它的默認按鈕不起作用:

Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 
shell.setLayout(new GridLayout(2, false)); 
shell.setSize(250, 100); 

Label loginLabel = new Label(shell, SWT.NONE); 
loginLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); 
loginLabel.setText("Login"); 

Text text = new Text(shell, SWT.BORDER); 
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

Composite buttonsContentComposite = new Composite(shell, SWT.NONE); 
buttonsContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); 
buttonsContentComposite.setLayout(new GridLayout(2, false)); 

Button okButton = new Button(buttonsContentComposite, SWT.PUSH); 
okButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); 
okButton.setText("Ok"); 
okButton.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 
     System.out.println("Ok"); 
    }; 
}); 

Button cancelButton = new Button(buttonsContentComposite, SWT.PUSH); 
cancelButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); 
cancelButton.setText("Cancel"); 
cancelButton.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 
     System.out.println("Cancel"); 
    }; 
}); 

shell.setDefaultButton(okButton); 

shell.open(); 
while (!shell.isDisposed()) { 
    if(!Display.getCurrent().readAndDispatch()) { 
     Display.getCurrent().sleep(); 
    } 
} 

過濾掉遍歷事件並禁止將其發送到外殼這樣默認的按鈕將無法工作。 但這種方法有一個細微差別 - 您可以添加遍歷偵聽器殼和覆蓋全球的濾鏡效果:

shell.addTraverseListener(new TraverseListener() { 
    @Override 
    public void keyTraversed(TraverseEvent event) { 
     event.doit = true; 
    } 
}); 

此偵聽器啓用事件背部和默認按鈕將觸發。