Set ObjectChoiceField values from a Database in BlackBerry - blackberry

How to set values to ObjectChoiceField from Database in BlackBerry?

String[] days={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
ObjectChoicdField ocf = new ObjectChoiceField("Days.",days,days.length-1) {
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
ocf.setChangeListener(this);
add(ocf);

Related

Not able to set the focus on a HorizontalFieldManager

I am not able to set the focus on the HorizontalFieldManager having two LabelFields. I want to highlight the HorizontalFieldManager when it gets focus. I am using the following code, but it is not working.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(
Manager.USE_ALL_HEIGHT|Manager.FOCUSABLE) {
protected void onFocus(int direction) {
focussed = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
focussed = false;
invalidate();
super.onUnfocus();
}
protected void paint(Graphics g) {
g.setBackgroundColor(0x646060);
if (focussed) {
g.setColor(Color.BLUE);
}
g.clear();
super.paint(g);
}
};
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
horizontalFieldManager_left15.add(hrzMgrTimeLabel);
Here is the LabelField implementation.
final LabelField a= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
final LabelField b= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
The getFocusRect() function might give you the answer.
It gives the extent for which the focus is applied in Manager and then the Graphics.paint() method or Border class can be used.
You need to at least one focusable field on the HorizontalFieldManager. The following line will do that.
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
Check the following code.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(Manager.USE_ALL_HEIGHT | Manager.FOCUSABLE) {
protected void paint(Graphics g) {
g.setBackgroundColor(isFocus() ? Color.BLUE : 0x646060);
g.clear();
super.paint(g);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
protected boolean navigationMovement(int dx, int dy, int status, int time) {
invalidate();
return super.navigationMovement(dx, dy, status, time);
}
protected boolean navigationClick(int status, int time) {
invalidate();
return super.navigationClick(status, time);
}
protected boolean navigationUnclick(int status, int time) {
invalidate();
return super.navigationUnclick(status, time);
}
};
final LabelField a = new LabelField("First Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
final LabelField b = new LabelField("Second Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
Note
onFocus(...), onUnfocus(), navigationMovement(...), navigationClick(...), navigationUnclick(...) - these methods were just overrided for refreshing the drawing.
If you want to focus your label field then use final LabelField a= new LabelField("".FOCUSABLE).

change selected font color in EditField/BasicEditField/RichTextField in blackberry?

I am preparing for a new app, just want to create a TextField.
I have to provide functionality to select a portion of text and change its color.
For example: hello, I am blackberry developer.
Now I want to change the font color of blackberry developer at runtime.
Is it possible in EditField or BasicEditField or RichTextField in any one???
Thanxx in advance!:)
LabelField labelField2 = new LabelField("hello, I am blackberry developer",Field.FOCUSABLE){
boolean _inFocus = false;
public void onFocus(int direction) {
_inFocus = true;
this.invalidate();
}
public void onUnfocus() {
_inFocus = false;
this.invalidate();
}
protected void paint(Graphics graphics) {
if(_inFocus){
graphics.setColor(Color.BLUE);
}else{
graphics.setColor(Color.RED);
}
super.paint(graphics);
}
protected void drawFocus(Graphics g, boolean on) {
}
};
Enjoy...

How can i create a popup for custom Choicefield and call it?

I have created my own simple ChoiceField by extending ObjectChoiceField.. It is like..
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Now i want to display the my choices when i select MyChoiceField in custom menu(not the default popup that blackberry provides when user clicks on a ChoiceField)..
How can i achieve it? Sample codes will be appreciable..
Thanks
Try Overriding the method fieldChangeNotify(int context) of ObjectChoiceField like this:
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
protected void fieldChangeNotify(int context) {
int index = getSelectedIndex();
try{
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));
PopupScreen popUpScreen = new PopupScreen(vfm){
public boolean onClose()
{
this.close();
return true;
}
};
UiApplication.getUiApplication().pushScreen(popUpScreen);
} catch(Exception exc) {
System.out.println("Exception in choiceField fieldChangeNotify");
}
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Use MyChoiceField now somewhat like this:
String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));
Let me know whether this solution works.

Custom Edit Field Creation problem

I have created one screen in which there is background.and on that i am creating a Custom Edit Fields with one button.now my problem is that my edit field is displaying on upper side.i want to make to the center of the screen.what modifications i have to do so.i am giving my code snippet.
public class CancelBooking extends MainScreen implements FieldChangeListener {
public LabelField heading;
ButtonField next;
String emp_id,emp_name,salary;
public CancelBooking(String id,String name,String sal) {
// TODO Auto-generated constructor stub
emp_id=id;
emp_name=name;
salary=sal;
}
// protected void sublayout(int width, int height){
// super.sublayout(Display.getWidth(), height);
// setExtent(Display.getWidth(), this.getPreferredHeight());
// int leftPadding = Display.getWidth()/2 -this.getPreferredWidth()/2;
// int rightPadding = Display.getWidth() - leftPadding -this.getPreferredWidth();
//
// setPadding(5,rightPadding,5,leftPadding);
// invalidate();
// }
CancelBooking()
{
heading = new LabelField(){
public void paint(Graphics g){
g.setColor(Color.WHITE);
super.paint(g);
}
};
heading.setFont(Font.getDefault().derive(Font.BOLD, Font.getDefault().getHeight()+2));
heading.setText("Check-In");
getMainManager().setBackground(
BackgroundFactory.createBitmapBackground(
Bitmap.getBitmapResource("PlainScreen.png")));
//setTitle("First Screen");
BasicEditField id=new BasicEditField("Last Name: ","");
BasicEditField name=new BasicEditField("Confirmation Number: ","");
//BasicEditField sal=new BasicEditField("Salary: ","");
add(id);
add(name);
//add(sal);
next=new ButtonField("Next");
next.setChangeListener(this);
// add(next);
}
public void fieldChanged(Field field, int context)
{
if(field==next)
UiApplication.getUiApplication().pushScreen(new MenuScreen());
}
}
You have to add FIELD_HCENTER and FIELD_VCENTER in the style of your field.
Consider reading this link
"Managers of Blackberry docs"

Adding fields to a BlackBerry PopupScreen

I want to add fields and buttons to a BlackBerry PopupScreen.
You want customize the popup screen like this
public class CustomPopUpScreen extends PopupScreen {
public CustomPopUpScreen() {
super(new VerticalFieldManager(CustomPopUpScreen.NO_HORIZONTAL_SCROLL));
add(new ButtonField());
add(new ButtonField());
}
}
This code will help you
Here I create object of type PopupScreen and call the method givePopup:
PopupScreen popup1=givePopup();
PopupScreen givePopup(){
VerticalFieldManager popvfm =new VerticalFieldManager();
ButtonField ok=new ButtonField("ok");
ButtonField cancel=new ButtonField("cancel");
popvfm.add(ok);
popvfm.add(cancel);
PopupScreen popup=new PopupScreen(popvfm);
return popup;
}
This method returns a PopupScreen instance that has one vertical field manager and I add two button field on this manager.
public class FriendPopupScreen extends MainScreen{
int dialogWidth;
int dialogHeight;
LabelField lblTitle;
HorizontalFieldManager hfmTitle;
Vector data;
public FriendPopupScreen() {
dialogWidth = 300;
dialogHeight = 150;
lblTitle = newLabelField("Choose option");
hfmTitle = new HorizontalFieldManager(USE_ALL_WIDTH){
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
maxWidth= Display.getWidth();
maxHeight=40;
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics graphics) {
// TODO Auto-generated method stub
graphics.setBackgroundColor(Color.BLACK);
graphics.clear();
super.paint(graphics);
}
};
hfmTitle.add(lblTitle);
lblTitle.setMargin(10,0,0,10);
add(hfmTitle);
}
protected void sublayout( int width, int height ) {
setExtent( dialogWidth, dialogHeight );
setPosition( Display.getWidth()/2-(dialogWidth/2), Display.getHeight()/2 - (dialogHeight/2));
layoutDelegate( dialogWidth, dialogHeight );
}
}
and call this:
FriendPopupScreen popup = new FriendPopupScreen();
UiApplication.getUiApplication().pushModalScreen(popup);

Resources