2012-02-14 69 views
0

我有一个问题,在构建我的shell并打开它之后,我需要在运行时将图形元素添加到shell中 并且一旦shell打开,看不到添加的新图形元素。 新的图形元素出现,以防万一我调整壳的大小。如何在ScrolledComposite中自动刷新SWT

有没有办法解决这个问题并自动刷新 这里有个简单的例子: 1-我在标签文件夹中添加了10个A按钮并打开了shell。 2-然后,我添加B的10个按钮到壳体 3-我可以看到按钮B只是如果调整壳

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.CTabFolder; 
import org.eclipse.swt.custom.CTabItem; 
import org.eclipse.swt.custom.ScrolledComposite; 
import org.eclipse.swt.events.ControlAdapter; 
import org.eclipse.swt.events.ControlEvent; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 


public class TabFolder 
{ 

    public static void main(String[] args) 
    { 
     Display display = new Display(); 

     final Shell shell = new Shell(display); 
     shell.setSize(500, 500); 
     shell.setLayout(new GridLayout()); 
     final CTabFolder folder = new CTabFolder(shell, SWT.BORDER); 
     folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
     folder.setSize(500, 500); 

     CTabItem item = new CTabItem(folder, SWT.CLOSE); 

     final ScrolledComposite scrollComp = new ScrolledComposite(folder, SWT.V_SCROLL | SWT.H_SCROLL); 
     item.setControl(scrollComp); 

     final Composite tab1Comp = new Composite(scrollComp, SWT.NONE); 
     tab1Comp.setLayout(new GridLayout(1, true)); 
     tab1Comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 

     scrollComp.setContent(tab1Comp); 
     scrollComp.setExpandVertical(true); 
     scrollComp.setExpandHorizontal(true); 
     scrollComp.addControlListener(new ControlAdapter() 
     { 
      public void controlResized(ControlEvent e) 
      { 
       Rectangle r = scrollComp.getClientArea(); 
       scrollComp.setMinSize(folder.computeSize(r.width, SWT.DEFAULT)); 
      } 
     }); 

     for (int x = 0; x < 10; x++) 
     { 
      Button text = new Button(tab1Comp, SWT.NONE); 
      text.setText("A"); 
     } 

     shell.open(); 

     for (int x = 10; x < 20; x++) 
     { 
      Button text = new Button(tab1Comp, SWT.NONE); 
      text.setText("B"); 
     } 
     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 

     display.dispose(); 
    } 
} 

感谢。

回答

0

添加B按钮后,您必须拨打tab1Comp.layout(),因为编程式更改后布局不会自动更新。您还必须更新您的ScrolledComposite的最小尺寸,因为controlResized(...)也未在程序化更改后调用。

+0

非常感谢您的回答 感谢您的帮助 – user1205079 2012-02-14 16:28:41