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
New Developer
hobby_johals
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Custom ObjectChoiseField

U can paste the code here .......

Amar
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: Custom ObjectChoiseField

import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.Graphics;
import java.lang.String;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.Font;
import net.rim.device.api.system.Bitmap;

public class CustomChoiceField extends ObjectChoiceField 
{    
//    public static final int TEXT_HALINGMENT_LEFT = 0;    
    public static final int TEXT_HALINGMENT_CENTER = 1;        
    
    String[] m_Choices;    
    
    private int m_MarginLeft = 1;    
    
    private int m_PaddingTop = 1+1;    
    private int m_PaddingBottom = 1;        
    private int m_MarginTop = 1;    
    private int m_MarginBottom = 1;       
    private int m_TextMarginRight = 5;    
    private int m_TextMarginLeft = 5;    
    private int m_HTextAlignment = 0;    
    private int m_ChoiceFieldWidth;    
    private int m_Height;    
    private boolean m_isFocused = false;    
    private int m_DropDownArrowWidth;    
    private Bitmap image,image_hover;
    private Font font;
    private FontFamily fontFamily[]=FontFamily.getFontFamilies(); 
    
    public CustomChoiceField(String label, String[] choices,  int initialIndex, long style,int flag) 
    {            	
        super(label, choices, initialIndex, Field.FIELD_LEFT|Field.FOCUSABLE);    	
        
        
        m_Choices = choices;         
        font=fontFamily[0].getFont(FontFamily.CBTF_FONT, 15).derive(Font.BOLD);
        Font currFont = font;       
//        m_DropDownArrowWidth = 
        	m_Height = currFont.getHeight();        
//        m_ChoiceFieldWidth = currFont.getAdvance(choices[0]);        
//        for(int i = 1; i < choices.length; i++)
//        {            
//            if(m_ChoiceFieldWidth < currFont.getAdvance(choices[i]))                
//                m_ChoiceFieldWidth = currFont.getAdvance(choices[i]);        
//        }        
//        m_ChoiceFieldWidth += m_TextMarginLeft + m_TextMarginRight + m_DropDownArrowWidth;            
        m_ChoiceFieldWidth =250;
        if(flag==1)
        {
        	image = Bitmap.getBitmapResource("dropdown-box-1.png");
           	image_hover= Bitmap.getBitmapResource("dropdown-box-1_h.png");
        }
        else if(flag==2)
        {
	       	image = Bitmap.getBitmapResource("dropdown-box-2.png");
	       	image_hover= Bitmap.getBitmapResource("dropdown-box-2_h.png");
        }        
    }        
         
    public void setChoices(Object[] choices)
    {    
    	super.setChoices(choices);        
        m_Choices = (String[])choices;      
    }           
 
    public int getPreferredWidth()
    {        
       return image_hover.getWidth()+5;
    }        
    
        
    public int getPreferredHeight()
    {        
        return m_MarginTop + m_PaddingTop + m_PaddingBottom + m_MarginBottom + m_Height+12;    
    }        
    
    public boolean isFocusable()
    {        
        return true;    
    }     
    
    public void getFocusRect(XYRect rect)
    {        
        
        rect.set(m_MarginLeft + getFont().getAdvance(getLabel()), m_MarginTop , m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop+6);
    }        
    
    protected void drawFocus(Graphics graphics, boolean on)
    {        
        invalidate();    
    }        
    
    protected void layout(int width, int height)
    {        
        setExtent(Math.min(getPreferredWidth(), width), Math.min(getPreferredHeight(), height));    
    }        
    
    public void onFocus(int direction)
    {        
    	
        m_isFocused = true;    
        invalidate();    
    }        
    
    public void onUnfocus()
    {        
       m_isFocused = false;  
       invalidate();
    }       
    
