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
Highlighted
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel
Accepted Solution

Custom Field, drawing focus problem

I have a class that extends Field. So I have basicallly created my own Field, however. I want to change the focus color and style from the default blue and filled rectangle. So I tried looking on the forum for help, I found all related posts. But none have helped me yet. Here is how my drawFocus looks. There is no change in the appearance.

 

}
...

	public void drawFocus(Graphics graphics, boolean on)
	{						
		graphics.setColor(Color.GRAY);
                graphics.drawRoundRect(0, 0, 180, 40, 5, 5);
		this.drawHighlightRegion(graphics, HIGHLIGHT_FOCUS, true, 0, 0, 180, 40);		
	}

...
}

 

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Custom Field, drawing focus problem

Drop that drawHighlightRegion - this thing overrides your own choice of colors and picks them from theme. Even though it is recommended by the docs, it is in fact not necessary. You are extending a Field, so do whatever custom things you want to be doing.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Field, drawing focus problem

So, take out the drawHighlightRegion; ok. So will it work with just those other two things there?
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Custom Field, drawing focus problem

If all you want to show is a gray rounded rectangle, then yes. Otherwise don't forget to paint the rest.of the details.

 

In fact, you'd better define drawFocus to do nothing and instead analyze graphics object in paint with isDrawingStyleSet(DRAWSTYLE_FOCUS) (or simply isFocus()). Then make your color decisions based on that.

 

Another remark: get into a habit of saving the graphics state before making changes to it and restoring them afterwards.

Something like this:

int oldColor = graphics.getColor();
graphics.setColor(...);
...
graphics.setColor(oldColor);

 Same is true for font, background color, DRAWSTYLE_ANTIALIASED, etc. - you will prevent many inexplicable errors from happening.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Field, drawing focus problem

Ok thank you again, but there is a bit of abstraction in this statement of yours,"and instead analyze graphics object in paint with isDrawingStyleSet(DRAWSTYLE_FOCUS) (or simply isFocus())."

I'm not quite I understand what you mean.
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Custom Field, drawing focus problem

Like this:

protected void drawFocus(Graphics g, boolean on) {
}

protected void paint(Graphics g) {
  int oldColor = g.getColor();
  int newColor;
  if (isFocus()) { // or g.isDrawingStyleSet(DRAWSTYLE_FOCUS)
    newColor = ...;
  } else {
    newColor = ...;
  }
  g.setColor(newColor);
  ...
  g.setColor(oldColor);
}

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Field, drawing focus problem

[ Edited ]

Oh I understand now; I should put custom features inside the paint methos. Are you sure this will work?

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Field, drawing focus problem

This doesn't work. When I overide the drawFocus to do nothing. The field doesn't even get highlighted at all. Any suggestion please?
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Custom Field, drawing focus problem

Oops - the field doesn't repaint because it doesn't know it should.

Anyway - try this drawFocus override:



	public void drawFocus(Graphics graphics, boolean on)
	{						
	 paint(graphics); 
	}

This way your paint will be called when the field gets focus.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Field, drawing focus problem

Ok, a little better but this only succeeds now in changing the font color, when I use setColor. I want to change the highlight color,
What I did was this. repaintedt he entire field. However when I chanfe the color using set Color. The font and the highlight Color Changes to red, so I can't see the text at all. Is there any way around this?

 

 

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.