2012-09-06 108 views
3

我在Java和SWT中創建了一個應用程序,我遇到了問題。我想把ExpandBar放到GridLayout中。一切都很好,而我不折疊擴展欄。當我這樣做時,它通常會碰撞,但複合材料沒有重新佈局。下面的屏幕應該解釋我的問題:ExpandBar與GridLayout工作不正確

崩潰之前:
Before collapse
崩潰後:
After collapse

我嘗試了一些技巧,例如像:

bar.addExpandListener(new ExpandListener() { 
    public void itemExpanded(ExpandEvent e) { 
     item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); 
     gridData2.heightHint = composite.computeSize(SWT.DEFAULT, 
       SWT.DEFAULT).y; 
     comp.layout(true, true); 
    } 

    public void itemCollapsed(ExpandEvent e) { 
     item0.setHeight(item0.getHeaderHeight()); 
     gridData2.heightHint = item0.getHeaderHeight(); 
     comp.layout(true, true); 
    } 
}); 

Altough它不」工作。這裏是SSCCE:

package stackoverflow; 

import org.eclipse.jface.window.Window; 
import org.eclipse.swt.SWT; 
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.Control; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.ExpandBar; 
import org.eclipse.swt.widgets.ExpandItem; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Tree; 

public class MainWindowSWT extends Window { 

    public MainWindowSWT() { 
     super((Shell)null); 

     setBlockOnOpen(true); 
     open(); 
     Display.getCurrent().dispose(); 
    } 

    @Override 
    protected Control createContents(Composite parent) { 
     final Composite comp = new Composite(getShell(), 0); 

     GridLayout layout = new GridLayout(1, true); 
     comp.setLayout(layout); 

     Tree tree = new Tree(comp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); 
     GridData gridData = new GridData(); 
     gridData.verticalAlignment = GridData.FILL; 
     gridData.verticalSpan = 2; 
     gridData.grabExcessVerticalSpace = true; 
     gridData.horizontalAlignment = GridData.FILL; 
     gridData.grabExcessHorizontalSpace = true; 
     tree.setLayoutData(gridData); 

     final ExpandBar bar = new ExpandBar(comp, SWT.V_SCROLL); 
     final Composite composite = new Composite(bar, SWT.NONE); 
     GridLayout gridLayout = new GridLayout(); 
     gridLayout.verticalSpacing = 10; 
     composite.setLayout(gridLayout); 
     Button button = new Button(composite, SWT.PUSH); 
     button.setText("SWT.PUSH"); 
     final ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0); 
     item0.setText("Test"); 
     item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); 
     item0.setControl(composite); 

     item0.setExpanded(true); 

     final GridData gridData2 = new GridData(); 
     gridData2.horizontalAlignment = GridData.FILL; 
     gridData2.grabExcessHorizontalSpace = true; 
     bar.setLayoutData(gridData2); 

     return parent; 
    } 
} 

Ubuntu 12.04.1 amd64,SWT 3.7.2。

在此先感謝。

//順便說一句:我不想調整shell的大小。我想調整展開欄上方的樹的大小。

回答

1

我得到它的工作,但我不滿意這個解決方案...

bar.addExpandListener(new ExpandAdapter() { 

    @Override 
    public void itemCollapsed(ExpandEvent e) { 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        synchronized (this) { 
         wait(250); 
        } 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Display.getDefault().asyncExec(new Runnable() { 
        public void run() { 
         layout(); 

        } 
       }); 
      } 
     }).start(); 
    } 

    @Override 
    public void itemExpanded(ExpandEvent e) { 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        synchronized (this) { 
         wait(250); 
        } 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Display.getDefault().asyncExec(new Runnable() { 
        public void run() { 
         layout(); 

        } 
       }); 
      } 
     }).start(); 
    } 
}); 
2

好了,我找到了一個解決方案,是一個小哈克,但它的工作原理:

public class MainWindowSWT { 

    public static void main(String[] args) { 
     final Display display = new Display(); 
     final Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     shell.setText("ExpandBar Example"); 
     final ExpandBar bar = new ExpandBar(shell, SWT.NONE); 

     final Composite composite = new Composite(bar, SWT.NONE); 
     GridLayout layout = new GridLayout(); 
     composite.setLayout(layout); 
     Button button = new Button(composite, SWT.PUSH); 
     button.setText("SWT.PUSH"); 
     ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0); 
     item0.setText("What is your favorite button"); 
     item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); 
     item0.setControl(composite); 

     item0.setExpanded(true); 

     bar.setSpacing(8); 

     bar.addExpandListener(new ExpandListener() { 

      @Override 
      public void itemExpanded(ExpandEvent e) { 
       if (e.item instanceof ExpandItem) { 
        ExpandItem item = (ExpandItem) e.item; 
        shell.setSize(shell.getSize().x, shell.getSize().y + item.getHeight()); 
       } 
      } 

      @Override 
      public void itemCollapsed(ExpandEvent e) { 
       if (e.item instanceof ExpandItem) { 
        ExpandItem item = (ExpandItem) e.item; 
        shell.setSize(shell.getSize().x, shell.getSize().y - item.getHeight()); 
       } 
      } 
     }); 
     shell.pack(); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
     display.dispose(); 
    } 
} 

基本上,你強制shell增大/減小其高度,以反映這些變化。我希望有一種更簡單/更漂亮的方式,但現在我無法想到任何事情。

部分基於this答案,但沒有使用線程。

+0

嗯...但我不想改變外殼的大小...有什麼辦法解決我的問題,而無需調整窗口大小? – m4tx

+0

那麼,你想調整大小呢? – Baz

+0

我想調整ExpandBar上方的樹的大小:)對不起,忘了提及這一點。 – m4tx