    public void paint(Graphics g)
    {  
    	String visibleText = (getSelectedIndex() == -1 || getSelectedIndex() >= m_Choices.length)?"":m_Choices[getSelectedIndex()];        
        int textLength = font.getAdvance(visibleText);        
        int labelLength = font.getAdvance(getLabel());        
        int textY = m_MarginTop + m_PaddingTop + (m_Height -font.getHeight())/2;        
        if(textY < 0)            
            textY = m_MarginTop;             
        
        while(textLength > m_ChoiceFieldWidth - m_TextMarginLeft - m_TextMarginRight - m_DropDownArrowWidth && visibleText.length() > 0)
        {            
            visibleText = visibleText.substring(0, visibleText.length()-1);
          textLength = font.getAdvance(visibleText);        
        }        
        int textX = (m_HTextAlignment == TEXT_HALINGMENT_CENTER)? m_MarginLeft + labelLength + m_TextMarginLeft + (m_ChoiceFieldWidth - m_TextMarginLeft - m_TextMarginRight - m_DropDownArrowWidth - textLength)/2:              
        m_MarginLeft + labelLength + m_TextMarginLeft;        
        if(textX < 0)            
        	textX = m_MarginLeft + labelLength + m_TextMarginLeft;                
       
        g.drawText(getLabel(), m_MarginLeft, textY);           
        if(m_isFocused==true)
        {
        	g.drawBitmap(m_MarginLeft + labelLength-1, m_MarginTop-1, image.getWidth(), image.getHeight()+11,image_hover,0,0);
       		g.drawText(visibleText, textX, textY+3,0,230);
        }
        else
        {	
        	g.drawBitmap(m_MarginLeft + labelLength-1, m_MarginTop-1,image_hover.getWidth() , image_hover.getHeight()+11,image,0,0);
        	g.drawText(visibleText, textX, textY+3,0,230);
        }        
  	}
}
 

Its woring well.

 

 In this some unnecessary methods are there. There is no problem. You can easily run this code.

 

You remove unnecessary data when you are free.

 

=========================================================================

feel free to press the like button on the right side to thank the user that helped you.

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
New Developer
hobby_johals
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Custom ObjectChoiseField

can provide us the png images that u have used in thie object choice field........

Amar
Please use plain text.
New Developer
hobby_johals
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Custom ObjectChoiseField

Very nice work dude...it realy helped me...Thnx

Amar
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: Custom ObjectChoiseField

If it works................

============================================================

feel free to press the like button on the right side to thank the user that helped you.

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: Custom ObjectChoiseField

I attached it check it once.

=====================================================================================

feel free to press the like button on the right side to thank the user that helped you.

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: Custom ObjectChoiseField

[ Edited ]

I use this image .Look it once.

 

dropdown-box-1.png

 

=====================================================================================

feel free to press the like button on the right side to thank the user that helped you.

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: Custom ObjectChoiseField

dropdown-box-1.png 

This image I used.

 

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Contributor
nilanchalapanigrahy
Posts: 23
Registered: ‎08-09-2010
My Device: 9800
My Carrier: Airtel

Re: Custom ObjectChoiceField

Formatted the above code

 

package mypackage;

import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.Graphics;
import java.lang.String;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.Font;

public class CMyChoiceField extends ObjectChoiceField {
	/** Constants for the horizontal text alignment */
	public static final int TEXT_HALINGMENT_LEFT = 0;
	public static final int TEXT_HALINGMENT_CENTER = 1;
	/** Array of choices */
	String[] m_Choices;
	/** Left margin of the field */
	private int m_MarginLeft = 1;
	/** Right margin of the field */
	private int m_MarginRight = 1;
	/** Top margin of the field */
	private int m_MarginTop = 1;
	/** Top margin of the field */
	private int m_MarginBottom = 1;
	/** Right margin of the text in the selectbox */
	private int m_TextMarginRight = 5;
	/** Left margin of the text in the selectbox */
	private int m_TextMarginLeft = 5;
	/** Horizonta text alingment */
	private int m_HTextAlignment = TEXT_HALINGMENT_CENTER;
	/** Width of the chpice box */
	private int m_ChoiceFieldWidth;
	/** Height of the field */
	private int m_Height;
	/** Determones is field focused */
	private boolean m_isFocused = false;
	/** Width of thr DropDown arrow */
	private int m_DropDownArrowWidth;
	/** Field default color */
	private int m_DefBgColor = 0xffffff;
	/** Field text default color */
	private int m_DefTextColor = 0;
	/** Field color when it is focused */
	private int m_FocusedBgColor = 0xf6921e;
	/** Field text color when it is focused */
	private int m_FocusedTextColor = 0;

