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
pa4o85
Posts: 14
Registered: ‎12-04-2008
My Device: Not Specified
Accepted Solution

Another problem with DateField in JDE 4.2.0 !!??

I have a custom date field object. It looks like that:

[code]
public class CustomDateField extends HorizontalFieldManager {
  /** the width of the custom date field */
  private int preferredWidth;
  /** the height of the custom date field */
  private int preferredHeight;
  /** the date field */
  private DateField dateField;
  /** the checkbox field */
  private CheckboxField checkboxField;
 
  private boolean readOnly;
 
  /**
   * Constructs a new custom date field object.
   *
   * @param date - long
   * @param width - int
   * @param height - int
   * @param font - Font
   * @param rOnly - boolean
   */
  public CustomDateField(long date, int width, int height, Font font, boolean rOnly) {
    super(Manager.NO_HORIZONTAL_SCROLL);
   
    preferredWidth = width;
    preferredHeight = height;
    readOnly = rOnly;
   
    if (!readOnly) {
      preferredWidth = 100;
      checkboxField = new CheckboxField("", (date != Long.MIN_VALUE)) {
        protected boolean trackwheelClick(int status, int time) {
          if (getChecked()) {
            dateField.setEditable(false);
          } else {
            dateField.setEditable(true);
          }
          return false;
        }
      };
      checkboxField.setFont(font);
    }
   
    dateField = new DateField("", 0, new SimpleDateFormat("dd MMM yyyy"), DateField.DATE | DrawStyle.HCENTER) {
      protected boolean trackwheelClick(int status, int time) {
        if (isEditable()) {
          if (getDate() == Long.MIN_VALUE) {
            setDate(System.currentTimeMillis());
          }
          return false;
        } else if (!readOnly) {
          Dialog.alert("Check the check box to edit the date!");
        }
        return true;
      }
    };
    dateField.setFont(font);
    dateField.setDate(date);
   
    if (readOnly || (date == Long.MIN_VALUE)) {
      dateField.setEditable(false);
    }
   
    if (checkboxField != null) {
      add(checkboxField);
    }
    add(dateField);
  }
 
  public int getPreferredWidth() {
    return preferredWidth;
  }
 
  public int getPreferredHeight() {
    return preferredHeight;
  }

  public long getDate() {
    if (dateField.isEditable() || readOnly) {
      return dateField.getDate();
    }
    return Long.MIN_VALUE;
  }
 
}
[/code]

When I check the checkbox, the date field become editable, but I can focus only the day subfield of the date field. So the only way to change the months or years is to increment the day. Do you have any suggestions? Could the problem be the horizontal field manager (I've tried the FlowFieldManager too - no success) ? Thanks!
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

[ Edited ]

Compare the following with what you have coded. 

 

Note that I have removed the Font because these fields will inherit the Font from the Manager, so you can set the Font of the Manager as you want.  I've also removed the Height, because the manager will calculate that correctly - why code something you don't need?  I've added a FieldChangeListener to detect the the changes in the Checkbox - that will work when the user uses the Space as well as the Click.  I've removed the click detection on the DateField, because I don't think it is needed with the way this version use the editable attribute to decide if the Field is focusable or not.

 

I didn't look hard enough to understand some parts of your code especially as related to setting and getting the date, but I wasn't bothered about this, since I'm sure you can take whatever I supply as turn it into something for yourself.  Also apologies for formatting and naming.  This is just demonstration rather than production code.

 

Hope it works for you.

 

public class CustomDateField extends HorizontalFieldManager implements FieldChangeListener { /** the width of the custom date field */ private int _preferredWidth; /** the date field */ private DateField _dateField; /** the checkbox field */ private CheckboxField _checkboxField; private boolean _editable; private boolean _dateSet = false; private long _date = 0; /** * Constructs a new custom date field object. * * @param date - long * @param width - int * @param editable - boolean */ public CustomDateField(long date, int width, boolean editable) { super(Manager.NO_HORIZONTAL_SCROLL); _preferredWidth = width; _editable = editable; _date = date; if ( _date > 0 ) { _dateSet = true; } else { _date = System.currentTimeMillis(); } _checkboxField = new CheckboxField("", false) { public boolean isFocusable() { return this.isEditable(); } }; _dateField = new DateField("", _date, new SimpleDateFormat("dd MMM yyyy"), DateField.DATE | DrawStyle.HCENTER) { public boolean isFocusable() { return this.isEditable(); } }; _checkboxField.setEditable(_editable); _checkboxField.setChangeListener(this); add(_checkboxField); _dateField.setEditable(_checkboxField.getChecked()); add(_dateField); } public void setEditable(boolean editable){ if ( editable != _editable ) { _editable = editable; _checkboxField.setEditable(_editable); } } public int getPreferredWidth() { return _preferredWidth; } public long getDate() { if ( _dateField.isEditable() ) { return _dateField.getDate(); } return Long.MIN_VALUE; } public void fieldChanged(Field field, int context) { if ( field == _checkboxField ) { if (_checkboxField.getChecked()) { _dateField.setEditable(true); if ( !_dateSet ) { // reset if needed _dateField.setDate(System.currentTimeMillis()); } _dateField.setFocus(); } else { _dateField.setEditable(false); } } }; }

 

Message Edited by peter_strange on 01-16-2009 10:27 AM
Please use plain text.
New Developer
pa4o85
Posts: 14
Registered: ‎12-04-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

Hi, I have tried this one. I replaced my class with your version. I agree with the things you removed - font and height. I need the editable boolean argument that is passed to the constructor in order to decide if the date field is editable, when it is read only I don't need checkbox - that was my idea. Anyway, the main problem is still happening with the class you gave to me. I really have no idea, why this is happening. When I uses the DateField class without any extentions and custom date field classes the focus movement over the date field is ok. Thank you for the help and for your time!
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

[ Edited ]

Sorry about taking out the code that made the Checkbox optional - I'm sure you can add it back in if you decide to use my code! 

 

I've tried my code in the 4.2 JDE running the standard 4.2 simulator (8100) and on a real 8100 device (running 4.2.1 unfortunately) and it works fine.  I can scroll left and right on the DateField using the 'trackball' (once I have checked the Checkbox of course).  I have not tested your code because I originally thought the problem was in the way you were swallowing the click event, however now I don't believe that to be true (and I have edited my post above to remove that comment).  On reflection I'm not sure why you are seeing a problem.

 

Can you confirm the details of your environment (JDE level, simulator/device level).

Message Edited by peter_strange on 01-16-2009 10:36 AM
Please use plain text.
New Developer
pa4o85
Posts: 14
Registered: ‎12-04-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

The JDE is 4.2.0, the simulator is the standard, default simulator (8100). I've tried it too on Blackberry 8100 handheld. Maybe somehow I've suppressed the way the date field changes the focus over its own subfields?
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

Have you overridden navigationMovement anywhere?
Please use plain text.
New Developer
pa4o85
Posts: 14
Registered: ‎12-04-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

Nice ! I use navigationMovement. I removed it and the date field focus movement was ok. I just didn't know that this could be the problem. Thank you so much for the wasted time !!!
Please use plain text.
New Developer
pa4o85
Posts: 14
Registered: ‎12-04-2008
My Device: Not Specified

Re: Another problem with DateField in JDE 4.2.0 !!??

What do you think is wrong with the navigationMovement method? In fact I put a condition with if clause, and if the condition is true some thing are done and the method return true so that nothing else to be done, otherwise the method return false. So when I move the trachball the focus should be changed in the dateField as the method returns false and it doesn't make any influence!
Please use plain text.