07-05-2010 01:48 PM
hi
can anyone tell me how i can fixed a width of objectchoicefield.
suppose there are two values in objectchoicefield.
"abc"
"abcdefg"
now i want that the width should be equal to max String.
when i select abc the objectchoicefield become smaller as compared to "abcdefg".
now i want that the width should be equal to max String.
Solved! Go to Solution.
07-05-2010 02:17 PM
Check this thread.
Regards
Bikas
07-05-2010 02:49 PM
hi bikas
i want that the size of objectchoicefield remain same for all options.
here Size of Euro is smaller but i want it equal to Australian Dollar.
07-05-2010 03:00 PM
Hi,
You have to customized the ObjectChoiceField for fixing its width.
The above link in my earlier post should give you an idea how to do it.
Then set the width of your customized ObjectChoiceField to the length of the longest sting.
Regards
Bikas
07-05-2010 03:08 PM
public CustomComboBox(final String[] listValues, final int width) {
super(VERTICAL_SCROLL);
bmpComboArrow = Bitmap.getBitmapResource("pic/down-arrow.png");
bmfArrow = new BitmapField(bmpComboArrow);
managerWidth = width;
// managerHeight = (bmpComboArrow.getHeight()>
// (Font.getDefault().getHeight()+5))?(bmpComboArrow.
managerHeight = bmpComboArrow.getHeight();
HorizontalFieldManager hfm = new HorizontalFieldManager(
HorizontalFieldManager.FIELD_VCENTER);
// final Font fnt = Font.getDefault().derive(Font.PLAIN);
choiceField = new ObjectChoiceField("", listValues, 0,
Field.FIELD_VCENTER | Field.FIELD_LEFT | Field.EDITABLE) {
public void paint(Graphics graphics) {
FontFamily fontfam[] = FontFamily.getFontFamilies();
Font lblFont;
//lblFont = fontfam[0].getFont(FontFamily.TRUE_TYPE_FONT, 14);
lblFont = fontfam[0].getFont(FontFamily.CBTF_FONT, 16).derive(
Font.PLAIN);
graphics.setFont(lblFont);
this.setFont(lblFont);
graphics.clear();
// graphics.drawText(listValues[this.getSelectedIndex
// (getWidth() - this.getFont().getHeight()) >> 1);
// invalidate();
super.paint(graphics);
}
public int getPreferredWidth() {
return managerWidth;
}
public int getPreferredHeight()
{
return managerHeight;
}
protected void layout(int width, int height) {
width = getPreferredWidth();
height = getPreferredHeight();
super.layout(width, height);
super.setExtent(width, height);
}
public void getFocusRect(XYRect rect)
{
rect.set(getFont().getAdvance(getLabel()), 0, managerWidth,managerHeight);
}
};
/*int top = (bmpComboArrow.getHeight() - choiceField.getFont()
.getHeight()) >> 1;
choiceField.setMargin(top, 0, top, 0);*/
hfm.add(choiceField);
//hfm.add(bmfArrow);
// hfm.setMargin(5, 0, 5, 0);
add(hfm);
}
//call from other class
i have passed the value 210 that is max.
but my problem is that when i select small string option the objectchoicefield became small and when i select max String option the objectchoicefield became bigger.
again i said that i want the size=maximum whatever option i select.
07-05-2010 04:14 PM
Hi,
I think you should write a customized class overring ObjectChoiceField by overring layout, paint, drawfocus etc. to satisfy you needs.
Ok here is one of my Customized ObjectChoiceField class. Copy pasted from one of my old code.
I think just playing a litte bit with the code you will should be able make it like yours.
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;
import net.rim.device.api.system.Bitmap;
public class CustomChoiceField 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_PaddingTop = 5;
/** Top margin of the field */
private int m_PaddingBottom = 5;
/** Top margin of the field */
private int m_MarginTop = 5;
/** Top margin of the field */
private int m_MarginBottom = 5;
/** 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 CustomChoiceField(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 CustomChoiceField(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 CustomChoiceField(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_PaddingTop + m_PaddingBottom + 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 + m_PaddingBottom + m_PaddingTop);
}
/**
* 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)
{
Bitmap image = Bitmap.getBitmapResource("dropdown.png");
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_PaddingTop + (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;
g.setColor(0);
g.drawText(getLabel(), m_MarginLeft, textY);
if(m_isFocused == false)
{
image = Utils.resizeBitmap(image, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop);
/*
//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.drawBitmap(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop, image, 0, 0);
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);
/*m_FocusedBgColor = ApplicationTheme.DEFAULT_TEXT_BOX_BACKGROUND_COLOR _ON_FOCUS;
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 bg
g.setColor(m_DefBgColor);
g.fillRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height);
*/
image = Utils.resizeBitmap(image, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop);
g.drawBitmap(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop, image, 0, 0);
//Draw edit box rect
g.setColor(0);
g.drawRect(m_MarginLeft + labelLength, m_MarginTop, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop);
//Draw edit box text
//Draw edit box text
g.setColor(m_FocusedTextColor);
g.drawText(visibleText, textX, textY);
}
}
}
Let me know if you still have any problem.
Regards
Bikas
10-09-2010 03:23 AM
you can use the setMinimalWidth() method to set width of field.
ObjectChoiceField list = new ObjectChoiceField("", h, 0, Field.FIELD_LEFT);
list.setMinimalWidth(200);
add(list);
04-19-2011 08:58 AM
Hi,
I want to Customize my ObjectChoiceField, and your given code is very help full for me,
but i did't find Utils.
image = Utils.resizeBitmap(image, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop);
so please send me whole source code if possible.
Thanks in Advance.
07-27-2011 03:17 AM
Hi Bikas,
ur code is very useful..but unable to implement due to missing
Utils.resizeBitmap function...
can u post it,if possible for u
Thanks
07-28-2011 03:35 AM
Hi,
You should start a new thread otherwise no one will reply.
check this for CustomObjectChoiceField
http://supportforums.blackberry.com/t5/Java-Develo
Thanks & Regards
pp