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: 226
Registered: ‎10-01-2011
My Device: Passport
Accepted Solution

How to control selected area in field

I am trying to find how to control the selected are of a field so when the field is selected only a portion of the field is highlighted. I can't find any way to do this other then embed a small field inside a largr non-selectable field.  

 

Any suggestions on how to do this?

 

Thanks!

 

Developer
Posts: 543
Registered: ‎12-23-2010
My Device: Z10

Re: How to control selected area in field

Hi crispoyz,

 

You can override fields drawfocus method to do this.

 

Try with this code. This is a sample code just for understanding.

 

 

class CustomField extends Field
    {
    	int width = 100;
    	int height = 100;
    	String _text;
    	
    	public CustomField(String text) 
    	{
    		super(Field.FOCUSABLE);
			_text = text;
		}

		protected void layout(int width, int height) {
			setExtent(getPreferredWidth(), getPreferredHeight());
			
		}

		protected void drawFocus(Graphics graphics, boolean on) 
		{
			if(on)
			{
				graphics.setColor(Color.GREEN);
				graphics.fillRect(20, 10, getPreferredWidth()-50, getPreferredHeight()-30);
				graphics.setColor(Color.RED);
				graphics.drawText(_text, (width - getFont().getAdvance(_text))/2,(height - getFont().getHeight())/2);
			}
		}
		protected void paint(Graphics graphics) {
			graphics.setColor(Color.RED);
			graphics.drawRoundRect(0, 0, getPreferredWidth(), getPreferredHeight(), 20, 20);
			graphics.drawText(_text, (width - getFont().getAdvance(_text))/2,(height - getFont().getHeight())/2);
			
		}
    	
		public int getPreferredWidth() {
			// TODO Auto-generated method stub
			return width;
		}
		public int getPreferredHeight() {
			// TODO Auto-generated method stub
			return height;
		}
    }

  Thanks.