2014-11-01 6 views
1

콘텐츠 길이가 동적으로 달라지는 SWT 텍스트 필드가 있습니다. 나는 전체 텍스트를 볼 수 있도록해야합니다. 나는 그것을하기 위해 아래의 코드를 사용하고있다. 텍스트가 큰 경우이 코드 동적으로 SWT 텍스트 필드 크기 조정

enter image description here

public class MyDynamicDialog extends TrayDialog { 
    Text errorMessageText; 
    ScrolledComposite scrolledComposite; 
    Shell shell; 
    DynamicData dynamicData; 

    public MyDynamicDialog (final Shell shell) { 
     super(shell); 
     this.shell = shell; 
    } 
    public MyDynamicDialog (final Shell shell, final DynamicData dynamicData) { 
     super(shell); 
     this.shell = shell;  
     this.dynamicData = dynamicData; 
    } 

    @Override 
    protected Control createDialogArea(final Composite parent) { 
     final GridLayout layout = new GridLayout(); 
     parent.setLayout(layout); 
     this.scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER); 
     this.scrolledComposite.setExpandHorizontal(true); 
     this.scrolledComposite.setExpandVertical(false); 

     final Composite composite = new Composite(this.scrolledComposite, SWT.NONE); 
     final GridLayout gridLayout = new GridLayout(); 
     gridLayout.numColumns = 2; 
     composite.setLayout(gridLayout); 
     composite.setSize(400, 400); 

     this.scrolledComposite.setContent(composite); 

     this.scrolledComposite.addControlListener(new ControlAdapter() { 
      @Override 
      public void controlResized(final ControlEvent e) { 
       final Rectangle clientArea =  
       MyDynamicDialog.this.scrolledComposite.getClientArea(); 
       final Point minSize = MyDynamicDialog.this.scrolledComposite.getContent().computeSize(clientArea.width, SWT.DEFAULT); 
       MyDynamicDialog.this.scrolledComposite.getContent().setSize(minSize); 
      } 
     }); 
     createErrorMessageText(composite); 
     return parent; 
    } 

    private void createErrorMessageText(final Composite parent) { 
     final GridData errText = new GridData(/*200, SWT.DEFAULT*/); 
     errText.grabExcessHorizontalSpace = true; 
     errText.horizontalSpan = 3; 
     errText.horizontalAlignment = GridData.FILL; 
     this.errorMessageText = new Text(parent, SWT.READ_ONLY /*| SWT.WRAP | SWT.MULTI*/); 
     this.errorMessageText.setLayoutData(errText); 

     this.errorMessageText.addModifyListener(new ModifyListener() { 
      public void modifyText(final ModifyEvent e) { 

       final int columns = MyDynamicDialog.this.errorMessageText.getText().length() + 3; 
       final GC gc = new GC (MyDynamicDialog.this.errorMessageText); 
       final FontMetrics fm = gc.getFontMetrics(); 
       final int width = columns * fm.getAverageCharWidth(); 
       final int height = fm.getHeight(); 
       gc.dispose(); 
       MyDynamicDialog.this.errorMessageText.setSize (MyDynamicDialog.this.errorMessageText.computeSize (width, height)); 
       MyDynamicDialog.this.errorMessageText.redraw(); 
       MyDynamicDialog.this.errorMessageText.getParent().layout(); 

      } 
     }); 
    } 
} 

는이를 화면의 외출. 런타임에 텍스트 필드의 크기를 조정할 수 없습니다. 또한 텍스트 상자의 크기를 조정 한 후 스크롤 된 합성에서 수신기를 다시 호출하려고했습니다. 그러나 그것의 아무도는 작동하지 않는다. 무엇이 잘못되었을 수 있는지 지적하십시오.

답변

3

setSize을 무시하는 레이아웃을 사용할 때 컨트롤에서 수행 할 수 있습니다.

GridDatawidthHint을 원하는 너비 (또한 heightHint)로 설정하십시오.

또한 대화 상자 영역 전체를 레이아웃 한 다음 셸의 크기를 조정해야합니다.

Composite dialogAreaComp = ... get the dialog area composite 

dialogAreaComp.layout(true, true); 


Shell shell = getShell(); 
shell.pack(true);