Skip to main content

How to display data on LWUIT J2ME form?

11 replies [Last post]
johneee999
Offline
Joined: 2011-11-03
Points: 0

Hi Guys,

my app has two forms, a connection- and an output form and trying to communicate with a java server (it runs a Matlab simulation on the PC). When i start the simulation the app recieves data from the server. I would like to display the data on the output form. I created the form with the LWUIT editor, but i dont know how print the data out.
Dialog.show("Alert", "Simulation starts...", "Ok", null);
<strong>Form root1 = Display.getInstance().getCurrent();</strong>
while((time1 = in.readDouble()) != -1.0) {
String str1 = in.readUTF();
StringTokenizer strTok = new StringTokenizer(str1);
hodnotaTime = (double)(int)((Double.parseDouble(strTok.nextToken())+0.005)*100.0)/100.0;
hodnotaOutput = (double)(int)((Double.parseDouble(strTok.nextToken())+0.005)*100.0)/100.0;
hodnotaAction = (double)(int)((Double.parseDouble(strTok.nextToken())+0.005)*100.0)/100.0;
hodnotaInput = (double)(int)((Double.parseDouble(strTok.nextToken())+0.005)*100.0)/100.0;
System.out.println(&quot;Time: &quot; + hodnotaTime + &quot; Action: &quot; + hodnotaAction +  &quot; Output: &quot; + hodnotaOutput + &quot; Input: &quot; + hodnotaInput);
<strong>findTextArea(root1).setText(&quot;Time: &quot; + hodnotaTime + &quot; Action: &quot; + hodnotaAction + &quot; Output: &quot; + hodnotaOutput + &quot; Input: &quot; + hodnotaInput + &quot;\n&quot;);</strong>
}
I tried it this way but this code writes out only the last row / rewrites the previous data....
It would be the best, if i could write the data out with labels, inside a container with Box Layout Y.
Could anyone help me with this. Thank you...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07
Points: 0

Just do something like this to fix your current code:

</p>
<p><strong>TextArea t = findTextArea(root1);</strong></p>
<p><strong>t.setText(t.getText() + "\nTime: " + ...);<br />
</strong></p>
<p>

Alternatively you can just place a container within the GUI builder and leave it empty, then add the labels as you desire to that container (you can use findContainerName(...) to locate it).

This approach is demonstrated in the t-zone friends demo where we add/remove friends into a container like that. That case is more elaborate since a "friend" is a Container we designed in the GUI builder and create on the fly but you can use it as a guideline.

johneee999
Offline
Joined: 2011-11-03
Points: 0

Thank you. It works fine, but only in Netbeans emulator. :/ When I try to run the app in the phone (HTC Touch HD), the app freezes. It opens the second form and it remains empty.

I tried the LWUITDemoApp and its the same. The app runs slowly, not as smooth like here http://www.youtube.com/watch?v=eLiSzb63qWA ..

I dont know why.. Any idea?

Thanks

vprise
Offline
Joined: 2003-11-07
Points: 0

Does your app do anything besides the GUI?

Usually issues like this relate to race conditions between threads which can be caused between the lifecycle thread and the LWUIT thread. Without actually debugging the application the best advice I can give you is to just comment out code until you narrow down the curlpit.

Its also possible you get an exception/error message on the device which is failing to show due to another error, the simulator sometimes fails in replicating device error conditions properly.

johneee999
Offline
Joined: 2011-11-03
Points: 0

My app estabilishes a socket connection with a java server and send/recieves data from it. Here is a piece of my code. Maybe there is a problem with the Threads?

I am newbie in J2ME programming :/

Thank you..

vprise
Offline
Joined: 2003-11-07
Points: 0

Why are you inheriting StateMachineBase?

Only the StateMachine should do that, and it should invoke your class when its ready (in init vars or a similar method).

johneee999
Offline
Joined: 2011-11-03
Points: 0

So I updated my code. Now I inherit the StateMachineBase only in StateMachine.

This way i have access to the elements in the form, which I have open.
For example i have the connectionForm, where I have 2textareas and a button. When i press the button the following method will be processed
protected boolean onConnectionFormConnect() {
boolean val = super.onConnectionFormConnect();
Form root = Display.getInstance().getCurrent();
String serverName = findServerName(root).getText();
String serverPort = findServerPort(root).getText();
connection2Server.connect(serverName,serverPort);
return val;
If the connection is established or not, i want to write out some text to the next form (outputForm), but I dont know how to access it. It is possible to access the form if i know only its name and its not opened yet?
Thanks for your patience.
vprise
Offline
Joined: 2003-11-07
Points: 0

A form doesn't exist in RAM if its not open, so you can't access it.

Since you know about the connection in a separate thread you can either track the Form enter/exit events and when the form enters keep a pointer to the text area. When the form exits just set the pointer to null to avoid a memory leak. That way when you get the result from the server check if the pointer is not null and set the text.

Alternatively you can use something like Display.getInstance.getCurrent() to get the current form and then try to find the text area on the current form.

If the name of the text area is unique (not repeated on other forms) then findMyTextArea(Display.getInstance.getCurrent()) should return that text area or null.

mohsenof
Offline
Joined: 2011-09-26
Points: 0

Could you solve the problem johneee?

It seems you have several ways to do what you need. Change parts of your solution one by one and see if the app works by each of them.

This mechanism will help you specially if your final destination handsets are limited to few models and you can test on all of them.

johneee999
Offline
Joined: 2011-11-03
Points: 0

I tried only the LWUIT interface, where were only forms and commands to switch between them. The app was slow too... Maybe something is wrong with my phone. I dont know... I will try it on another...

johneee999
Offline
Joined: 2011-11-03
Points: 0

So I tried my app on HTC HD2 and it works fine! The problem was with my phone...

vimald
Offline
Joined: 2010-11-06
Points: 0

@johneee999, if you feel that your have received the solution for your question than please mark this question as answered.