Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How do I do a setText to a SWT text box ....(How do I do a setText to a SWT text box from a function?)
How do I do a setText to a SWT text box .... [message #665274] Thu, 14 April 2011 01:02 Go to next message
Bill Swanson is currently offline Bill SwansonFriend
Messages: 13
Registered: April 2011
Junior Member
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 #665286 is a reply to message #665274] Thu, 14 April 2011 06:12 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2011-04-14 03:02, Bill Swanson wrote:
> 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?

I'm not sure, what you are implying with your question. There is no need
to register any listener to the SWT text to write into the widget - why
should there be one? But you have to ensure that invoking setText
happens in the UI thread. To realize that from your event handler you
probably need to perform this change within an Display.asyncExec
invokation (use the Display that is associated to the widget).

HTH & Greetings from Bremen,

Daniel Krügler
Re: How do I do a setText to a SWT text box .... [message #665399 is a reply to message #665286] Thu, 14 April 2011 14:27 Go to previous messageGo to next message
Bill Swanson is currently offline Bill SwansonFriend
Messages: 13
Registered: April 2011
Junior Member
public static Text txtLogReadings;

public static void main(String[] args) 
	{



When I run this in the debugger, I am getting "Invalid thread access" when I try to txtLogReadings.setText("hello");
from outside the main function.

Any suggestions?
Re: How do I do a setText to a SWT text box .... [message #665407 is a reply to message #665399] Thu, 14 April 2011 14:36 Go to previous messageGo to next message
Bill Swanson is currently offline Bill SwansonFriend
Messages: 13
Registered: April 2011
Junior Member
new Thread(new Runnable() {
			      public void run() {
			         while (true) {
			            try { Thread.sleep(1000); } catch (Exception e) { }
			            Display.getDefault().asyncExec(new Runnable() {
			               public void run() {
			            			       			txtLogReadings.setText(logString.toString());
			               }
			            });
			         }
			      }
			   }).start();


Thanks for the tip. The above code works.

Bill
Re: How do I do a setText to a SWT text box .... [message #665421 is a reply to message #665399] Thu, 14 April 2011 14:29 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2011-04-14 16:27, Bill Swanson wrote:
> public static Text txtLogReadings;
>
> public static void main(String[] args) {
>
>
> When I run this in the debugger, I am getting "Invalid thread access"
> when I try to txtLogReadings.setText("hello");
> from outside the main function.
>
> Any suggestions?

Yes, as I suggested in my previous reply you need to use Display.async
and access the Text widget within the Runnable provided to that function.

HTH & Greetings from Bremen,

Daniel Krügler
Re: How do I do a setText to a SWT text box .... [message #686642 is a reply to message #665407] Wed, 22 June 2011 16:30 Go to previous messageGo to next message
Brian  is currently offline Brian Friend
Messages: 2
Registered: June 2011
Junior Member
I have a similar requirement. However, when using Display.getDefault().asyncExec(), the UI appears to be locked/unresponsive. I simply want to allow the widgets in my view to be updated by background processes while at the same time allowing me to click other controls within my application. Even though the asyncExec() method appears to start a new thread, I don't want the rest of my UI locked up.

Here is a very simple example of what I am doing...

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();			



Re: How do I do a setText to a SWT text box .... [message #687560 is a reply to message #686642] Wed, 22 June 2011 18:32 Go to previous messageGo to next message
Andi Thomas is currently offline Andi ThomasFriend
Messages: 38
Registered: July 2009
Member
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 #687593 is a reply to message #687560] Wed, 22 June 2011 19:57 Go to previous messageGo to next message
Brian  is currently offline Brian Friend
Messages: 2
Registered: June 2011
Junior Member
that did the trick - thanks!
Re: How do I do a setText to a SWT text box .... [message #687745 is a reply to message #686642] Thu, 23 June 2011 05:32 Go to previous message
Eclipse UserFriend
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
Previous Topic:Removing listeners from Scrollbar [solved]
Next Topic:Drag and Drop in Swt TreeViewer?
Goto Forum:
  


Current Time: Thu Apr 18 07:38:41 GMT 2024

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

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

Back to the top