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
New Contributor
nwboy74
Posts: 7
Registered: ‎02-22-2010
My Device: 8110
My Carrier: AT&T
Accepted Solution

Custom Text Field

I'd like to implement a custom text field that displays its label on the LEADING edge of a line and its editable portion on the TRAILING edge in the same manner as a ChoiceField. Is there any documentation available to accomplish this (ie- sample code perhaps)? Or even if I could see the source code for ChoiceField. I'm developing for a Blackberry Pearl 8110 with 4.3.

Please use plain text.
Developer
mlisaoverdrive
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: Custom Text Field

Did you check the knowledgebase for code samples?

 

It's searchable...you can find it here:Developer Knowledgebase

Please use plain text.
New Contributor
nwboy74
Posts: 7
Registered: ‎02-22-2010
My Device: 8110
My Carrier: AT&T

Re: Custom Text Field

Yes. I've scoured the website and have not found anything that works. I'm not sure if it's just me or not, but this site is intolerably slow! It becomes quite frustrating when you're trying to look for a solution and it takes 30 seconds, 45 seconds, 90 seconds... to get a response from the server! Back to my original question... it's obvious that the functionality exists to allow separation of an edit field's label and entry portion since it is the standard for list components. But everywhere I look, the only answers are extremely vague and undocumented.

Please use plain text.
New Contributor
nwboy74
Posts: 7
Registered: ‎02-22-2010
My Device: 8110
My Carrier: AT&T

Re: Custom Text Field

I ended up solving this using horizontal layout managers inside a vertical manager. I have to use a separate label field for the accompanying edit field. As lines are added, I keep track of how wide the new label is. Then in sublayout align the edit fields according to the stored maximum width plus some variant. Couldn't get it to align all the way to the right side.

 

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.text.*;

public class MyLayoutManager extends VerticalFieldManager {
	protected int maxw;
	protected Font font;
	protected int font_height;
	private int size;
	
	public MyLayoutManager() {
		super(Field.USE_ALL_WIDTH);
		this.maxw = 0;
		this.font = Font.getDefault().derive(Font.PLAIN, 12);
		this.font_height = this.font.getHeight();
		this.size = 0;
	}
	
	public void addLine(String label, int initial) {
		MyEditField mef = new MyEditField(label, initial);
		int label_width = this.font.getAdvance(label);
		this.maxw = Math.max(label_width, this.maxw);
		this.add(mef);
		this.size++;
	}
	
	public int getPreferredHeight() {
		return this.size * this.font_height;
	}
	
	public int getPreferredWidth() {
		return 240;
	}
	
	protected void sublayout(int maxwidth, int maxheight) {
		int y = 0;
		for (int i = 0; i < this.size; i++) {
			Field f = this.getField(i);
			this.setPositionChild(f, 0, y);
			this.layoutChild(f, 240, this.getPreferredHeight());
			y += this.font_height;
		}
		this.setExtent(240, this.getPreferredHeight());
	}
	
	protected class MyEditField extends HorizontalFieldManager {
		private LabelField the_label;
		private JunkField the_value;
		
		public MyEditField(String label, int initial) {
			super(Field.USE_ALL_WIDTH);
			this.the_label = new LabelField(label);
			this.the_label.setFont(font);
			this.the_value = new JunkField(initial + "");
			this.the_value.setFont(font);
			this.add(this.the_label);
			this.add(this.the_value);
		}
		
		public int getPreferredHeight() {
			return font_height;
		}
		
		public int getPreferredWidth() {
			return 240;
		}
		
		protected void sublayout(int maxwidth, int maxheight) {
			Field f = this.getField(0);
			this.setPositionChild(f, 0, 0);
			this.layoutChild(f, maxwidth, maxheight);
			f = this.getField(1);
			this.setPositionChild(f, maxw + 20, 0);
			this.layoutChild(f, maxwidth, maxheight);
			this.setExtent(maxwidth, font_height);
		}
		
		private class JunkField extends EditField {
			private boolean firstFocus;
			
			public JunkField(String deflt) {
				super(Field.EDITABLE | Field.FOCUSABLE);
				this.setText(deflt);
				this.setFilter(new NumericTextFilter(TextFilter.NUMERIC));
				this.firstFocus = false;
			}
			
			protected void onFocus(int direction) {
				this.firstFocus = true;
			}
			
			protected boolean keyChar(char key, int status, int time) {
				if (this.firstFocus) {
					this.setText("");
					this.firstFocus = false;
				}
				return super.keyChar(key, status, time);
			}
		}
		
	}
}

 

Please use plain text.