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
Super Contributor
Achin
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

font size problem

in the pic, i'm chnaging the font styles and size, the font style is working good, but when i try to cahnge the size of text in label filed that was giving me error(errr=20, means when i print the exception it's showing me the selecd font size in catch), here is my code

public class MyApp extends UiApplication
{

String[] iFontSize = {"10","20","25","30","35","40"};
String[] Backimages = {"first","second","third","fourth","Fifth","sixth"};
MainScreen mainScreen;
ObjectChoiceField pickSize, Background,pickFont;
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();
}

public MyApp()
{

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);

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 == pickFont)
{
//Get the new font selected in the ObjectChoiceField and set the
//current font to match what is selected.
try{
font = fontFamily[pickFont.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT, 20);
fontText.setFont(font);
}catch(Exception e)
{
System.out.println("font error:="+e.getMessage());
}
}
if (field == pickSize){
try {
font = fontFamily[pickFont.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT,
Integer.parseInt((String) pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt));
fontText.setFont(font);
} catch (Exception er) {
// TODO Auto-generated catch block
System.out.println("errr="+er.getMessage());
er.printStackTrace();
}

}

}
}
}

Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: font size problem

use the debugger to see what is wrong.
what looks suspicious is that you read the value from the choicefield but don't do anything with it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Super Contributor
Achin
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: font size problem

yes, i think u are right,but how can i change size of text in label in my code
Please use plain text.
Contributor
ansarint
Posts: 39
Registered: ‎02-22-2013
My Device: blacberry torch 2012
My Carrier: airtel

Re: font size problem

with the help of debugger..

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

Re: font size problem

i have use this to change the size but not working
font = fontFamily[pickSize.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT,
Integer.parseInt(pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt));
text.setFont(font);
Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: font size problem

parseInt returns an int value, but you don't use it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Super Contributor
Achin
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: font size problem

then plz u can help me how to use that value...
Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: font size problem

Sorry, i was mistaken, you use the value. Maybe you could use the code tag to format your posts, makes them readable a bit better.

is it intended that you use the index of pickSize to get a fontfamily? According to your description you use only a single family and want to change its size.

in any case, you want to solve something, don't try to win a prize for the shortest line of code. assign the values to variables and check if they contain what you think with the debugger.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: font size problem

"in any case, you want to solve something, don't try to win a prize for the shortest line of code. assign the values to variables and check if they contain what you think with the debugger"

 

I agree, that appears to be the problem in this case.

 

Compare following with your code:

        int intValue = Integer.parseInt(pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt);
        Font font = fontFamily[pickSize.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT, intValue);

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

Re: font size problem

i have used that one but not working giving me excepton

private Font font1;

font1 = fontFamily[0].getFont(FontFamily.SCALABLE_FONT, 20);

 

pickSize.setChangeListener(new FieldChangeListener() {

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

try {
/*font1 = fontFamily[choiceField.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT,
Integer.parseInt((String) pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt));

text.setFont(font1);*/

int intValue = Integer.parseInt(pickSize.getChoice(pickSize.getSelectedIndex()).toString(),Ui.UNITS_pt);
font1 = fontFamily[pickSize.getSelectedIndex()].getFont(FontFamily.SCALABLE_FONT, intValue);
text.setFont(font1);


} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.out.println("fffont size error=="+e.getMessage());
e.printStackTrace();
}

}
});

Please use plain text.