Display not updating while application menu is open [message #749871] |
Tue, 25 October 2011 13:41 |
Sarah Missing name Messages: 4 Registered: July 2009 |
Junior Member |
|
|
I'm running an application that uses a timer execution to update the display, creating a new composite and hiding the old one once the specified time has expired. In most cases this works fine, but if the application menu is open when the timer event fires then the new composite is not displayed. It appears that this is due to layout being called on the new composite with a flag of SWT.DEFER, meaning the layout will be deferred until display.readAndDispatch() is called. It appears that this call is not made while the menu is open. This leads me to two questions:
1.) Is it expected that display.readAndDispatch() will not be called while the application menu is open?
2.) Is there a way to programatically dismiss the open menu? I've found a couple very hacky ways to dismiss it, but don't really like them.
Here is a quick snippet that demonstrates the update behavior while the menu is open. There's a composite with a label added about every second. When the menu is not open, you'll see the labels added (up to 30). Once the menu is open, the application does not display the new labels until the menu is dismissed and then all labels added while the menu was open will be visible. If you uncomment the display.readAndDispatch() line, then the newly added labels will be visible in the display while the menu is open.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MenuTestApp {
private static int RUN_COUNT = 0;
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.DIALOG_TRIM);
GridLayout gridLayout = new GridLayout(10, true);
gridLayout.marginTop = 20;
shell.setLayout(gridLayout);
shell.setSize(800, 200);
final Menu menu = new Menu(shell, SWT.BAR);
final MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
menuItem.setText("Open Menu");
final Menu menu2 = new Menu(menuItem);
menuItem.setMenu(menu2);
final MenuItem menuItem2 = new MenuItem(menu2, SWT.CASCADE);
menuItem2.setText("Display stops updating");
shell.setMenuBar(menu);
final Runnable timer = new Runnable() {
@Override
public void run() {
++RUN_COUNT;
final Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FillLayout());
final Label label = new Label(composite, SWT.NONE);
label.setText("Updated " + RUN_COUNT);
final Control[] controls = { label };
composite.layout(controls, SWT.DEFER);
shell.layout(true, true);
shell.update();
if (RUN_COUNT < 30) {
display.timerExec(1000, this);
}
// Uncomment this line and the display will update correctly
// display.readAndDispatch();
}
};
display.timerExec(500, timer);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03088 seconds