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
Contributor
Posts: 23
Registered: ‎03-30-2011
My Device: Several Simulators
My Carrier: none - simulator only

Almost a solution

[ Edited ]

OK, I found a semi solution.

 

I also have TwoLineEditField and TwoLineChoiceField. So the only thing different in TwoLineDateField is that the DateField itself has several focus states. If it displays e.g. the time, the focus can either be on the hours or the minutes. So I think this" confuses" the focus system.

 

I changed the TwoLineDateField class, so that the DateField inside it can no longer get the focus. Instead the TwoLineDateField gets the focus and also handles the navigation click.

 

No everything works well except, that there is no visual feedback, that the TwoLineDateField has focus.

 

If someone now can help me how I can get this nice blue Background by overriding drawFocus() , that would be fantastic.

 

Here is my TwoLineDateField class. I leave work now (it's 4:40 pm over here).

 

Hope you can help me and I will check this post here tomorrow again.

 

Thanks Peter

 

 

public class TwoLineDateField extends VerticalFieldManager {
	
	private DateField _dateField;
	private String _dateformat="dd-MMM-yyyy";
	private String _timeformat="HH:mm";
	
    public TwoLineDateField(String label, long date, long style, String dateformat,String timeformat) {
        super(NO_HORIZONTAL_SCROLL);
        _dateformat=dateformat;
        _timeformat=timeformat;
        
        //add the label in the first row of the vertical manager
        add(new LabelField(label));
        
        
        //the actual datefield
        _dateField = new DateField("", date, style | Field.READONLY)
        {
        	
        	//draw a rectangle around the DateField, to indicate, this is a field waiting for user input
            public void paint(final Graphics g)
            {
                g.drawRoundRect(0, 0, _dateField.getWidth(), _dateField.getHeight(), 10, 10);
                super.paint(g);
            }
            
            
            
            public boolean isFocusable() {  
                return false;  
            }  
            
        };
        add(_dateField);

    }

    public long getDate() {
        return _dateField.getDate();
    }
    
    public void setDate(long date){
    	this._dateField.setDate(date);
    }
    
    public boolean isFocusable() {  
        return true;  
    }
    
  //when clicking the datefield pop up a datePicker Dialog
    protected boolean navigationClick(final int status, final int time)
    {
    	Calendar cal=Calendar.getInstance();
    	cal.setTime(new Date(_dateField.getDate()));
        final DateTimePicker datePicker = DateTimePicker.createInstance(cal,_dateformat,_timeformat);
        if (datePicker.doModal())
        {
            this._dateField.setDate(datePicker.getDateTime().getTime());
        }

        return true;
    }

}

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Almost a solution

[ Edited ]

Removed the DateTimePicker becuase I was doing testing in 4.2.1 (which is a lot faster).  Anyway, add it back in and try the code below.

 

This is not really production quality code, I just stuck things in as quickly as I could,  But hopefully you can spruce it up where you think you need to.

 

And I managed to reproduce your error in a 9550 Simulator with your code and not with this code!. 

 

I recommend you review this KB article.  The code I have hacked into this was really from there:

http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-manage...

 

[Aside: Next time when someone asks for the level of a Simulator, could you use the Options, and then About, on the Simulator iteself if someone asks you for its OS level.  Same on a device.  ]

 

class TwoLineDateField extends VerticalFieldManager {
  
       private DateField _dateField;
       private LabelField _dateLabel;
  private String _dateformat="dd-MMM-yyyy";
      private String _timeformat="HH:mm";
   
    public TwoLineDateField(String label, long date, long style, String dateformat,String timeformat) {
        super();
        _dateformat=dateformat;
        _timeformat=timeformat;
       
        //add the label in the first row of the vertical manager
        add(new LabelField(label));
       
       
        //the actual datefield
        _dateField = new DateField("", date, style | Field.READONLY);
        _dateLabel = new LabelField(_dateField.toString(),LabelField.FOCUSABLE | LabelField.USE_ALL_WIDTH | DrawStyle.RIGHT )        
        {
            protected void drawFocus( Graphics g, boolean on ) {
                boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
                try {
                    if( on ) {
                        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
                    }
                    this.paint(g);
                } finally {
                    g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
                }
            }

               //draw a rectangle around the DateField, to indicate, this is a field waiting for user input
            public void paint(final Graphics g)
            {
                int currCol = g.getColor();
                if ( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ) {
                    try {
                        g.setColor(0x186DEF);
                        g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
                    } finally {
                        g.setColor(currCol);
                    }
                }
                g.drawRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
                if ( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ) {
                    g.setColor(0x00FFFFFF);
                }
                super.paint(g);
                g.setColor(currCol);
            }
           
        };
        add(_dateLabel);

    }

    public long getDate() {
        return _dateField.getDate();
    }
   
    public void setDate(long date){
            this._dateField.setDate(date);
            this._dateLabel.setText(_dateField.toString());
    }
   
    public boolean isFocusable() { 
        return true; 
    }
   
  //when clicking the datefield pop up a datePicker Dialog
    protected boolean navigationClick(final int status, final int time)
    {
      Calendar cal=Calendar.getInstance();
           cal.setTime(new Date(_dateField.getDate()));
        /*
        final DateTimePicker datePicker = DateTimePicker.createInstance(cal,_dateformat,_timeformat);
        if (datePicker.doModal())
        {
            this._dateField.setDate(datePicker.getDateTime().getTime());
        }
        */

        return true;
    }

}

Contributor
Posts: 23
Registered: ‎03-30-2011
My Device: Several Simulators
My Carrier: none - simulator only

Re: Almost a solution

Great!

 

I never thought of this and was stuck with solving the focus issue. This is a simple and clean solution. I also learned a lot from your code. In my opinion the way BB handles the layout and painting of UI components is not well documented.

 

Thanks again Peter! The solution works on 9700 and 9550

 

And finally: The level of my simulator....I figured it out.....is:

 

Blackberry 9550

v5.0.0.535 (Platform 2.13.0.1.108)

 

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Almost a solution

Great.

 

Can you mark this as solved then?