	/**
	 * * Constructor. The width of choice box became as big as * maximum length
	 * of choice strings + text marfins + derop-down arrow width. * * @param
	 * String - label of the field * @param String[] - field choices
	 */

	public CMyChoiceField(String label, String[] choices) {
		super(label, choices);
		m_Choices = choices;
		// dimensions of selectable field
		Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = currFont.getHeight();
		m_ChoiceFieldWidth = currFont.getAdvance(choices[0]);

		for (int i = 1; i < choices.length; i++) {
			if (m_ChoiceFieldWidth < currFont.getAdvance(choices[i]))
				m_ChoiceFieldWidth = currFont.getAdvance(choices[i]);
		}

		m_ChoiceFieldWidth += m_TextMarginLeft + m_TextMarginRight
				+ m_DropDownArrowWidth;
	}

	/**
	 * * Constructor. The width of choice box became as big as * maximum length
	 * of choice strings + text marfins + derop-down arrow width. * * @param
	 * String - label of the field * @param String[] - field choices * @param int
	 * - initial choice index
	 */

	public CMyChoiceField(String label, String[] choices, int initialIndex) {
		super(label, choices, initialIndex);
		m_Choices = choices;
		// dimensions of selectable field
		Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = currFont.getHeight();
		m_ChoiceFieldWidth = currFont.getAdvance(choices[0]);
		for (int i = 1; i < choices.length; i++) {
			if (m_ChoiceFieldWidth < currFont.getAdvance(choices[i]))
				m_ChoiceFieldWidth = currFont.getAdvance(choices[i]);
		}
		m_ChoiceFieldWidth += m_TextMarginLeft + m_TextMarginRight
				+ m_DropDownArrowWidth;
	}

	/**
	 * * Constructor. The width of choice box became as big as * maximum length
	 * of choice strings + text marfins + derop-down arrow width. * * @param
	 * String - label of the field * @param String[] - field choices * @param int
	 * - initial choice index * @param long - field style(see ObjectChoiceField
	 * styles)
	 */
	public CMyChoiceField(String label, String[] choices, int initialIndex,
			long style) {
		super(label, choices, initialIndex, style);
		m_Choices = choices;
		// dimensions of selectable field
		Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = currFont.getHeight();
		m_ChoiceFieldWidth = currFont.getAdvance(choices[0]);
		for (int i = 1; i < choices.length; i++) {
			if (m_ChoiceFieldWidth < currFont.getAdvance(choices[i]))
				m_ChoiceFieldWidth = currFont.getAdvance(choices[i]);
		}
		m_ChoiceFieldWidth += m_TextMarginLeft + m_TextMarginRight
				+ m_DropDownArrowWidth;
	}

	/** * Set field choices * * @param String[] - array of field choices */
	public void setChoices(Object[] choices) {
		super.setChoices(choices);
		m_Choices = (String[]) choices;
	}

	/**
	 * * Set field margins. This method takes an effect for * parameter to be >=
	 * 0. * * @param int - left margin of the field * @param int - right margin
	 * of the field * @param int - top margin of the field * @param int - bottom
	 * margin of the field
	 */

	public void setMargins(int marginLeft, int marginRight, int marginTop,
			int marginBottom) {
		if (marginLeft >= 0)
			this.m_MarginLeft = marginLeft;
		if (marginRight >= 0)
			this.m_MarginRight = marginRight;
		if (marginTop >= 0)
			this.m_MarginTop = marginTop;
		if (marginBottom >= 0)
			this.m_MarginBottom = marginBottom;
	}

