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

Create a custom Dialog

by Retired on ‎02-12-2010 03:46 PM - edited on ‎03-18-2011 01:34 PM by Retired (6,917 Views)

Summary

 

This article applies to the following:

  • BlackBerry® Java® Development Environment (JDE)
  • BlackBerry wireless devices based on Java®

Procedure

 

You can create a custom Dialog by extending the net.rim.device.api.ui.component.Dialog class and customizing it to suit your needs. A custom Dialog can allow you to request specific information from a user. When the Dialog closes, this information is passed back to your application. To do this, you have to extend the Dialog class.

 

The following example shows a custom Dialog that contains an EditField that retrieves text from the user, then displays it on the main screen:

 

 

/**
* Custom Dialog Screen
*/

package com.samples.dialogDataEntry;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public final class CustomDialog extends Dialog
{
private EditField entryField;

public CustomDialog()
{
super(Dialog.D_OK, "Custom Dialog", 1,
Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.FOCUSABLE);

entryField = new EditField
("Enter some text: ", "", 50, EditField.EDITABLE);
add(entryField);

}

public boolean keyChar(char key, int status, int time)
{
//Override key commands

switch (key)
{
case Characters.ENTER:
//Update main class with the entered text.
DialogDataEntry.setText(entryField.getText());
this.close();
break;
//Override the backspace key to delete the last character.
case Characters.BACKSPACE:
if (entryField.getTextLength() > 0)
{
entryField.setText(entryField.getText().substring
(0, entryField.getTextLength() - 1));
}
break;
//Override the escape key to delete the last character.
case Characters.ESCAPE:
if (entryField.getTextLength() > 0)
{
entryField.setText(entryField.getText().substring
(0, entryField.getTextLength() - 1));
}
break;
//Enter the character the user typed into the EditField.
default:
entryField.setText(entryField.getText() + key);
break;
}

return true;

}

/**
* Main Class
*/
package com.samples.dialogDataEntry;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

class DialogDataEntry extends UiApplication
{
private MainScreen mainScreen;
private static RichTextField enteredText;

public static void main(String[] args)
{
DialogDataEntry theApp = new DialogDataEntry();
theApp.enterEventDispatcher();
}

public DialogDataEntry()
{

mainScreen = new MainScreen();

enteredText = new RichTextField("", RichTextField.NON_FOCUSABLE);

LabelField title = new LabelField("Dialog Data Entry Example",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);

mainScreen.setTitle(title);

mainScreen.add(enteredText);

//Display the main screen
pushScreen(mainScreen);

//Create an instance of and display the custom dialog.
CustomDialog textEntryDialog = new CustomDialog();
textEntryDialog.show();
}

//Allow the Dialog to update the text by calling this method.
public static void setText(String newText)
{
enteredText.setText(newText);
}
}

 


Additional Information

 

Users Online
Currently online: 26 members 2,085 guests
Please welcome our newest community members: