How do I do a setText to a SWT text box .... [message #665274] |
Wed, 13 April 2011 21:02  |
Eclipse User |
|
|
|
I have created a SWT text widget in the main function. I can do a setText within the main function with no problem. But I have an event driven function that writes data to a variable which I would like to display in the text box.
Do I need to add a listener to the SWT text to write the variable to the text?
|
|
|
|
|
|
|
|
Re: How do I do a setText to a SWT text box .... [message #687560 is a reply to message #686642] |
Wed, 22 June 2011 14:32   |
Eclipse User |
|
|
|
The problem is that you are sleeping the UI thread in your for loop.
This should work, and I've attached a full example.
Thread t = new Thread() {
public void run() {
for (int index = 0; index < 30; index++) {
final int i = index;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
swtLabel.setText("" + i);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
On 6/22/11 12:30 PM, Brian wrote:
>
>
> Thread t = new Thread () {
> public void run () {
> Display.getDefault().asyncExec(new Runnable(){
> public void run () {
> try {
> for ( int index = 0; index < 10; index++ ) {
> swtLabel.setText("" + index);
> Thread.sleep(1000);
> }
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> });
> }
> };
> t.start();
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class BackgroundTesting {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Label swtLabel = new Label(shell, SWT.SINGLE | SWT.LEAD);
swtLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
final Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
false));
button.setText("Click Me");
final Text text = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.LEAD | SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
text.setEnabled(false);
text.setEditable(false);
final Thread t = new Thread() {
public void run() {
for (int index = 0; index < 30; index++) {
final int i = index;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
swtLabel.setText("" + i);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
t.start();
button.setEnabled(false);
text.setEnabled(true);
text.setEditable(true);
text.setText("Start Typing");
text.selectAll();
text.setFocus();
}});
shell.setSize(400,200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
|
|
|
Re: How do I do a setText to a SWT text box .... [message #687745 is a reply to message #686642] |
Thu, 23 June 2011 01:32  |
Eclipse User |
|
|
|
Originally posted by:
On 2011-06-22 18:30, Brian wrote:
> Even though the asyncExec() method appears to start a new thread
This is definitively an incorrect interpretation, asyncExec does *not*
start a new thread, it only realizes that the provided runnable is added
to the event queue returning immediately, not waiting for the runnable
to be executed (in contrast to syncExec, which waits until the runnable
has been executed).
HTH & Greetings from Bremen,
Daniel Krügler
|
|
|
Powered by
FUDForum. Page generated in 0.07891 seconds