2013-12-09 90 views
0

如何強制Listviewer更新大小?當我加入到這是長於現有大小列表中的一個元素,在listviewer應擴大:Jface:如何強制Listviewer調整大小

enter image description here

我以這種方式更新Listviewer:

lvConstraints.refresh(); 

回答

1

確定在添加內容後重新調整它是個好主意?國際海事組織(IMO)在運行時進行擴展和崩潰會導致非常糟糕的用戶體驗你不能想到元素的平均寬度,包含在查看器內?對於更長的元素,我建議你使用滾動功能。

如果你還是想改變viwer的大小添加的元素之後,你可以做這樣的:

viewer.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT); 
viewer.getControl().pack(); 

例子:

public class Test { 

    private class MyContentProvider implements IStructuredContentProvider { 

     @Override 
     public Object[] getElements(Object inputElement) { 
      return ((List) inputElement).toArray(new MyModel[0]); 
     } 

     @Override 
     public void dispose() { 
     } 

     @Override 
     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 
     } 

    } 

    public class MyModel { 
     public String counter; 

     public MyModel(String counter) { 
      this.counter = counter; 
     } 

     @Override 
     public String toString() { 
      return "Item " + counter; 
     } 
    } 

    public Test(Shell shell) { 
     // final TableViewer v = new TableViewer(shell); 
     Composite c = new Composite(shell, SWT.NONE); 
     GridLayoutFactory.swtDefaults().applyTo(c); 
     GridDataFactory.defaultsFor(c).hint(100, 100).applyTo(c); 

     final ListViewer v = new ListViewer(c); 
     // v.setSize(200, 300); 
     v.setLabelProvider(new LabelProvider()); 
     // for demonstration purposes use custom content provider 
     // alternatively you could use ArrayContentProvider.getInstance() 
     v.setContentProvider(new MyContentProvider()); 
     v.setComparator(new ViewerComparator()); 
     v.setInput(createModel()); 

     GridDataFactory.defaultsFor(v.getControl()).align(SWT.BEGINNING, SWT.TOP).hint(100, 100).grab(true, true) 
      .applyTo(v.getControl()); 
     // v.getTable().setLinesVisible(true); 

     Button b = new Button(c, SWT.NONE); 
     b.setText("button"); 
     b.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 
       int nextInt = RandomUtils.nextInt(100); 
       String s = ""; 
       for (int i = 0; i < nextInt; i++) { 
        s += "" + i; 
       } 
       // v.add(new MyModel(s)); 
       ((List) v.getInput()).add(new MyModel(s)); 
       v.refresh(); 
       v.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT); 
       v.getControl().pack(); 
      } 
     }); 
    } 

    private List<MyModel> createModel() { 
     List<MyModel> elements = new ArrayList<Test.MyModel>(); 

     for (int i = 0; i < 10; i++) { 
      elements.add(new MyModel("" + i)); 
     } 

     return elements; 
    } 

    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     new Test(shell); 
     shell.open(); 

     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 

     display.dispose(); 

    } 

} 

要顯示一個垂直滾動條,創建觀衆SWT.V_SCROLL風格:像這樣:

final ListViewer v = new ListViewer(c, SWT.V_SCROLL); 

爲了防止調整大小, (請注意,您也可以提供寬度提示,以防止調整大小,然後調整特定寬度)(注意::pack()方法已被刪除):

v.refresh(); 
Point computeSize = v.getControl().computeSize(SWT.DEFAULT, 200); 
v.getList().setSize(computeSize); 
+0

到目前爲止工作的Thx。我怎樣才能爲列表查看器設置最大高度?當達到這個最大高度時,列表查看器應該提供一個滾動條。 – ph09

+1

請看我更新的答案。 –

相關問題