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
Developer
Posts: 204
Registered: ‎07-09-2009
My Device: Not Specified

Correct width of list in ChoiceField

I have an ObjectChoice field with a bunch of numbers, i.e. 2.75, 1.5, etc., and it displays too narrow so many of the numbers are cut off with ellipses ("...").  Maybe this is because the font in the list is larger than the screen font for some reason.  The list may be calculating its width by the set screen font but then displaying the items in the default font.

 

Need info on how to set the font used in the list (the field itself displays in the smaller font, but the list in the default), or else make the list correctly size itself wide enough so all the items can fit.

 

Details:

 

In the MainScreen's main VerticalFieldManager, I add another VerticalFieldManager (just for this one field) with the ObjectChoiceField under it, so I can lay it out indented.  That subordinate VFM has USE_ALL_WIDTH set, but I create the ObjectChoiceField without that attribute because I don't want the list way over at the right, but next to the label.  In the VFM's sublayour(), I layoutChild() the OCF according to its preferred width unless that's wider than the manager (which it never is).  Either way, it does the same thing, the list pops up too narrow whether at the right or next to the label.

 

Thank you,

Highlighted
Developer
Posts: 204
Registered: ‎07-09-2009
My Device: Not Specified

Re: Correct width of list in ChoiceField

[ Edited ]

Might as well include the code, maybe some people will find an auto-indenting ObjectChoiceField useful once it works right:

 

class CiObjectChoiceField extends VerticalFieldManager implements FieldChangeListener {  // indenting
  ObjectChoiceField field;
  int indents, ordinal=0;
  FieldChangeListener listener=null;
  public CiObjectChoiceField(int indents_p, String label, Object[] choices) {
    super(VerticalFieldManager.USE_ALL_WIDTH);
    indents = indents_p;
    field = new ObjectChoiceField(label, choices);
    field.setChangeListener(this);
    add(field);
    }
  public void sublayout(int width, int height) {
    super.sublayout(width, height);
    int indent = getFont().getHeight() * indents;
    if ((field.getPreferredWidth() + indent) > width)
      layoutChild(field, width - indent, height);
    else
      layoutChild(field, field.getPreferredWidth(), height);
    setPositionChild(field, indent, 0);
    }
  public Object getChoice() {
    return field.getChoice(field.getSelectedIndex());
    }
  public String getText() {
    return field.getChoice(field.getSelectedIndex()).toString();
    }
  public int getInt() {
    return Integer.valueOf(getText()).intValue();
    }
  public float getFloat() {
    return Float.valueOf(getText()).floatValue();
    }
  public double getDouble() {
    return Double.valueOf(getText()).doubleValue();
    }
 public Object getChoice(int idx) {
    return field.getChoice(idx);
    }
  public int getSelectedIndex() {
    return field.getSelectedIndex();
    }
  public void setOrdinal(int ordinal_p) {
    ordinal = ordinal_p;
    }
  public void setCustomChangeListener(FieldChangeListener listener_p) {
    listener = listener_p;
    }
  public void fieldChanged(Field f, int context) {
    if (listener != null)
      listener.fieldChanged(this, ordinal);
    }
  }