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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Trusted Contributor
Himanshu_berry
Posts: 131
Registered: ‎04-19-2012
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
{
}
}

}

Please use plain text.
Developer
simon_hain
Posts: 13,796
Registered: ‎07-29-2008
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
Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 131
Registered: ‎04-19-2012
My Carrier: Nokia

Re: Start n stop the Thread

your means should display first 

then sleep.

Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 131
Registered: ‎04-19-2012
My Carrier: Nokia

Re: Start n stop the Thread

9810.jpg

it is showing only one hello look.

 

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011

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

Please use plain text.
Developer
simon_hain
Posts: 13,796
Registered: ‎07-29-2008
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
Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 131
Registered: ‎04-19-2012
My Carrier: Nokia

Re: Start n stop the Thread

:smileysad: i ll try to do course.

thanks

Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 131
Registered: ‎04-19-2012
My Carrier: Nokia

Re: Start n stop the Thread

Thanks

mr.simon

:smileyhappy:

i got the solution finally.

 

Please use plain text.
Developer
simon_hain
Posts: 13,796
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Start n stop the Thread

good to hear!
please mark the thread as solved, then :smileyhappy:
----------------------------------------------------------
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
Please use plain text.