	/**
	 * * Set horizontal text alignment in the text box. * * @param int -
	 * alignment of the text
	 */
	public void setTextHAlignment(int alignment) {
		m_HTextAlignment = alignment;
	}

	/** * Set choice box width. * * @param int - choice box width. */
	public void setChoiceWidth(int width) {
		m_ChoiceFieldWidth = width;
	}

	/**
	 * * Set text font. * * @param int - font style (see Font class) * @param
	 * int - font height * @param boolean - if true control height updates
	 * according to the new font height.
	 */
	public void setTextFont(int style, int height, boolean updateHeight) {
		this.setFont(getFont().derive(style, height));
		if (updateHeight)
			m_DropDownArrowWidth = m_Height = getFont().getHeight();
	}

	/**
	 * * Method retrieves this field's preferred width. * * @return int -
	 * field's preferred width
	 */
	public int getPreferredWidth() {
		return m_MarginLeft + m_MarginRight + m_ChoiceFieldWidth
				+ getFont().getAdvance(getLabel());
	}

	/**
	 * * Method retrieves this field's preferred height. * * @return int -
	 * field's preferred height
	 */
	public int getPreferredHeight() {
		return m_MarginTop + m_MarginBottom + m_Height;
	}

	/**
	 * * Determines if this field accepts the focus. * * @return boolean -
	 * always true.
	 */
	public boolean isFocusable() {
		return true;

	}

	/**
	 * * Retrieves this field's current focus region. * * @param XYRect - object
	 * to contain the focus rect for this field in local coordinates
	 */
	public void getFocusRect(XYRect rect) {
		rect.set(m_MarginLeft + getFont().getAdvance(getLabel()), m_MarginTop,
				m_ChoiceFieldWidth, m_Height);
	}

	/**
	 * * Draws the focus indicator for this field * * @param Graphics - graphics
	 * context for drawing the focus * @param boolean - true if the focus should
	 * be set; otherwise, false.
	 */
	protected void drawFocus(Graphics graphics, boolean on) {
		invalidate();
	}

	/**
	 * * Lays out field contents. * * @param int - amount of available
	 * horizontal space. * @param int - amount of available vertical space.
	 */
	protected void layout(int width, int height) {
		setExtent(Math.min(getPreferredWidth(), width),
				Math.min(getPreferredHeight(), height));
	}

	/**
	 * * Invoked when a field receives the focus. * * @param int - Indicates
	 * from which direction the focus enters the field.
	 */
	public void onFocus(int direction) {
		m_isFocused = true;
		invalidate();
	}

	/** * Invoked when a field loses the focus. */
	public void onUnfocus() {
		m_isFocused = false;
		invalidate();
	}

