在ScrolledComposite中调整扩展栏SWT的大小

时间:2014-01-29 10:30:34

标签: java swt scrolledcomposite

我正在使用scrolledcomposite中的扩展栏构建一个java SWT独立应用程序。在某些时候,我会在其中一个组件中添加一个字段。因此,项目的大小将增长。首先,我销毁扩展栏中的所有当前项目:

 public void closeAll(){
    for(int a = bar.getItemCount()-1; a>=0; a--){
        bar.getItem(a).getControl().dispose();
        bar.getItem(a).dispose();
    }
}

然后我会再次阅读它们(我将复合材料存储在其他地方)。我重新计算每件物品的高度:

for(int a = 0; a<bar.getItemCount(); a++){
        bar.getItem(a).setHeight(bar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);

    }

当我这样做时,滚动的复合材料将不会向下滚动(扩展栏的一部分现在位于ScrolledComposite的客户区域之外)。

当我调整窗口大小时,所有问题都解决了。那么我怎么能强制这个更新通常与没有代码的窗口大小调整相关联?

提前谢谢大家!

//修改

我已经添加了我正在谈论的课程:

public class InputPanel extends Panel implements Refresh{

private Editor parent;
private ExpandBar expandBar;

private ArrayList<InputBlock> blocks;
public ScrolledComposite scroll;



public InputPanel(Editor parent, Composite panel) {
    super(panel);
    this.parent = parent;
    setGridLayout(0,0,1);
    blocks = new ArrayList<InputBlock>();
    init();
}
private void init(){
    //two pannels first button pannel
    Panel buttonPanel = new Panel(new Composite(panel, SWT.BORDER));
    buttonPanel.setGridLayout(0, 0, 3);
    buttonPanel.fillGridData();

    Composite barComp = new Composite(panel, SWT.BORDER);

    barComp.setLayout(new FillLayout());
    GridData data1 = new GridData();
    data1.grabExcessHorizontalSpace = true;
    data1.horizontalAlignment = SWT.FILL;
    data1.verticalAlignment = SWT.FILL;
    barComp.setLayoutData(data1);

    scroll = new ScrolledComposite(barComp, SWT.V_SCROLL);
    expandBar = new ExpandBar(scroll, SWT.None);
    GridData data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;
    data.horizontalAlignment = SWT.FILL;
    data.verticalAlignment = SWT.TOP;

    scroll.setContent(expandBar);
    scroll.setExpandVertical(true);
    scroll.setExpandHorizontal(true);
    scroll.setAlwaysShowScrollBars(true);
    for(int a = 0; a<Main.database.size(); a++){
        Composite composite = new Composite(expandBar, SWT.BORDER);
        blocks.add(new InputBlock(this, composite, Main.database.get(a)));
        addItem(composite, Main.database.get(a).name);
    }
    refreshHeights();
    scroll.setMinSize(expandBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scroll.layout(true);
}
public void addItem(Composite composite, String tabName){
    ExpandItem expandItem = new ExpandItem(expandBar, SWT.None);
    expandItem.setText(tabName);
    expandItem.setControl(composite);
    expandItem.setExpanded(true);
    composite.setVisible(true);
}
public void refreshHeights(){
    for(int a = 0; a<expandBar.getItemCount(); a++){
        expandBar.getItem(a).setHeight(expandBar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    }

}
public void closeAll(){
    ExpandItem[] items = expandBar.getItems();
    System.out.println(items.length);

    for(int a = items.length-1; a>=0; a--){
        items[a].getControl().dispose();
        items[a].dispose();
    }
}


public void refresh(){
    closeAll();
    blocks = new ArrayList<InputBlock>();
    for(int a = 0; a<Main.database.size(); a++){
        Composite composite = new Composite(expandBar, SWT.BORDER);
        blocks.add(new InputBlock(this, composite, Main.database.get(a)));
        addItem(composite, Main.database.get(a).name);
    }
    refreshHeights();
    scroll.setMinSize(expandBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scroll.layout(true);
}

public Editor getParent(){
    return parent;
}


public void setEditorMenu(boolean show){
    for(int a = 0; a<blocks.size(); a++){
        blocks.get(a).setEditorMenu(show);
    }
}



public ArrayList<InputBlock> getBlocks(){
    return blocks;
}
public boolean setValue(DatabaseField field, String value){
    value = value.trim();
    for(int a = 0; a<blocks.size(); a++){
        if(blocks.get(a).setValue(field, value)){
            return true;
        }
    }
    return false;
}

}

我试图做一个例子,但是这个例子开箱即用。我看不出有什么区别。 InputBlocks是保存有关复合中所需字段的数据的类,如果调用它们将生成复合。

持有ScrolledComposite的合成目前是SashForm,我不知道这会有什么不同吗?

我得到了这个错误(仅限Linux),但在我尝试的示例中没有:

SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
(SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
(SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed

更新

我为我的问题做了一个更好的例子。此代码正在运行并为我生成相同的错误:

(SWT:6765): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed

我用过这段代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
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.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;


 public class ExpandBarExample implements SelectionListener{



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

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

private  ExpandBar bar;
private  ScrolledComposite scroll;
private  Composite composite;
private SashForm sash;
private CTabFolder folder;
private Button button;

public ExpandBarExample(Composite composite){
    this.composite = composite;

    init();
}
public void init(){


    composite.setLayout(new FillLayout());
    sash = new SashForm(composite, SWT.VERTICAL);

    folder = new CTabFolder(sash, SWT.None);


    scroll = new ScrolledComposite(folder, SWT.V_SCROLL);
    GridData data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;

    bar = new ExpandBar(scroll, SWT.NONE);
    scroll.setContent(bar);
    scroll.setExpandVertical(true);
    scroll.setExpandHorizontal(true);
    scroll.setAlwaysShowScrollBars(true);



    CTabItem tab = new CTabItem(folder, SWT.None);
    tab.setControl(scroll);
    tab.setText("TEST");

    Composite sashFill = new Composite(sash, SWT.BORDER);
    sashFill.setLayout(new FillLayout());
    button = new Button(sashFill, SWT.PUSH);
    button.addSelectionListener(this);
    button.setText("Add block");

    sash.setWeights(new int[]{3,1});

    for(int a = 0; a<10; a++){
        Composite composite = new Composite(bar, SWT.BORDER);

        composite.setLayout(new GridLayout(2, false));

        for(int b = 0; b<10; b++){
            Label label = new Label(composite, SWT.BORDER);
            label.setText("TEST");
        }

        addItem(composite, "Item " + a);
    }
    computeHeights();
    scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scroll.layout(true);



    //delete old panels
    removeAll();
    //trying to add more panels

    for(int a = 0; a<10; a++){
        Composite composite = new Composite(bar, SWT.BORDER);


        composite.setLayout(new GridLayout(2, false));

        for(int b = 0; b<20; b++){
            Label label = new Label(composite, SWT.BORDER);
            label.setText("TEST");
        }

        addItem(composite, "Item " + a);
    }


    computeHeights();
    scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scroll.layout(true);

}
public void refresh(){

}
public void removeAll(){
    ExpandItem[] items = bar.getItems();
    System.out.println(items.length);

    for(int a = items.length-1; a>=0; a--){
        items[a].getControl().dispose();
        items[a].dispose();
    }
}
public void addItem(Composite composite, String tabName){
    ExpandItem expandItem = new ExpandItem(bar, SWT.None);
    expandItem.setText(tabName);
    expandItem.setControl(composite);
    expandItem.setExpanded(true);
}
public void computeHeights(){
    for(int a = 0; a<bar.getItemCount(); a++){
        bar.getItem(a).setHeight(bar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    }
}
@Override
public void widgetSelected(SelectionEvent e) {
    if(e.getSource() == button){
        Composite comp = new Composite(bar, SWT.BORDER);
        comp.setLayout(new GridLayout(2, false));
        for(int a = 0; a<10; a++){
            Label label = new Label(comp, SWT.BORDER);
            label.setText("TEST");
        }
        comp.layout(true);
        addItem(comp, "ADDED");
        computeHeights();

        scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));

        scroll.layout(true, true);
    }

}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
    // TODO Auto-generated method stub

}

}

我试图在Ubuntu和OpenSuse上运行它,两者都给出了相同的Gtk错误。我希望有人可以帮助我!

2 个答案:

答案 0 :(得分:1)

实际上1毫秒总是足够的!

expandBar.addExpandListener(new ExpandAdapter() {
        @Override
        public void itemExpanded(ExpandEvent e) {
            resetScrolledCompositeMinSize();
        }
        @Override
        public void itemCollapsed(ExpandEvent e) {
            resetScrolledCompositeMinSize();
        }
        private void resetScrolledCompositeMinSize() {
            Display.getDefault().timerExec(1, new Runnable() //1 ms is more than enough!!
            {
                @Override
                public void run()
                {
                    scrolledComposite.setMinSize(expandBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                }
            });
        }
    });

答案 1 :(得分:0)

好吧,我找到了解决方法。它不是一个解决问题的解决方案,而是一个避免它的东西。

您的问题似乎是由于展开ExpandItem时发生的延迟造成的。因此,更新minSize的代码使用未展开的大小。在设置minSize之前添加延迟可以解决您的问题。尝试使用延迟值,但300毫秒似乎工作正常,并没有真正干扰可用性:

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

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

private ExpandBar           bar;
private ScrolledComposite   scroll;
private Composite           composite;
private SashForm            sash;
private CTabFolder          folder;
private Button              button;

public ExpandBarExample(Composite composite)
{
    this.composite = composite;

    init();
}

public void init()
{

    composite.setLayout(new FillLayout());
    sash = new SashForm(composite, SWT.VERTICAL);

    folder = new CTabFolder(sash, SWT.None);

    scroll = new ScrolledComposite(folder, SWT.V_SCROLL);
    GridData data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;

    bar = new ExpandBar(scroll, SWT.NONE);
    scroll.setContent(bar);
    scroll.setExpandVertical(true);
    scroll.setExpandHorizontal(true);
    scroll.setAlwaysShowScrollBars(true);

    CTabItem tab = new CTabItem(folder, SWT.None);
    tab.setControl(scroll);
    tab.setText("TEST");

    Composite sashFill = new Composite(sash, SWT.BORDER);
    sashFill.setLayout(new FillLayout());
    button = new Button(sashFill, SWT.PUSH);
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Composite comp = new Composite(bar, SWT.BORDER);
            comp.setLayout(new GridLayout(2, false));
            for (int a = 0; a < 10; a++)
            {
                Label label = new Label(comp, SWT.BORDER);
                label.setText("TEST");
            }
            comp.layout(true);
            addItem(comp, "ADDED");

            scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
            scroll.layout(true, true);
        }
    });
    button.setText("Add block");

    sash.setWeights(new int[] { 3, 1 });

    scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scroll.layout(true);
}

public void refresh()
{

}

public void removeAll()
{
    ExpandItem[] items = bar.getItems();
    System.out.println(items.length);

    for (int a = items.length - 1; a >= 0; a--)
    {
        items[a].getControl().dispose();
        items[a].dispose();
    }
}

public void addItem(Composite composite, String tabName)
{
    final ExpandItem expandItem = new ExpandItem(bar, SWT.None);
    expandItem.setText(tabName);
    expandItem.setControl(composite);
    expandItem.setExpanded(true);
    expandItem.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);

    Display.getDefault().timerExec(300, new Runnable()
    {
        @Override
        public void run()
        {
            scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
            scroll.layout(true, true);
        }
    });
}