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

Reply
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: font size

You seem to be saying that you are not sure which image is to be selected.  But in your change listener you can check the selectedIndex.  So you know whether the first second or third one has been chosen.  So you can do this the same way as you choose the font. 

Please use plain text.
Super Contributor
Achin
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: font size

[ Edited ]

here is my code,i have retrieve the values of the object choice field look at my FontChangeListener, now i did not understand how will i give reference to my bitmap images, and how can i set that selected images to background image for my layout.

public class MyApp extends UiApplication
{
String[] iFontSize = {"10","20","25","30","35","40"};
String[] Backimages = {"first","second","third","fourth","Fifth","sixth"};

ObjectChoiceField pickSize, Background;

RichTextField fontText;

private Font font, font1;
//Get all fonts available on the BlackBerry
private FontFamily fontFamily[] = FontFamily.getFontFamilies();

public static void main(String[] args)
{

MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}

/**
* Creates a new MyApp object
*/
public MyApp()
{

MainScreen mainScreen;

FontChangeListener fontChangeListener = new FontChangeListener();

//Set the current font to the first in the font list.
font = fontFamily[0].getFont(FontFamily.SCALABLE_FONT, 20);
Font.setDefaultFont(font);

mainScreen = new MainScreen();

mainScreen.setTitle("Font Test Example");

pickSize= new ObjectChoiceField ("Select Font Size:", iFontSize,0);
Background = new ObjectChoiceField("images:", Backimages,0);

ObjectChoiceField pickFont = new ObjectChoiceField("Select font:", fontFamily, 0);


pickFont.setChangeListener(null);
pickFont.setChangeListener(fontChangeListener);

mainScreen.add(pickFont);
mainScreen.add(new SeparatorField());
mainScreen.add(pickSize);
mainScreen.add(new SeparatorField());
mainScreen.add(Background);

pickSize.setChangeListener(null);
pickSize.setChangeListener(fontChangeListener);

Background.setChangeListener(fontChangeListener);
mainScreen.add(new SeparatorField());

//RichTextField that displays text in the specified colour.
fontText = new RichTextField("The quick red fox jumps over the lazy brown dog.")
{
public void paint(Graphics graphics)
{
//Change the colour of the text in the RichTextField to green.
graphics.setColor(0x00008800);
super.paint(graphics);
}
};
mainScreen.add(fontText);

//Display the main screen
pushScreen(mainScreen);

}

//Create a custom FieldChangeListener that will change the current font
//to what is selected in the ObjectChangeField.

class FontChangeListener implements FieldChangeListener
{
public void fieldChanged(Field field, int context)
{
//If the ObjectCoiceField has changed
if (field instanceof ObjectChoiceField)
{
//Get the new font selected in the ObjectChoiceField and set the
//current font to match what is selected.
try{
ObjectChoiceField choiceField = (ObjectChoiceField)field;
font = fontFamily[choiceField.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT,
Integer.parseInt((String) pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt));
//Font.setDefaultFont(font);
fontText.setFont(font);
}catch(Exception e)
{
System.out.println("font error:="+e.getMessage());
}
}
if(field == Background)
{
String index = (String) Background.getChoice(Background.getSelectedIndex());
system.out.println(index);// here i got the values of my ObjectCoiceField

}

}
}
}

 

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: font size

[ Edited ]

I am not clear on what you don't understand - you seem to have the solution already in front of you, or I am not understanding the question you are asking. 

 

In your listener you have:

Background.getSelectedIndex()

 

So if this is 0, then you mean one image, 1 means another and so on.  Exactly how you do relate the index to an image is up to you.  But by using that index, you can get a Bitmap. 

 

At this point, you will also have the Field/Manager that you want to set the image on, because this doesn't change.  And you have the image selected, which you have figured out using the index.  So you just have to set the image. 

 

Exactly how you do that will depend on how you have coded your Field or Manager. If the Field you wanted to update was a BitmapField, then all you would have to do is setBitmap.  If this was a Manager, then you could use BackgroundFactory to create a Bitmap background, and then do setBackground.  If it is your own special Field that paints its own Bitmap, then you will have to provide a method that allows the Bitmap t be updated externally. 

 

So you have all the pieces you need, I don't understand why you can't put them together.  So I can only suggest you have another think about it, talk the problem over with a co-worker, look at the answers on here again and hopefully it will come to you. 

 

OK?

 

In future, can I suggest that you don't post all your code - the vast majority of this code is not relevant to the problem you are having and you should not expect us to spend our valuable time going through loads of code.  If you are struggling with a specific problem, then please publish a snippet of code that relates to that problem.  I know it is easier for you to just cut and paste your code, but it is not easier for us, and you really want to make it easy for us otherwise we will not be bothered looking at your issues. 

 

I have already pointed you at this:

http://www.catb.org/esr/faqs/smart-questions.html

I recommend that you review it to get the best from this and any other forum you try to use.  Here are some sections I think would help you:

http://www.catb.org/esr/faqs/smart-questions.html#code

http://www.catb.org/esr/faqs/smart-questions.html#explicit

Please use plain text.
Super Contributor
Achin
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: font size

[ Edited ]

i have use this code..

 

public final class MyScreen extends MainScreen
{
String[] Backimages = {"image1","image2","image3","image4","image5","image6"};
ObjectChoiceField pickSize, Background;
private VerticalFieldManager mainManager;
private VerticalFieldManager subManager;
private Bitmap _backgroundBitmap = Bitmap.getBitmapResource("a.png");
private int deviceWidth = Display.getWidth();
private int deviceHeight = Display.getHeight();
String index;

public MyScreen()
{
super(NO_VERTICAL_SCROLL);
//this manager is used for the static background image
Background = new ObjectChoiceField("images:", Backimages,0);



mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL|Manager.NO_VERTICAL_SCROLLBAR)
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth,deviceHeight, _backgroundBitmap, 0, 0);
super.paint(graphics);
}
};

//this manger is used for adding the componentes
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR)
{
protected void sublayout(int maxWidth, int maxHeight)
{
int displayWidth = deviceWidth;
int displayHeight = deviceHeight;

super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};

// add your component to this subManager
subManager.add(Background);
Background.setChangeListener(new FieldChangeListener() {

public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
if(field == Background)
{

index = (String) Background.getChoice(Background.getSelectedIndex());
Dialog.alert(""+index);
if(index.equals("image1"))
{
mainManager.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("brush.png")));
}


}
}
});

//add subManager over the mainManager
mainManager.add(subManager);

//finally add the mainManager over the screen
this.add(mainManager);

}


}

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: font size

[ Edited ]

You seem to be trying to set the background image for

mainManager

You have overridden paint to try to set that and you are also trying to set it using setBackground.  For vest results I recommend that you choose one or the other and not try to use a mixture. 

 

You have some layout code in here which I am not sure about, I would make sure that  you have read and understood this KB (I suspect I have pointed you at this before but just in case)

http://supportforums.blackberry.com/t5/Java-Development/MainScreen-explained/ta-p/606644

 

If this does not help can you please describe the problem you are having in detail?  Can you debug your code and tell us which statements are not doing what you expect them to do?

Please use plain text.