Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

[ Edited ]

i am doing this to print "hello " 10 times in  my textbox but it prints only one time.

plz check this

 

class ThreadUi extends MainScreen implements FieldChangeListener
{
public ButtonField start, stop;
public TextBoxField txtfield =null;
public String str = "";
public ThreadUi()
{
ThreadScreen();
}

public void ThreadScreen()
{
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
hfm.setMargin(40,0,0,0);
start = new ButtonField("Start");
start.setMargin(0,10,0,0);
stop = new ButtonField("Stop");
hfm.add(start);
hfm.add(stop);
add(hfm);

txtfield = new TextBoxField(400,400);
txtfield.setMargin(20,0,0,20);
add(txtfield);


start.setChangeListener(this);
stop.setChangeListener(this);
}

public void startThread()
{
Thread th = new Thread()
{
public void run()
{
try
{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException e){}
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
for(int i = 0; i<10;i++)
{
str = "hello";
txtfield.setText(str);
}

}
});
}
};
th.start();
}

public void fieldChanged(Field field, int context)
{
if(field == start)
{
startThread();
}

else if(field == stop)
{

}

else
{
}
}

}

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Start n stop the Thread

well, your thread sleeps for 1 second, then sets "hello" to the textfield 10 times in a row.
maybe you should take a second look at your process flow...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

your means should display first 

then sleep.

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

9810.jpg

it is showing only one hello look.

 

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Start n stop the Thread

Read carefully simon's post.

 

You update the same(!!!) text field with the same text so it doesn't really change...

 

Try adding more text fileds or changing the value of the text field to include the old text with the new text:

setText(getText() + "\n" + "hello");

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Start n stop the Thread

no, i mean your code does not make any sense.
if you call setText it sets the text of the field. if you do that 10 times in a row it still gets the same result. you could use "hello"+i, that way it would at least print the index with it.

if you want to sleep between the setText statements you have to put the for loop on the outside, and a sleep and invokelater on the inside.

this forum is mainly for the specialities of blackberry java, if you want to learn java development i suggest to take a look at some online courses, starting on BB directly is quite difficult.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

Smiley Sad i ll try to do course.

thanks

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

Thanks

mr.simon

Smiley Happy

i got the solution finally.

 

Highlighted
Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Start n stop the Thread

good to hear!
please mark the thread as solved, then Smiley Happy
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter