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: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom
Accepted Solution

Focus question of EditField and LabelField in HorizontalFieldManager

    I want to made a customized editfield (a label with a textbox), when it was got focus on that the textbox will be draw a highlight border, and I want the label got the same effect, however, I can drawn a border outside the textbox easily because of the textbox was focusable, below is my code:

 

public class MyTextField extends EditField
{
	public MyTextField()
	{
		super();
	}

	public void layout(int width, int height)
	{
		super.layout(width, height);
	}
	
	protected void paint(Graphics g)
	{
		if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
		{
			setBorder(BorderFactory.createRoundedBorder(new XYEdges(6, 6, 6, 6), 0x186DEF, Border.STYLE_SOLID));
		}else{
			setBorder(BorderFactory.createRoundedBorder(new XYEdges(6, 6, 6, 6), Color.GRAY, Border.STYLE_SOLID));
		}

		super.paint(g);
	 }
}

 

    And I use this code to show them in mainscreen

 

    	HorizontalFieldManager hfm=new HorizontalFieldManager();
    	LabelField label=new LabelField("something");
    	MyTextField text=new MyTextField();
            
    	hfm.add(label);
    	hfm.add(text);
    	add(hfm);

 

    Is there any way to implement that when the textbox got focus at the same time also changed the color of the label text.

Developer
Posts: 16,676
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Focus question of EditField and LabelField in HorizontalFieldManager

define a boolean flag that you check in paint, set it on the field (from outside) and call invalidate to redraw.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Focus question of EditField and LabelField in HorizontalFieldManager

thanks simon, but I really don't know how to deliver a parameter from textbox to label, I mean how to notify the label that its partner was already got focus?

Developer
Posts: 16,676
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Focus question of EditField and LabelField in HorizontalFieldManager

If you don't want to extend the class you can use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/ui/Field.html#setCookie(java.l... to set a flag on the target field.
Another option would be to have a reference to the field in your screen and check field.isFocus() from your other field.

I think the most elegant solution would be to add a focuschangelistener to the field that triggers the redraw.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Focus question of EditField and LabelField in HorizontalFieldManager

[ Edited ]

thanks again, simon, you're right, in fact I'm using 3 extend classes to support my wish, and I'm not agree that using the focuschangelistener to monitor the changes of focus, because of I have to made a lots of this combination field on main screen, I think its will be using a lots of system resource.

 

Below is my codes

 

class MyHorizontalFieldManager

public class MyHorizontalFieldManager extends HorizontalFieldManager
{
	public MyHorizontalFieldManager()
	{
		super(NO_VERTICAL_SCROLL);
	}
    
	protected void paint(Graphics g)
	{
		super.paint(g);
	    
		Field child1=getField(0);
		Field child2=getField(1);
		
		((MyLabelField) child2)._flag=((MyTextField) child1).isFocus() ? true : false;
	} 
}

 

class MyLabelField

public class MyLabelField extends LabelField
{
	public boolean _flag;
	
	public MyLabelField(String label)
	{
		super(label);
	}
	
	public void layout(int width, int height)
	{
		super.layout(width, height);
	}
	
	protected void paint(Graphics g)
	{
		int oldColor=g.getColor();

		try
		{
			if(_flag)
			{
				g.setColor(Color.BLUE);
			}else{
				g.setColor(Color.BLACK);
			}
    	
			super.paint(g);
		} finally {
			g.setColor(oldColor);
		}
	} 
}

 

class MyTextField

public class MyTextField extends EditField
{
	public MyTextField()
	{
		super(TextField.NO_NEWLINE | EditField.FILTER_NUMERIC);
	}
	
	public void layout(int width, int height)
	{
		super.layout(width, height);
	}
	
	protected void paint(Graphics g)
	{
		if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
		{
			setBorder(BorderFactory.createRoundedBorder(new XYEdges(6, 6, 6, 6), Color.BLUE, Border.STYLE_SOLID));
		}else{
setBorder(BorderFactory.createRoundedBorder(new XYEdges(6, 6, 6, 6), Color.GRAY, Border.STYLE_SOLID)); } super.paint(g); } }

 

Developer
Posts: 16,676
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Focus question of EditField and LabelField in HorizontalFieldManager

In the labelfield, instead of drawText i would suggest to call super, otherwise you lose a lot of the processing behind the scenes.

I would also suggest that you re-set the color of the graphics object. store the getColor value, set your own, process super and set it to the stored value. The graphics object can be used in different places and changing its color can have unintentional side-effects.

overriding layout and just calling super makes little sense btw :smileyhappy:
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Focus question of EditField and LabelField in HorizontalFieldManager

I have been updated the class MyLabelField, and I need more and more suggestions about extent class, so could you please re-check again my codes?:Punk:

Developer
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Focus question of EditField and LabelField in HorizontalFieldManager

hi simon, I'm facing a new problem which when the label update its color by using super.invalidate() or super.updateLayout(), this may cause a highlevel cpu usage and the simulator or device will be slow down.

 

class MyLabelField extends LabelField
{
	private boolean _isFocus ;
      
	public MyLabelField(String label)
	{
		super(label);
	}

	public void isFocus(boolean on)
	{
		_isFocus=on;
		//super.updateLayout();
		//super.invalidate();
	}
	
	public void layout(int width, int height)
	{
		super.layout(width, height);
	}
	
	protected void paint(Graphics g)
	{
		int oldColor=g.getColor();
            
		try {
			if(_isFocus)
			{
				g.setColor(Color.BLUE);
			} else {
				g.setColor(Color.BLACK);
			}
                  
			super.paint(g);
		} finally {
			g.setColor(oldColor);
		}
	}
}

 

Other class can be using label.isFocus(boolean) to invoke it.

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

Re: Focus question of EditField and LabelField in HorizontalFieldManager

invalidate() to paint.

 

Only use updateLayout if format of the Field changes,

Developer
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Focus question of EditField and LabelField in HorizontalFieldManager

Roger that, but invalidate() is really damage, I can not accept high level cpu usage only used for one method.