2013-09-29 55 views
0

當我創建一個檢查按鈕,並在按鈕欄中的對話框兩端的按鈕, 默認情況下創建它,但是當我拖動或調整對話框的大小,檢查按鈕移動離開對話框的左端。調整對話框大小時應該如何防止它移動。 這是我如何overrided我的按鈕欄。如何防止檢查按鈕移動時,我在swt中拖動對話框?

 @Override 
    protected void createButtonsForButtonBar(Composite parent) { 
    super.createButtonsForButtonBar(parent); 


    ((GridLayout) parent.getLayout()).numColumns = ((GridLayout) parent.getLayout()).numColumns+2; 

    GridData checkData = new GridData(SWT.LEFT, 
      SWT.BOTTOM,true,true); 

    GridData labelData = new GridData(SWT.CENTER, 
      SWT.CENTER,true,true); 

    GridData closeLayoutData = new GridData(SWT.RIGHT, 
      SWT.BOTTOM,true,true); 

    parent.setLayoutData(closeLayoutData); 

    Button close = getButton(IDialogConstants.CANCEL_ID); 
    close.setText("Close"); 
    close.setLayoutData(closeLayoutData); 
    close.setParent(parent); 

    final Button checkButton = new Button(parent, SWT.CHECK); 
    checkButton.setText("View All schedules"); 
    checkButton.setSelection(false); 
    checkButton.setLayoutData(checkData); 
    checkButton.setParent(parent); 
    checkButton.moveAbove(close); 
    checkButton.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent e) { 
      if (checkButton.getSelection()) { 
       treeViewer.setFilters(new ViewerFilter[] { new ExistingExpiredScheduleFilter() }); 
      } else { 
       treeViewer.setFilters(new ViewerFilter[] { new ExistingScheduleFilter() }); 
      } 
     } 
    }); 


    Label label = new Label(parent,SWT.HORIZONTAL); 
    label.setLayoutData(labelData); 
    label.setParent(parent); 
    label.moveAbove(close); 

} 

回答

1

這工作:

protected void createButtonsForButtonBar(Composite parent) 
    { 
     final GridLayout layout = (GridLayout)parent.getLayout(); 
     layout.numColumns += 2; 
     layout.makeColumnsEqualWidth = false; 

     parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));  

     final Button checkButton = new Button(parent, SWT.CHECK); 
     checkButton.setText("View All schedules"); 
     checkButton.setSelection(false); 
     checkButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); 
     // Left out selection code for clarity 

     final Label label = new Label(parent, SWT.NONE); 
     label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

     createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 

     createButton(parent, IDialogConstants.CANCEL_ID, "Close", false); 
    } 

無需調用setParent。我已經創建了一些東西,所以不需要重新排序。

+0

我想知道我做了什麼錯誤嗎?,除了設置父項,我都做了相同的操作,調用超級方法有什麼區別,我甚至嘗試過不調用父級。無論如何,你的回答是正確的 – Juseeth

+0

超級電話只是做兩個'createButton'調用,我已經在我的代碼中完成了它們的清晰。問題主要是通常對齊的「父」的佈局數據。還有'GridData'值的'搶橫向空間'參數。 –

相關問題