SWT Threads [message #630986] |
Tue, 05 October 2010 18:39 |
larsk Messages: 22 Registered: October 2009 Location: Sweden |
Junior Member |
|
|
I am creating an application in Java with SWT where I have a main shell from where I can open several other shells. Each new shell is created in a new thread and opened from a button on the main shell.
But when one of the other shells is working, the other shells is unresponsive.
The code is something like the following code but when I press the button, a new shell is created and opened. I just created this to show that the list isn't updating when clicking in it while the other thread is working.
Is there a way to make the other shells responsive while one shell is working?
package threadtest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
public class Main extends Shell
{
List list = new List (this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
Button buttonStart = new Button(this, SWT.NONE);
public static void main(String[] args)
{
Main main = new Main();
}
Main()
{
super(Display.getDefault(), SWT.CLOSE | SWT.TITLE | SWT.MIN);
this.setText("ThreadTest");
this.setBounds(150, 150, 200, 340);
buttonStart.setText("Start");
buttonStart.setLocation(100, 270);
buttonStart.pack();
buttonStart.addSelectionListener(startListener);
list.setBounds(10, 10, 150, 250);
for (int index = 0; index < 10; index++)
list.add("Item " + index);
this.open();
while (!this.isDisposed())
{
if (!Display.getDefault().readAndDispatch())
Display.getDefault().sleep();
}
}
@Override
protected void checkSubclass()
{
// Disable the check that prevents subclassing of SWT components
}
SelectionListener startListener = new SelectionListener()
{
@Override
public void widgetSelected(SelectionEvent event)
{
Thread threadStart;
threadStart = new Thread(startRunnable);
threadStart.start();
}
@Override
public void widgetDefaultSelected(SelectionEvent event)
{
}
};
Runnable startRunnable = new Runnable()
{
@Override
public void run()
{
Display.getDefault().asyncExec (new Runnable ()
{
@Override
public void run ()
{
for (int index = 0; index < 100000; index++)
System.out.println("index: " + index);
System.out.println("Done");
}
});
}
};
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03144 seconds