eclipse swt ExpandBar:扩展/折叠事件的大小错误

时间:2018-03-15 16:17:54

标签: java linux eclipse swt

我在eclipse SWT中有以下布局:一个GridLayout,一列和两个孩子,一个在顶部,一个在底部。最下面的是Expandpar。展开或折叠ExpandBar时,应更新两个子项的大小。这是代码:

package tests.julia.layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class MyLayoutExpandBar {
    public static void main (String [] args) {
        final Display display = new Display ();
        Shell shell = new Shell (display);
        shell.setLayout (new FillLayout());

        final Composite parent = new Composite (shell, SWT.BORDER);
        parent.setLayout(new GridLayout());

        Composite comp = new Composite(parent, SWT.BORDER);
        comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final ExpandBar expandBar = new ExpandBar (parent, SWT.V_SCROLL);
        expandBar.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));

        ExpandItem item = new ExpandItem (expandBar, SWT.NONE);
        item.setText("Expand Item");
        item.setHeight(200);
        item.setExpanded(true);

        Group group = new Group(expandBar, SWT.NONE);
        group.setText("Item Content");
        item.setControl(group);


        /* Listener */
        expandBar.addListener(SWT.Expand, new Listener() {

            @Override
            public void handleEvent(Event event) {
                display.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        System.out.println("Expand: " + expandBar.getSize());
                        parent.layout();
                        System.out.println("Expand: " + expandBar.getSize());
                    }
                });
            }
        });
        expandBar.addListener(SWT.Collapse, new Listener() {

            @Override
            public void handleEvent(Event event) {
                display.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        System.out.println("Collapse: " + expandBar.getSize());
                        parent.layout();
                        System.out.println("Collapse: " + expandBar.getSize());
                    }
                });
            }
        });
        shell.setText("Shell");
        shell.setSize(300, 300);
        shell.open ();

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

这在Windows下工作正常,但在Linux下,ExpandBar的大小在展开时很小,在折叠时很大,所以恰好与它应该相反。调整Shell大小(通过拖动其边框)后,ExpandBar的大小变为正确。

有谁知道如何做到这一点?

谢谢朱莉娅

2 个答案:

答案 0 :(得分:0)

这件事最终可能是Ubuntu唯一的错误。 请参阅此错误报告 https://bugs.eclipse.org/bugs/show_bug.cgi?id=532546

答案 1 :(得分:0)

类似于this的变通办法似乎正在解决此问题。

我直接在ExpandBar上使用Shell,并在其顶部放置垂直RowLayout并将followong ExpandListener添加到栏中为我解决了问题:

expandBar.addExpandListener(new ExpandListener() {

    @Override
    public void itemExpanded(ExpandEvent e) {
        changed(e, true);
    }

    @Override
    public void itemCollapsed(ExpandEvent e) {
        changed(e, false);
    }

    private void changed(ExpandEvent e, boolean expand) {
        if (e.item instanceof ExpandItem) {
            ExpandItem expItem = (ExpandItem) e.item;
            expandBar.setLayoutData(new RowData(expandBar.getSize().x,
                    expandBar.getSize().y + (expand ? expItem.getHeight() : -expItem.getHeight())));
            shell.layout();
        }
    }
});