MyException - 我的异常网
当前位置:我的异常网» 操作系统 » 动态改变Composite中的子控件时,用layout()而非red

动态改变Composite中的子控件时,用layout()而非redraw() ,用CTabFolder的例证进行说明

www.MyException.Cn  网友分享于:2013-02-20  浏览:35次
动态改变Composite中的子控件时,用layout()而非redraw() ,用CTabFolder的例子进行说明
{
  Composite topComp = null;
  // ...

  // CTabFolder
  CTabFolder tabFolder = new CTabFolder(topComp, SWT.TOP | SWT.BORDER);
  GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
  gd.horizontalSpan = 20;
  tabFolder.setLayoutData(gd);
  tabFolder.setTabHeight(30);
  tabFolder.setSimple(true);
  tabFolder.marginHeight = 0;
  tabFolder.marginWidth = 0;
  Font font = null;
  // font = FontFactory.getFont("Arial", 8, SWT.BOLD);

  // CTabItem
  CTabItem item2 = new CTabItem(tabFolder, SWT.NULL);
  item2.setText("Pins");
  item2.setFont(font);
  // ----
  Group pinsGroup = new Group(tabFolder, SWT.FILL);
  pinsGroup.setText("Pins");
  Color color = null;
  pinsGroup.setBackground(color /* ColorFactory.getColor(255, 255, 255) */);
  pinsGroup.setLayout(new RowLayout());

  // 动态填充 pinsGroup的内容
  setDynamicData(pinsGroup);

  item2.setControl(pinsGroup);

  // ============================================

 }

 public void setDynamicData(Composite pinsGroup) {
  // 1.清除掉原来的 子控件
  Control[] controls = pinsGroup.getChildren();
  for (int i = 0; i < controls.length; i++) {
   controls[i].dispose();
  }
  // 2. 数据填充,例如:
  for (int i = 0; i < 20; i++) {
   final CLabel pinLabel = new CLabel(pinsGroup, SWT.CENTER);
   pinLabel.setText("pinLabel:"+i);
   RowData rowData = new RowData();
   rowData.height = 20;
   rowData.width =40;
   pinLabel.setLayoutData(rowData);
  }
  
  //3. layout--->这是关键(用pinsGroup.redraw();//这个还是没有效果)
  pinsGroup.layout(); 
  /*markLayout()这是源码中的真正实现,只有设置了.layout(),参数changed才为true,才有动态改变的效果
   * -----执行---->if (all) {...}分支
  void markLayout (boolean changed, boolean all) {
   if (layout != null) {
    state |= LAYOUT_NEEDED;
    if (changed) state |= LAYOUT_CHANGED;
   }
   if (all) {
    Control [] children = _getChildren ();
    for (int i=0; i<children.length; i++) {
     children [i].markLayout (changed, all);
    }
   }
  }*/
  //pinsGroup.redraw();
  /*设置.redraw()后的内部实现,参数all为false-----执行---->OS.RedrawWindow (handle, null, 0, flags);
  void redraw (boolean all) {
   if (!OS.IsWindowVisible (handle)) return;
   if (OS.IsWinCE) {
    OS.InvalidateRect (handle, null, true);
   } else {
    int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
    if (all) flags |= OS.RDW_ALLCHILDREN;
    OS.RedrawWindow (handle, null, 0, flags);
   }
  }*/
 }

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有