[英]How to make automaic refresh in ScrolledComposite SWT
我有一個問題,就是在構建並打開外殼后,需要在運行時打開外殼后向外殼中添加圖形元素,而且一旦外殼打開,我將無法看到添加的新圖形元素。 新圖形元素會出現,以防萬一我調整外殼的大小。
有沒有一種方法可以解決此問題並自動查看刷新,這是一個簡單的示例:1-我在選項卡文件夾中添加了10個A按鈕,然后打開外殼。 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();
}
}
謝謝。
添加B按鈕后,必須調用tab1Comp.layout()
,因為在進行編程更改后布局不會自動更新。 您還必須更新ScrolledComposite
的最小大小,因為在編程更改后也不會調用controlResized(...)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.