process bar in j2me

Hello friends,

I want to know how we are able to implements a process bar in j2me.I want to show when we click on the button it will show that process bar until you got the response from the server through internet.

Please any body help me or give me source code for that . i am not able to do that..

[319 byte] By [anup123a] at [2007-11-26 19:39:18]
# 1
show us your source code who's trying to do what you wanna do and we will help you !
suparenoa at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.io.*;

import java.lang.*;

import java.lang.String.*;

import javax.microedition.rms.*;

import javax.microedition.io.*;

import java.util.*;

class Menu extends Form implements CommandListener,Runnable

{

public MobileDemo appl;

private TextField Username,Password;

private TextField[] textfield = new TextField[10];

private DateField[] datefield = new DateField[10];

private Command exitCommand;

private Command loginCommand;

private String username,password;

private RecordStore loginRecord,loginRecord1;

private String routeLocatorURL,message ="";

private HttpConnection connection;

private DataInputStream dis = null;

private String StringSeparator="|";

private String SubStringSeparator=",";

private String strSubString ="";

private String strSubSubString ="";

private String controlId="";

private String controlName="";

private String controlValue="";

private String returnValue="";

private String RBGroup="";

private String CBGroup="";

private String ListGroup="";

private String strHidden = "";

private String strCheck = "";

private String returnOption="";

private String returnOption1="";

private String LabelValue="";

private String TextFieldValue = "";

private String MyLabel="";

private String monthdate="";

private String StringValue="";

private String ActionIdString="";

private String LsActionString="";

private String RsActionString="";

private String CsActionString="";

static final String CB = "CB";

static final String TB = "TB";

static final String RB = "RB";

static final String LS = "LS";

static final String CS = "CS";

static final String RS = "RS";

static final String MO = "MO";

static final String LB = "LB";

static final String PB = "PB";

static final String HD = "HD";

static final String CL = "CL";

private ChoiceGroup menuChoiceGroup,menuChoiceGroup1;

int ch,l=0,i=0,h=0,m=0;

private Command leftButton;

private Command rightButton;

private Command centerButton;

private List list;

private RecordStore rs = null;

private static Image image = null;

private Form sendingForm;

//private Form senderForm = new Form("SENDING DATA");

private Gauge sendingItem = new Gauge("SENDING DATA WAIT.........", false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);

//private Gauge sendingItem = new Gauge("", false, 256, 0);

public Menu(MobileDemo mid)

{

super("....Mobile Application....");

appl=mid;

Username=new TextField("User ID:-", null, 25, TextField.ANY);

Password=new TextField("Password:-", null, 25, TextField.PASSWORD);

loginCommand = new Command("Login",Command.OK,1);

exitCommand = new Command("Exit",Command.EXIT, 1);

try

{

image = Image.createImage("/_single8.png");

}

catch(Exception e)

{

//System.out.println(e);

}

sendingForm = new Form("");

sendingForm.append(sendingItem);

append(Username);

append(Password);

addCommand(loginCommand);

addCommand(exitCommand);

appl.display.setCurrent(this);

setCommandListener(this);

}

public void commandAction(Command command,Displayable displayable)

{

if(command == loginCommand)

{

appl.display.setCurrent(this.sendingForm);

this.deleteAll();

this.removeCommand(exitCommand);

this.removeCommand(loginCommand);

username=Username.getString();

password=Password.getString();

//System.out.println("="+username);

// System.out.println("="+password);

BuildURL();

new Thread(this).start();

}

if(command == exitCommand)

{

appl.destroyApp(true);

appl.notifyDestroyed();

}

if(command==leftButton)

{

if(command==leftButton)

{

message="";

appl.display.setCurrent(this.sendingForm);

try

{

loginRecord = RecordStore.openRecordStore("LogInRecord",true);

byte [] UrlByte = loginRecord.getRecord(1);

loginRecord.closeRecordStore();

String url = new String(UrlByte);

routeLocatorURL = "http://"+url+"?uid="+username+"";

routeLocatorURL = routeLocatorURL + LsActionString;

IterateItem();

while(strHidden.length() > 1)

{

String substrHidden = GetSubString(strHidden,StringSeparator);

strHidden = GetRemString(strHidden,StringSeparator);

routeLocatorURL = routeLocatorURL + substrHidden; // Finally add the hidden values

}

// System.out.println("main menu:-->"+routeLocatorURL);

// deleteRMS();

this.deleteAll();

this.removeCommand(leftButton);

this.removeCommand(rightButton);

this.removeCommand(centerButton);

new Thread(this).start();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

if(command==rightButton)

{

//System.out.println("ActionIdString"+ActionIdString);

if(ActionIdString.equals("00")==true)

{

//System.out.println("dest");

appl.destroyApp(true);

appl.notifyDestroyed();

// System.out.println("dest");

}

else

{

message="";

try

{

appl.display.setCurrent(this.sendingForm);

loginRecord = RecordStore.openRecordStore("LogInRecord",true);

byte [] UrlByte = loginRecord.getRecord(1);

loginRecord.closeRecordStore();

String url = new String(UrlByte);

routeLocatorURL = "http://"+url+"?uid="+username+"";

routeLocatorURL = routeLocatorURL + RsActionString;

IterateItem();

while(strHidden.length() > 1)

{

String substrHidden = GetSubString(strHidden,StringSeparator);

strHidden = GetRemString(strHidden,StringSeparator);

routeLocatorURL = routeLocatorURL + substrHidden; // Finally add the hidden values

}

//System.out.println("main menu:-->"+routeLocatorURL);

this.deleteAll();

this.removeCommand(leftButton);

this.removeCommand(rightButton);

this.removeCommand(centerButton);

new Thread(this).start();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

if(command==centerButton)

{

message="";

try

{

appl.display.setCurrent(this.sendingForm);

loginRecord = RecordStore.openRecordStore("LogInRecord",true);

byte [] UrlByte = loginRecord.getRecord(1);

loginRecord.closeRecordStore();

String url = new String(UrlByte);

routeLocatorURL = "http://"+url+"?uid="+username+"";

routeLocatorURL = routeLocatorURL + CsActionString;

//System.out.println("main menu:-->"+routeLocatorURL);

IterateItem();

while(strHidden.length() > 1)

{

String substrHidden = GetSubString(strHidden,StringSeparator);

strHidden = GetRemString(strHidden,StringSeparator);

routeLocatorURL = routeLocatorURL + substrHidden; // Finally add the hidden values

}

//System.out.println("main menu:-->"+routeLocatorURL);

this.deleteAll();

this.removeCommand(leftButton);

this.removeCommand(rightButton);

this.removeCommand(centerButton);

new Thread(this).start();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

/*void showProgress(String label, int maxValue) {

sendingItem.setLabel(label);

sendingItem.setValue(0);

sendingItem.setMaxValue(maxValue);

Display.getDisplay(appl).setCurrent(senderForm);

}

void updateProgress(int value) {

sendingItem.setValue(value);

}*/

public void run()

{

try

{

establishConnection();

BuildScreen(message);

appl.display.setCurrent(this);

setCommandListener(this);

}

catch(Exception f)

{

//System.out.println(f.getMessage());

}

}

public void BuildURL()

{

try

{

loginRecord = RecordStore.openRecordStore("LogInRecord",true);

byte [] UrlByte = loginRecord.getRecord(1);

loginRecord.closeRecordStore();

String url = new String(UrlByte);

routeLocatorURL = "http://"+url+"?aid=01&uid="+username+"&pwd="+password+"&sid=CSE00";

//System.out.println("in menu for login-->"+routeLocatorURL);

}

catch(Exception e)

{

//System.out.println("some problem");

e.printStackTrace();

}

}

public void establishConnection()

{

try

{

connection = (HttpConnection)Connector.open(routeLocatorURL);

dis = new DataInputStream(connection.openInputStream());

/* System.out.println(""+ dis.available());

showProgress("Connecting ...", dis.available());

updateProgress(dis.available());*/

}

catch (Exception e)

{

//System.out.println("some problem");

e.printStackTrace();

}

try

{

while((ch = dis.read()) != -1)

{

message = message + (char) ch;

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

private void BuildScreen(String message)

{

strHidden="";

i=0;

l=0;

h=0;

m=0;

while(message.length() > 1)

{

strSubString = GetSubString(message,StringSeparator);

BuildControls(strSubString);

message = GetRemString(message,StringSeparator);

//System.out.println(message);

}

// closeRMS();

CheckRadioButtonStatus("");

CheckBOxStatus("");

CheckListGroupStatus("");

}

private void BuildControls(String strSubString)

{

controlId = GetSubString(strSubString,SubStringSeparator);

strSubString = GetRemString(strSubString,SubStringSeparator);

controlName = GetSubString(strSubString,SubStringSeparator);

strSubString = GetRemString(strSubString,SubStringSeparator);

controlValue = GetSubString(strSubString,SubStringSeparator);

strSubString = GetRemString(strSubString,SubStringSeparator);

returnValue = GetSubString(strSubString,SubStringSeparator);

// System.out.println(controlId);

CheckRadioButtonStatus(controlId);

CheckBOxStatus(controlId);

CheckListGroupStatus(controlId);

if(controlId.equals(CB)==true)

{

MakeCheckBox(controlName, controlValue);

}

else if(controlId.equals(TB)==true)

{

MakeTextBox(controlName, controlValue);

}

else if(controlId.equals(RB)==true)

{

MakeRadioButton(controlName, controlValue, returnValue);

}

else if(controlId.equals(LS)==true)

{

MakeLeftButton(controlName, controlValue ,returnValue);

}

else if(controlId.equals(CS)==true)

{

MakeCentreButton(controlName, controlValue,returnValue);

}

else if(controlId.equals(RS)==true)

{

MakeRightButton(controlName, controlValue,returnValue);

}

else if(controlId.equals(MO)==true)

{

MakeMenuOption(controlName, controlValue);

}

else if(controlId.equals(LB)==true)

{

MakeLabel(controlName, controlValue);

}

else if(controlId.equals(PB)==true)

{

MakePasswordTextBox(controlName, controlValue);

}

else if(controlId.equals(HD)==true)

{

MakeHidden(controlName, controlValue);

}

else if(controlId.equals(CL)==true)

{

MakeCalender(controlName,controlValue);

}

}

private void IterateItem()

{

//System.out.println("in IterateItem");

int s = this.size();

//System.out.println("in IterateItem no->"+s);

for (int j=0; j<s; j++)

{

Item item = this.get(j);

//System.out.println(""+item);

if (item instanceof TextField)

{

routeLocatorURL = routeLocatorURL + "&" + textfield[l].getLabel() + "=";

//System.out.println(">"+textfield[l].getLabel());

routeLocatorURL = routeLocatorURL + URLEncoder(textfield[l].getString());

l++;

}

else if(item instanceof DateField)

{

if(datefield[m].getDate()==null)

routeLocatorURL = routeLocatorURL;

else

{

routeLocatorURL = routeLocatorURL + "&" + datefield[m].getLabel() + "=";

String finaldate=datefield[m].getDate().toString();

getDateString(finaldate);

routeLocatorURL = routeLocatorURL + URLEncoder(monthdate);

}

m++;

}

else if(item instanceof StringItem)

{

routeLocatorURL = routeLocatorURL;

}

/*else if(menuChoiceGroup1 instanceof ChoiceGroup)

{

boolean selected[] = new boolean[menuChoiceGroup1.size()];

menuChoiceGroup1.getSelectedFlags(selected);

for (int i = 0; i < menuChoiceGroup1.size(); i++)

{

routeLocatorURL = routeLocatorURL + "&" + menuChoiceGroup1.getString(i) + "=";

routeLocatorURL = routeLocatorURL + selected[i];

}

}*/

else if(item instanceof ChoiceGroup)

{

openRMS("RBStore");

routeLocatorURL = routeLocatorURL + returnOption;

routeLocatorURL = routeLocatorURL + readRMS(menuChoiceGroup.getString(menuChoiceGroup.getSelectedIndex()));

closeRMS();

}

}

}

private String getDateString(String finaldate)

{

monthdate=finaldate.substring(4,7);

String daydate=finaldate.substring(8,10);

String yeardate=finaldate.substring(24,28);

monthdate=monthdate+" "+daydate;

monthdate=monthdate+" "+yeardate;

return monthdate;

}

private void CheckRadioButtonStatus(String controlId)

{

if(controlId.equals(RB) != true)

{

if (RBGroup != "")

{

append(menuChoiceGroup);

//closeRMS();

RBGroup = "";

}

}

}

private void CheckBOxStatus(String controlId)

{

if(controlId.equals(CB) != true)

{

if (CBGroup != "")

{

append(menuChoiceGroup1);

CBGroup = "";

}

}

}

private void CheckListGroupStatus(String controlId)

{

if(controlId.equals(MO) != true)

{

if(ListGroup != "")

{

appl.display.setCurrent(list);

ListGroup="";

}

}

}

private void MakeCheckBox(String controlName,String controlValue)

{

if(CBGroup=="")

{

menuChoiceGroup1 = new ChoiceGroup("",ChoiceGroup.MULTIPLE);

CBGroup=controlName;

}

menuChoiceGroup1.append(controlValue,null);

}

private void MakeHidden(String controlName,String controlValue)

{

if(strHidden=="")

{

strHidden = "&" + controlName + "=" + controlValue;

}

else

{

strHidden = strHidden + '|' + "&" + controlName + "=" + controlValue;

}

}

private void MakeTextBox(String controlName,String controlValue)

{

//System.out.println("i"+i);

textfield [i] = new TextField(controlName, null, 50, TextField.ANY);

append(textfield[i]);

i++;

}

private void MakeRadioButton(String controlName,String controlValue, String returnValue)

{

if(RBGroup=="")

{

menuChoiceGroup = new ChoiceGroup(controlName,ChoiceGroup.EXCLUSIVE);

RBGroup = controlName;

}

openRMS("RBStore");

menuChoiceGroup.append(controlValue,image);

writeRMS(controlValue + SubStringSeparator + returnValue);

returnOption = "&" + RBGroup + "=";

closeRMS();

}

private void MakeLeftButton(String controlName,String controlValue,String returnValue)

{

leftButton = new Command(controlName, Command.ITEM, 1);

addCommand(leftButton);

LsActionString = "&" + "aid" + "=" + returnValue;

}

private void MakeCentreButton(String controlName,String controlValue , String returnValue)

{

centerButton = new Command(controlName, Command.ITEM, 1);

addCommand(centerButton);

CsActionString = "&" + "aid" + "=" + returnValue;

}

private void MakeRightButton(String controlName,String controlValue,String returnValue)

{

rightButton = new Command(controlName, Command.ITEM, 1);

addCommand(rightButton);

ActionIdString = returnValue;

RsActionString = "&" + "aid" + "=" + ActionIdString;

}

private void MakeMenuOption(String controlName,String controlValue)

{

list = new List("",List.IMPLICIT);

if(ListGroup=="")

{

ListGroup=controlName;

}

list.append(controlValue,null);

}

private void MakeLabel(String controlName,String controlValue)

{

append(new StringItem(controlName,controlValue));

}

private void MakePasswordTextBox(String controlName,String controlValue)

{

append(new TextField(controlValue, null, 15, TextField.PASSWORD));

}

private void MakeCalender(String controlName,String controlValue)

{

datefield [h] = new DateField(controlName,DateField.DATE);

append(datefield[h]);

h++;

}

private String GetSubString(String message,String StringSeparator)

{

String strSubString ="";

int idx=0;

if (message.length() == 0)

return "";

idx=message.indexOf(StringSeparator);

if(idx != -1)

{

strSubString=message.substring(0,idx);

}

else

{

strSubString=message;

}

return strSubString;

}

private String GetRemString(String message,String StringSeparator)

{

String strSubString ="";

int idx=0;

idx=message.indexOf(StringSeparator);

if(idx != -1)

{

strSubString=message.substring(idx+1, message.length());

}

else

{

strSubString="";

}

return strSubString;

}

// Manage Record Store

public void openRMS(String rsName)

{

try

{

// Create the record store if it does not exist

rs = RecordStore.openRecordStore(rsName, true );

}

catch (Exception e)

{

db(e.toString());

}

}

public void closeRMS()

{

try

{

rs.closeRecordStore();

//System.out.println("close recordstore");

}

catch (Exception e)

{

//db(e.toString());

e.printStackTrace();

}

}

/*public void deleteRMS()

{

try

{

int j=rs.getNumRecords();

for (int i = 1; i <= j; i++)

{

System.out.println(""+j);

rs.deleteRecord(i);

System.out.println("Record Deleted"+i);

}

}

catch(Exception e)

{

e.printStackTrace();

}

}*/

public void deleteRMS()

{

//closeRMS();

if (RecordStore.listRecordStores() != null)

{

try

{

RecordStore.deleteRecordStore("RBStore");

}

catch (Exception e)

{

db(e.toString());

}

}

}

public void writeRMS(String str)

{

byte[] rec = str.getBytes();

try

{

rs.addRecord(rec, 0, rec.length);

//System.out.println("Adding: " + str);

//System.out.println("Adding: "+ rs.getNumRecords());

}

catch(Exception e)

{

db(e.toString());

}

}

public String readRMS(String rsValue)

{

try

{

//System.out.println("In the Read RMS"+rsValue);

//System.out.println("In the Read RMS");

byte[] data = new byte[50];

int dataLen;

String strData = "";

String strName = "";

String strValue = "";

//int j=rs.getNumRecords();

//System.out.println("In the Read RMS"+j);

for (int i = 1; i <= rs.getNumRecords(); i++)

{

dataLen = rs.getRecord(i, data, 0);

//System.out.println(""+dataLen);

strData = new String(data, 0, dataLen);

strName = GetSubString(strData, SubStringSeparator);

strData = GetRemString(strData,SubStringSeparator);

strValue = GetSubString(strData, SubStringSeparator);

// Compare values

if(strName.equals(rsValue) == true)

{

//System.out.println("***Returning RB Value: " + strValue);

//closeRMS();

return(strValue);

}

//System.out.println("Record #" + i + ": " + new String(data, 0, dataLen));

}

}

catch(Exception e)

{

db(e.toString());

}

//closeRMS();

return ("");

}

private void db(String str)

{

//System.out.println("ERROR" + str);

}

//textfield data encode i.e. space character

public static String URLEncoder(String str)

{

if (str == null)

return null;

StringBuffer resultStr = new StringBuffer(str.length());

char tmpChar;

for(int x=0;x<str.length();x++)

{

tmpChar = str.charAt(x);

switch(tmpChar)

{

case ' ' : resultStr.append( "%20" );

break;

default : resultStr.append( tmpChar );

break;

}

}

return resultStr.toString();

}

}

hi, this is my code when i click on the login button it will show process bar or connection bar until we get the response from the server over the internet...

please help me in this

that is what i want to do....>

anup123a at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
I have tried it out with gauge but it will not show what i need it shows the image moving or small reactangle moving i want to set proper process bar which we can generally see when we connecting to internet ..
anup123a at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

this example is taken from this link..

http://developers.sun.com/techtopics/mobility/midp/articles/midp2network/

this is a basic connection to a server...

...

SocketConnection client = (SocketConnection) Connector.open("socket://" + hostname + ":" + port);

// set application-specific options on the socket. Call setSocketOption to set other options

client.setSocketOption(DELAY, 0);

client.setSocketOption(KEEPALIVE, 0);

InputStream is = client.openInputStream();

OutputStream os = client.openOutputStream();

// send something to server or not

// depends on your application

os.write("some string".getBytes());

when the connection is established, you can display a new Form with a moving object

to tell to the user that the application loading the response...

// read server response

// display the "waiting form"

int c = 0;

while((c = is.read()) != -1) {

// do something with the response

//

}

// close streams and connection

is.close();

os.close();

client.close();

...

when the treatement is finished, you can display the result of the request

i hope that it will help you !

suparenoa at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

hi,

Thanks for your help but i want to know how to set that image. Cause i want to set the moving image . I am not able to set the image in gif format it will giving me null pointer exception and jpg and png images are able to set but they does not have moving animation please tell me that..

Thanks in advance

anup123a at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
Hi supareno, Please tell me how to set the moving image in the code i am not able to do it please help me out............ Please send me some code or the idea how to set the moving image in the code.Thanks in advance..............
anup123a at 2007-7-9 22:18:20 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

my idea is:

1 - Create 4 small images in png that represents a rotation such the characters below...

. |

./

. --

. \

2 - in your run() method, you display the 4 images one by one like thay:

. | / -- \ | / -- etc...

it will display a rotating bar...

i hope that i'm clear !!!!!!

the code should be:

...

Vector images=new Vector();

...

private void loadImages(){

for(int i=1;i<5;i++){

// images are in the res folder of the MIdlet suite

// and don't forget the try and catch !!!

images.addElement(Image.createImage("img/img"+i+".png");

}

}

...

public void run(){

while(searching){

int newValue = getCurrentImage() + 1;

if (newValue == 4 {

delta = 1;

}

imageItem.setImage(images.elementAt(newValue);

// calling repaint() ? test !!!

try {

Thread.currentThread().sleep(1000);

} catch (InterruptedException err) {

err.printStackTrace();

}

}

}

...

i hope it will help you !

suparenoa at 2007-7-9 22:18:21 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8
you can draw a progress bar on canvas. use Graphics.fillRect to draw your progress bar.
RiekeyLeea at 2007-7-9 22:18:21 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
thanks supareno and all to help me out this problem it works i have used canvas to draw the process bar and then use thread to display it...
anup123a at 2007-7-9 22:18:21 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
thanks RiekeyLee for u r help too......
anup123a at 2007-7-9 22:18:21 > top of Java-index,Java Mobility Forums,Java ME Technologies...