2014-07-10 2 views
1

나는 5 열의 gridlayout를 갖는 합성물을 가지고 있으며 일부에는 버튼이 포함되어 있습니다. 이제 다른 컨트롤을 에 GridLayout의 특정 위치에 추가 할 수 있습니까? 예를 들어, 하나의 TextField를 Button3 대신에 세 번째 열의 첫 번째 행에 추가하고 Button 3이 button7 이후의 마지막 행으로 이동해야합니다. 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

GridLayout과는 상자에서이 작업을 수행하지 않을 것이다, 그러나 당신이 버튼을 정렬 사용자 지정 합성을 만들 수 있도록, 부모 복합체를 반환하는 순서로 버튼을 렌더링하고 그에 추가합니다 오히려 직접 쉘보다는 : 버튼에

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 방법은 비교기를 정렬하는 데 사용할 수있는 그들에게 데이터를 첨부 할 수있는 좋은 방법을 제공한다.