Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 976249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:43:01+00:00 2026-05-16T03:43:01+00:00

This is a simple executable snippet that shows the issue. When using the ExpandBar

  • 0

This is a simple executable snippet that shows the issue.

When using the ExpandBar the desired outcome is to resize the window when there is a collapse or expand. It works properly on Mac but does not on Linux.

It looks like the ExpandListener is called before the collapse/expand actually occurs and therefore the pack() resizes incorrectly.

The async execution is merely a bandage to have it work on Mac but this does not work on Linux.

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;

public class ExpandBarExample {
    public static void main(String[] args) {
        Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.MIN
                | SWT.APPLICATION_MODAL);
        shell.setLayout(new FormLayout());
        shell.setText("Expand Bar");
        final ExpandBar bar = new ExpandBar(shell, SWT.NONE);
        FormData fd = new FormData();
        fd.top = new FormAttachment(0);
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        fd.bottom = new FormAttachment(100);
        bar.setLayoutData(fd);

        bar.addExpandListener(new ExpandListener() {

            public void itemCollapsed(ExpandEvent arg0) {
                Display.getCurrent().asyncExec(new Runnable() {
                    public void run() {
                        bar.getShell().pack();
                    }
                });
            }

            public void itemExpanded(ExpandEvent arg0) {
                bar.getShell().pack();

                Display.getCurrent().asyncExec(new Runnable() {
                    public void run() {
                        bar.getShell().pack();
                    }
                });
            }

        });

        Composite composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        FormLayout layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;

        composite.setLayout(layout);
        Label label = new Label(composite, SWT.NONE);
        label.setText("This is Bar 1");
        ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 0);
        item1.setText("Bar 1");
        item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item1.setControl(composite);
        item1.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar2");
        ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 1);
        item2.setText("Bar 2");
        item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item2.setControl(composite);
        item2.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar3");
        ExpandItem item3 = new ExpandItem(bar, SWT.NONE, 2);
        item3.setText("Bar 3");
        item3.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item3.setControl(composite);
        item3.setExpanded(true);

        bar.setSpacing(6);
        shell.pack();
        shell.open();
        Display display = shell.getDisplay();

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:43:02+00:00Added an answer on May 16, 2026 at 3:43 am

    I am unhappy with this solution but it works.

    Definitely a XXX solution

    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.
    – java.sun.com

    without further ado

    import org.eclipse.swt.*;
    import org.eclipse.swt.layout.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.events.ExpandEvent;
    import org.eclipse.swt.events.ExpandListener;
    
    public class ExpandBarExample {
        public static void main(String[] args) {
            Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.MIN
                    | SWT.APPLICATION_MODAL);
            shell.setLayout(new FormLayout());
            shell.setText("Expand Bar");
            final ExpandBar bar = new ExpandBar(shell, SWT.NONE);
            FormData fd = new FormData();
            fd.top = new FormAttachment(0);
            fd.left = new FormAttachment(0);
            fd.right = new FormAttachment(100);
            fd.bottom = new FormAttachment(100);
            bar.setLayoutData(fd);
    
            bar.addExpandListener(new ExpandListener() {
    
                private void resize(final ExpandEvent event, final boolean expand){
    
                    final Display display = Display.getCurrent();
    
                    new Thread(new Runnable() {
                        public void run() {
    
                            final int[] orgSize = new int[1];
                            final int[] currentSize = new int[1];
    
                            final Object lock = new Object();
    
                            if (display.isDisposed() || bar.isDisposed()){
                                return;
                            }
    
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (bar.isDisposed() || bar.getShell().isDisposed()){
                                        return;
                                    }
    
                                    synchronized(lock){
                                        bar.getShell().pack(true);
                                        orgSize[0] = bar.getShell().getSize().y;
                                        currentSize[0] = orgSize[0];
                                    }
                                }
                            });     
    
                            while (currentSize[0] == orgSize[0]){
                                if (display.isDisposed() || bar.isDisposed()){
                                    return;
                                }
    
                                display.syncExec(new Runnable() {
                                    public void run() {
    
                                        synchronized(lock){
                                            if (bar.isDisposed() || bar.getShell().isDisposed()){
                                                return;
                                            }
    
                                            currentSize[0] = bar.getShell().getSize().y;
    
                                            if (currentSize[0] != orgSize[0]){
                                                return;
                                            }
                                            else{
                                                bar.getShell().layout(true);
                                                bar.getShell().pack(true);
                                            }
                                        }
                                    }
                                });                             
                            }
                        }
                    }).start();
            }
    
            public void itemCollapsed(ExpandEvent event) {
                resize(event, false);
            }
    
            public void itemExpanded(ExpandEvent event) {        
                resize(event, true);
            }
    
            });
    
            Composite composite = new Composite(bar, SWT.NONE);
            fd = new FormData();
            fd.left = new FormAttachment(0);
            fd.right = new FormAttachment(100);
            composite.setLayoutData(fd);
    
            FormLayout layout = new FormLayout();
            layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
    
            composite.setLayout(layout);
            Label label = new Label(composite, SWT.NONE);
            label.setText("This is Bar 1");
            ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 0);
            item1.setText("Bar 1");
            item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
            item1.setControl(composite);
            item1.setExpanded(true);
    
            composite = new Composite(bar, SWT.NONE);
            fd = new FormData();
            fd.left = new FormAttachment(0);
            fd.right = new FormAttachment(100);
            composite.setLayoutData(fd);
    
            layout = new FormLayout();
            layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
            composite.setLayout(layout);
            label = new Label(composite, SWT.NONE);
            label.setText("This is Bar2");
            ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 1);
            item2.setText("Bar 2");
            item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
            item2.setControl(composite);
            item2.setExpanded(true);
    
            composite = new Composite(bar, SWT.NONE);
            fd = new FormData();
            fd.left = new FormAttachment(0);
            fd.right = new FormAttachment(100);
            composite.setLayoutData(fd);
    
            layout = new FormLayout();
            layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
            composite.setLayout(layout);
            label = new Label(composite, SWT.NONE);
            label.setText("This is Bar3");
            ExpandItem item3 = new ExpandItem(bar, SWT.NONE, 2);
            item3.setText("Bar 3");
            item3.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
            item3.setControl(composite);
            item3.setExpanded(true);
    
            bar.setSpacing(6);
            shell.pack();
            shell.open();
            Display display = shell.getDisplay();
    
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();
        }
    
    }
    

    If anyone wants to know what this is doing and doesn’t want to look at the code.

    Basically the ExpandListener looks at the original height of the shell, and issues syncExec’s to pack() the shell until the shell actually changes size. A very busy waiting approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple bash executable program (SIMPLE!!! in the repositories!) to iterate the
I want to create a binary executable for a relatively simple script that would
I realize there is likely a simple method to do this but how does
Assume I have simple program (executable compiled from a C program)that provides text information
I'm trying to build a simple unit test executable, using cpputest. I've built the
I have a VERY simple C# Winforms project that I made using Visual Studio
This simple query session = com.jthink.songlayer.hibernate.HibernateUtil.getSession(); Query q = session.createQuery(recNo from SongChanges); giving this
This simple script should theoretically check the form for errors and then print any
This simple example fails to compile in VS2K8: io_service io2; shared_ptr<asio::deadline_timer> dt(make_shared<asio::deadline_timer>(io2, posix_time::seconds(20))); As
This simple regex matching returns a string instead of an object on every browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.