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
New Developer
nurhanisah
Posts: 13
Registered: ‎09-29-2008

User Input Text Dialog Box

Hi.

 

I am creating a dialog box which allows users to key in some text inside. I am building on version 4.1.  After keying in the text, user would choose either Ok or Cancel. Here is the code for it:

 

final class TestDialog extends Dialog{

private EditField userNameField;
private ButtonField okButton;
private ButtonField cancelButton;

public TestDialog(String choices[],int values[]){
super("Enter a username", choices,values,Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.QUESTION), Dialog.GLOBAL_STATUS);
userNameField = new EditField("User Name: ", "", 50, EditField.EDITABLE | EditField.FILTER_PHONE);
net.rim.device.api.ui.Manager delegate = getDelegate();
if( delegate instanceof DialogFieldManager){
DialogFieldManager dfm = (DialogFieldManager)delegate;
net.rim.device.api.ui.Manager manager =dfm.getCustomManager();
if( manager != null ){
manager.insert(userNameField, 0);
}
}
}

public String getUsernameFromField(){
return userNameField.getText();
}

}

 

I am however, facing problems in retrieving which button user has selected (OK or CANCEL). When selecting OK, nothing happens. When selecting CANCEL,I would get an ArrayIndexOutOfBounds error.

 

String choices[] = {"Ok","Cancel"};
int values[] = {Dialog.OK,Dialog.CANCEL};

TestDialog pw = new TestDialog(choices,values);
System.out.println(""+pw.doModal());

if (pw.doModal() == Dialog.OK) {
...
}

 

Does any of you have any idea about this? Thanks in advance.

 

Hanisah

Please use plain text.
Administrator
MSohm
Posts: 12,957
Registered: ‎07-09-2008
My Carrier: Bell

Re: User Input Text Dialog Box

You are passing Dialog.OK into the constructor for Dialog in the parameter that specifies the default choice.  This choice value is the index of the array (choices[] in your case) that you want selected by default.  Dialog.OK is a constant for the Dialog class, and won't match up with your array position.

 

I recommend having a look at the VoiceNotesDemoDialog class that is part of the voicenotesdemo included with the BlackBerry JDE.  It is an example of a custom Dialog that prompts for user input.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Please use plain text.
New Developer
zhijunsheng
Posts: 16
Registered: ‎02-06-2009

Re: User Input Text Dialog Box

I searched JDE 4.2.1/4.6.0/4.7.0 but failed to find any demo with name "voicenotesdem" or similar.  Where is it?  Thanks!
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009

Re: User Input Text Dialog Box

Try with the following:

 

 

class TestScreen extends MainScreen { private Dialog diag; public TestScreen() { String choices[] = {"Ok","Cancel"}; int values[] = {Dialog.OK,Dialog.CANCEL}; diag = new TestDialog(choices,values); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { int iResponse = diag.doModal(); if(iResponse == 0) { System.out.println("OK Pressed"); } else { System.out.println("Cancel Presses"); }

} }); } }

 

Regards

Bikas 

 

Please use plain text.
Developer
peter_strange
Posts: 17,631
Registered: ‎07-14-2008

Re: User Input Text Dialog Box

Minor point, possibly more accurate to use:

 

if(iResponse == Dialog.OK)

Please use plain text.
Developer
sreid55
Posts: 41
Registered: ‎09-28-2009

Re: User Input Text Dialog Box

I realize this thread is old, but I just came across it and did figure out how to avoid the array out of bounds exception.  

Add an additional value to the front of the values[] array to be a 'placeholder' for for editfield.

 

int values[] = {0,Dialog.OK,Dialog.CANCEL};


Please use plain text.