Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem with Display.readAndDispatch(readAndDispatch returns true)
Problem with Display.readAndDispatch [message #1768891] Mon, 24 July 2017 15:55 Go to next message
Jonathan Gossage is currently offline Jonathan GossageFriend
Messages: 51
Registered: March 2010
Location: San Miguel de Allende, Gt...
Member
I am brand new with SWT and I am attempting to follow a tutorial. I am running on Ubuntu 16.04 with Eclipse Oxygen and Java Openjdk-8. I have the following code:

package org.ayudante.example.swt.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
* @author jonathan
*
*/
public class FirstSWTApplication {

/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
// Layout manager handles the layout of the widgets in the container
shell.setLayout(new FillLayout());

// TODO add some widgets to the Shell
// Shell can be used as a container
final Label label = new Label(shell, SWT.BORDER);
label.setText("This is a label"); //$NON-NLS-1$
label.setToolTipText("This is the tooltip of this label"); //$NON-NLS-1$

final Text text = new Text(shell, SWT.NONE);
text.setText("This is the text in the text widget"); //$NON-NLS-1$
text.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
text.setForeground(display.getSystemColor(SWT.COLOR_WHITE));

// set widget size to their preferred size
text.pack();
label.pack();

// Make the shell ready to run
shell.open();
// This is the dispatch loop for the window
System.out.println("About to enter dispatch loop"); //$NON-NLS-1$
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
System.out.println("Waiting for an event"); //$NON-NLS-1$
display.sleep();
}
System.out.println("About to quit"); //$NON-NLS-1$
display.dispose();
}
System.out.println("Shell has been disposed"); //$NON-NLS-1$
}
}

and when I run the application I get the following results in the console and it exits immediately:

About to enter dispatch loop
About to quit
Shell has been disposed

I can find nothing that explains what I should do if readAndDispatch returns true and I need help deciding how to proceed.

Re: Problem with Display.readAndDispatch [message #1768910 is a reply to message #1768891] Mon, 24 July 2017 20:48 Go to previous message
Jonathan Gossage is currently offline Jonathan GossageFriend
Messages: 51
Registered: March 2010
Location: San Miguel de Allende, Gt...
Member
I discovered that the event loop was incorrectly displayed in the tutorial. Moving the display.close() statement one level father out where the print statement for shell termination was solved the problem. The application was starting up with pending events that needed to be dispatched.
Previous Topic:Unable to get Cascade menu attached to Bar Menu in Eclipse Oxygen
Next Topic:Gray out disabled menus under Linux
Goto Forum:
  


Current Time: Mon Dec 11 13:08:11 GMT 2017

Powered by FUDForum. Page generated in 0.01663 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software