Handling long string not visible in one entry of list field in Blackberry (show ...@ end)

This code returns string which can be shown in a field having width=width, 5 pixel margin on left and 5 pixel on right.
    private String getTextShown( String completeString, int width ) {
        if ( completeString == null )
            return null;
        width = width - 10;
        Font font = listfield.getFont( );
        if ( font.getAdvance( completeString ) < width )
            return completeString;
        for ( int i = completeString.length( ) - 1; i > 0; i-- ) {
            completeString = completeString.substring( 0, i );
            StringBuffer tempBuffer = new StringBuffer( completeString );
            tempBuffer.append( "..." );
            String temp = tempBuffer.toString( );
            if ( font.getAdvance( temp ) < width ) {
                return temp;
            }
        }
        return completeString;
    }
This can be called from

     protected void paint( Graphics graphics ) {
         String stringToDraw = getTextShown( "This is very long text. this will not fit in one line on the blackberry device", w );
         graphics.drawText( stringToDraw, x, y );
     }

Comments

Popular posts from this blog

Enhancing LLM Responses with Prompt Stuffing in Spring Boot AI

Automate Library Integration with Cursor's Agent Mode

Upload an image from Android to Google's app engine server