2014-07-10 117 views
1

我有一個5列網格佈局的組合,它包含 一些按鈕。現在是否可以添加任何其他控件到 GridLayout的特定位置?例如,我想在第三列的第一行添加一個TextField,即代替Button3 ,並且按鈕3應該下到最後一行,即按鈕7之後。 GridLayout中可以使用任何類型的動態定位?動態組件網格佈局定位

package zrcp; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.widgets.Shell; 

    public class LayoutExample { 

    protected Shell shell; 

    /** 
    * Launch the application. 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try { 
      LayoutExample window = new LayoutExample(); 
      window.open(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Open the window. 
    */ 
    public void open() { 
     Display display = Display.getDefault(); 
     createContents(); 
     shell.open(); 
     shell.layout(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
    } 

    /** 
    * Create contents of the window. 
    */ 
    protected void createContents() { 
     shell = new Shell(); 
     shell.setSize(450, 300); 
     shell.setText("SWT Application"); 
     shell.setLayout(new GridLayout(5, false)); 

     Button btnNewButton = new Button(shell, SWT.NONE); 
     btnNewButton.setText("Button1"); 

     Button btnNewButton_1 = new Button(shell, SWT.NONE); 
     btnNewButton_1.setText("Button2"); 

     Button btnNewButton_2 = new Button(shell, SWT.NONE); 
     btnNewButton_2.setText("Button3"); 

     Button btnNewButton_3 = new Button(shell, SWT.NONE); 
     btnNewButton_3.setText("Button4"); 

     Button btnNewButton_4 = new Button(shell, SWT.NONE); 
     btnNewButton_4.setText("Button5"); 

     Button btnNewButton_5 = new Button(shell, SWT.NONE); 
     btnNewButton_5.setText("Button6"); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 

     Button btnNewButton_6 = new Button(shell, SWT.NONE); 
     btnNewButton_6.setText("Button7"); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 
     new Label(shell, SWT.NONE); 

    } 

} 

回答

1

網格佈局是不會這樣做的開箱即用,但它會呈現父複合返回它們的順序按一下按鈕,這樣你就可以創建一個排序按鍵的自定義組合,並將它們添加到而不是直接向殼牌:

class SortedComposite extends Composite { 
    private Comparator<Control> comparator; // TODO Create this 

    public SortedComposite(Composite parent, int style) { 
     super(parent, style); 
    } 

    @Override 
    public Control[] getChildren() { 
     Control[] children = super.getChildren(); 
     Arrays.sort(children, comparator); 
     return children; 
    } 

} 

setData方法上的按鈕將提供數據附加到他們的比較可以使用對它們進行排序的好方法。