	/** * Method draws a edit field. * * @param Graphics - graphic context. */
	public void paint(Graphics g) {
		// needed variables
		String visibleText = (getSelectedIndex() == -1 || getSelectedIndex() >= m_Choices.length) ? "": m_Choices[getSelectedIndex()];
		int textLength = getFont().getAdvance(visibleText);
		int labelLength = getFont().getAdvance(getLabel());
		int textY = m_MarginTop + (m_Height - getFont().getHeight()) / 2;
		if (textY < 0)
			textY = m_MarginTop;
		
		// Text can be as big as text box field
		while (textLength > m_ChoiceFieldWidth - m_TextMarginLeft - m_TextMarginRight - m_DropDownArrowWidth && visibleText.length() > 0) {
			visibleText = visibleText.substring(0, visibleText.length() - 1);
			textLength = getFont().getAdvance(visibleText);
		}
		
		int textX = (m_HTextAlignment == TEXT_HALINGMENT_CENTER) ? m_MarginLeft	+ labelLength + m_TextMarginLeft	+ (m_ChoiceFieldWidth - m_TextMarginLeft - m_TextMarginRight - m_DropDownArrowWidth - textLength) / 2 : m_MarginLeft + labelLength + m_TextMarginLeft;
		if (textX < 0)
			textX = m_MarginLeft + labelLength + m_TextMarginLeft;
		// draw a lable
		g.setColor(0);
		g.drawText(getLabel(), m_MarginLeft, textY);
		if (m_isFocused == false) {
			// Draw edit box bg
			g.setColor(m_DefBgColor);
			g.fillRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height);
			// Draw edit box rect
			g.setColor(0);
			g.drawRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height);
			// Draw edit box text
			g.setColor(m_DefTextColor);
			g.drawText(visibleText, textX, textY);
			// g.drawText(Integer.toString(cursorPosition), m_MarginLeft +
			// labelLength + m_TextMarginLeft, textY);
		}

		else {
			// Draw edit box bg
			g.setColor(m_FocusedBgColor);
			g.fillRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height);
			// Draw edit box rect
			g.setColor(0);
			g.drawRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height);
			// Draw edit box text
			g.setColor(m_FocusedTextColor);
			g.drawText(visibleText, textX, textY);
		}
		// draw DropDown arrow
		int ddaLeft = m_MarginLeft + labelLength + m_ChoiceFieldWidth - m_DropDownArrowWidth;
		// fill arrow
		g.setColor(0xc6d4e9);
		g.fillRect(ddaLeft, m_MarginTop, m_DropDownArrowWidth, m_Height / 2);
		int leftPoint = ddaLeft, rightPoint = ddaLeft + m_DropDownArrowWidth - 1, height = m_MarginTop + m_Height / 2;
		int bottom = m_MarginTop + m_Height - 1;
		
		for (; height < bottom; height++, leftPoint++, rightPoint--) 
			g.drawLine(leftPoint, height, rightPoint, height);
		
		// outer line
		g.setColor(0);
		g.drawLine(ddaLeft, m_MarginTop, ddaLeft + m_DropDownArrowWidth - 1, m_MarginTop);
		g.drawLine(ddaLeft + m_DropDownArrowWidth - 1, m_MarginTop, ddaLeft + m_DropDownArrowWidth - 1, m_MarginTop + m_Height / 2);
		g.drawLine(ddaLeft + m_DropDownArrowWidth - 1, m_MarginTop + m_Height / 2, ddaLeft + m_DropDownArrowWidth / 2, m_MarginTop + m_Height - 1);
		g.drawLine(ddaLeft + m_DropDownArrowWidth / 2, m_MarginTop + m_Height - 1, ddaLeft, m_MarginTop + m_Height / 2);
		g.drawLine(ddaLeft, m_MarginTop + m_Height / 2, ddaLeft, m_MarginTop);

		// inner line
		g.drawLine(ddaLeft + 1, m_MarginTop + 1, ddaLeft + m_DropDownArrowWidth - 2, m_MarginTop + 1);
		g.drawLine(ddaLeft + m_DropDownArrowWidth - 2, m_MarginTop + 1, ddaLeft + m_DropDownArrowWidth - 2, m_MarginTop + m_Height / 2);
		g.drawLine(ddaLeft + m_DropDownArrowWidth - 2, m_MarginTop + m_Height / 2, ddaLeft + m_DropDownArrowWidth / 2, m_MarginTop + m_Height - 2);
		g.drawLine(ddaLeft + m_DropDownArrowWidth / 2, m_MarginTop + m_Height - 2, ddaLeft + 1, m_MarginTop + m_Height / 2);
		g.drawLine(ddaLeft + 1, m_MarginTop + m_Height / 2, ddaLeft + 1,m_MarginTop + 1);

	}
};

 

Please use plain text.
Trusted Contributor
avi_yach
Posts: 184
Registered: ‎11-26-2010
My Device: Not Specified

Re: Custom ObjectChoiceField

Hello all,

I am going to use the customobjectchoice field attached in this thread. But, I would like to know from the members who used it if they faced any issues at OS level or Device level. I have faced issues with Customfield with OS level and device levels before. My targeted OS are 5, 6 and 7.

Will be happy to hear from you guys!! :smileyhappy:

Regards,

A Y.

Please use plain text.