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
Highlighted
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

Font size painting problem

Using this class:

 

package net.berrysoft.dib.fields; import net.rim.device.api.ui.DrawStyle; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Font; import net.rim.device.api.ui.Graphics; public class MultiColumnTextField extends Field { public int getBuffer( ) { return buffer; } public void setBuffer( int buffer ) { this.buffer = buffer; } public int[ ] getSpaces( ) { return spaces; } public void setSpaces( int[ ] spaces ) { this.spaces = spaces; } public String[] getColText() { return colText; } public void setColText( String[] text ) { colText = text; } public MultiColumnTextField( Object[] data ) throws IllegalArgumentException { this( data, Field.FOCUSABLE ); } public MultiColumnTextField( Object[] data, int buffer ) throws IllegalArgumentException { this( data, buffer, Field.FOCUSABLE ); } public MultiColumnTextField( Object[] data, long style ) throws IllegalArgumentException { this( data, 2, style | Field.FOCUSABLE ); } public MultiColumnTextField( Object[] data, int buffer, long style ) throws IllegalArgumentException { super( style ); //setFont( Font.getDefault( ).derive( Font.PLAIN, 40 )); int n = data.length; if ((n % 2) != 0) { throw new IllegalArgumentException("Invalid number of elements"); } else { spaces = new int[n/2]; colText = new String[n/2]; } for( int i = 0; i < n ; i = i + 2 ) { colText[i/2] = (String)data[i]; spaces[i/2] = ( (Integer)data[i+1] ).intValue(); } this.buffer = buffer; } public void layout(int width, int height) { Font font = this.getFont( ); int fieldWidth = 0; for ( int i = 0; i < spaces.length; ++i ) { fieldWidth += spaces[i]; if ( i < spaces.length-1 ) { fieldWidth += buffer; } } setExtent( fieldWidth, font.getHeight() ); } public void paint( Graphics g ) { int xpos = 0; for ( int i = 0; i < spaces.length; ++i ) { g.drawText( colText[i], xpos, 0, DrawStyle.ELLIPSIS, spaces[i] ); xpos += spaces[i]; if ( i < spaces.length-1 ) { xpos += buffer; } } } // PRIVATE IMPLEMENTATION private int buffer; // Space between each column private int[] spaces; // Number of spaces allocated to column private String[] colText; // Array of column text }

 

 I create a dialog which as one of these fields with test code like this:

Object[] fieldData1 = { "Buddah", new Integer(NAME_COLS), "81", new Integer(SCORE_COLS), }; dm.add( new MultiColumnTextField( fieldData1, 2, Field.FOCUSABLE ) );

but while the height of the resulting field appears correct, the "text" painted appears to be very tiny boxes (like the font is too tiny).  But even if I uncomment the line in the constructor which explicitly sets a larger font, there is no effect.

 

Anyone see what I'm doing wrong?

 

Patrick

 


 

Developer
Developer
Posts: 319
Registered: ‎07-20-2008
My Device: Not Specified

Re: Font size painting problem

I think that since you're overriding paint, you have to call g.setFont explicitly before g.drawText().
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Font size painting problem

Thanks taking a look... but even if I set the font in the constructor (uncomment that line), and set it in paint() as you suggest,

 

 

g.setFont( getFont() );

 

 

I still get tiny boxes rather than characters within a layout that is sized correctly.

 

Patrick

 

 

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Font size painting problem

What BlackBerry model and handheld software version are you testing on?  You can find this under Options, About on the BlackBerry handheld.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 143
Registered: ‎07-14-2008
My Device: Not Specified

Re: Font size painting problem

Model 8820, Platform: 2.7.0.68, App Version: 4.5.0.55(81), however, code is targeting JDK 4.2.1.

 

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Font size painting problem

The width value you are specifying in the drawText method is too small and the text is being clipped. 

 

You can use the Font.getAdvance method to determine the width of a string of text.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker