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
Regular Contributor
Posts: 60
Registered: ‎02-24-2011
My Device: Not Specified

Custom Edit Field with unexpected behaviour

Hello, I developed a custom EditField in order to be able to alter it's background color and layout, also adding the possibility to specify a fixed height when setting a property. It works fine, however I found 2 problems, the first one is that if I set USE_ALL_HEIGHT, and then try to add anything below that field, the EditField will override them. occupying all of the screen that is left. And second, If I set a custom  height, the field will not scroll vertically when the text surpasses the draw space. How could I fix that 2 problems, any suggestion would be great. This is the code of the field.

 

package ec.com.smx.workflow.movil.gui;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.EditField;

public class RectangularEditField extends EditField {

	//Default to White
	private int COLOR_BACKGROUND = 0xFFFFFF;
	private int BLACK_COLOR = 0x000000;
	private XYEdges padding;
	
	int customHeight=20;
	int height;

	public RectangularEditField(String defaultLabel, long style, XYEdges padding) {
		super(style);
		if (padding == null) {
			this.padding = new XYEdges(0, 0, 0, 0);
		} else {
			this.padding = padding;
		}
		if (defaultLabel != null)
			this.setText(defaultLabel);
		this.setPadding(padding);
	}
	
	public RectangularEditField(String defaultLabel, int height , long style, XYEdges padding) {
		this(defaultLabel, style, padding);	
		this.height=height;
		customHeight=height;
	}


	public void layout(int width, int height) {
		int newHeight = height - (padding.top + padding.bottom);
		if(!isStyle(Field.USE_ALL_HEIGHT)){
			newHeight = customHeight;
		}
		super.layout(width, newHeight);
		setExtent(width, newHeight);
			
		
	}

	protected void paintBackground(Graphics g) {
		// White Color
		int background = COLOR_BACKGROUND;

		g.setColor(background);
		g.fillRect(padding.left, 0, getWidth() - (padding.left + padding.right),
				getHeight());
		g.drawRect(padding.left, 0, getWidth() - (padding.left + padding.right),
				getHeight());
		super.paintBackground(g);
		g.setColor(BLACK_COLOR);		
		
	}

	public void setBackgroundColor(int color) {
		COLOR_BACKGROUND = color;
	}
	
	public void setCustomHeight(int height){
		customHeight = height;
	}
	
	public int getOriginalHeight(){
		return height;
	}
}

 

 

Thanks a lot.

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Custom Edit Field with unexpected behaviour

There is a vertically scrolling, limited height edit field implementation which can be easily extended to unclude backgrounds and such. Check this knowledge base article:

TextBoxField revisited

Good luck!

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!