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
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
Accepted Solution

labels are going out of the custom manager

Hi,

 

In my application, i am trying to add some labels in a custom vertical manager. The texts of the labels will change dynamically. So, i want to change the vertical manager height according to the change of label contents. Now i am trying to add one border of that manger and then adding the labels. But the labels are going out of the border like the image:

 

 

9900.png

 

For this manager. i wrote the below code. But it is not working::

 

	public PasswordScreen() {
		super(NO_HORIZONTAL_SCROLL);
		// GridFieldManager gm = setTitleFieldMgr();
		// setTitle(gm);
		vfmMailHeader = new VerticalFieldManager(NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL);
		vfmMailTop = new VerticalFieldManager(NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL) {

			protected void sublayout(int nMaxWidth, int nMaxHeight) {
				 Field field;
			       //get total number of fields within this manager
			       int numberOfFields = getFieldCount();
			       int x = 0;
			       int y = 0;
			       for (int i = 0;i < numberOfFields;i++) {
			           field = getField(i); //get the field
			           setPositionChild(field,x,y); //set the position for the field
			           layoutChild(field, nMaxWidth, nMaxHeight); //lay out the field
			           y = y + field.getHeight();
			       }
			       
			       super.sublayout(nMaxWidth, nMaxHeight);
			       setExtent(nMaxWidth, nMaxHeight);
			}

			protected void paint(Graphics g) {
				Logger.out("Decrypted Mail", "it is in the paint method");
				int nWidth = Display.getWidth();
				int nHight = getPreferredHeight();
				Logger.out("Decrypted Mail", "printed the height::   " + nHight);
				g.setColor(0x949CAD);
				g.fillRect(0, 0, nWidth, nHight);
				g.setColor(Color.WHITE);
				g.fillRoundRect(10, 10, nWidth - 20, nHight - 20, 10, 10);
				g.setColor(Color.BLACK);
				super.paint(g);
			}

			public int getPreferredWidth() {
				return (Display.getWidth());
			}

			public int getPreferredHeight() {
				int height = 0;
				int numberOfFields = getFieldCount();
				for (int i = 0; i < numberOfFields; i++) {
					height += getField(i).getPreferredHeight();
					Logger.out("Decrypted Mail", "each::   " + height);
				}
				Logger.out("Decrypted Mail", "total::   " + height);
				return height;
			}
		};
		hfmMailSub = new HorizontalFieldManager();
		lfMailSub = new LabelField("Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.", Field.FIELD_LEFT);
		try {
			lfMailSub.setFont(FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 26));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lfMailSub.setMargin(new XYEdges(10, 0, 0, 10));
		VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH);
		hfmMailSub.add(lfMailSub);
		hfmMailSub.add(vfm);
		lfMailFrom = new LabelField("From: <inno1> inno1.senditbank.com hhdhhfgjgdfgdfgdsjfggdf ", Field.FIELD_LEFT);
		try {
			lfMailFrom.setFont(FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 24));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lfMailFrom.setMargin(new XYEdges(10, 0, 0, 10));
		lfMailTo = new LabelField("To: <inno2> inno2.senditbank.com ", Field.FIELD_LEFT);
		try {
			lfMailTo.setFont(FontFamily.forName("BBAlpha Sans").getFont(Font.BOLD, 24));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lfMailTo.setMargin(new XYEdges(10, 0, 20, 10));
		vfmMailTop.add(hfmMailSub);
		vfmMailTop.add(lfMailFrom);
		vfmMailTop.add(lfMailTo);
		vfmMailTop.add(new LabelField());
		add(vfmMailTop);
}

   Can anyone tell me where is the wrong i am doing??/

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: labels are going out of the custom manager

Your sublayout is incorrect on many levels.

 

First, it looks like you are duplicating the job of VerticalFieldManager (without regard to field margins, though). Then you completely trash these results by calling super.sublayout. Finally, you are confusing the superclass by setting its extent (it is already set by super.sublayout and is probably included in subsequent paint calculations).

 

In fact, you don't have to do anything - setText on LabelField or any TextField invokes updateLayout which recalculates the layout (duh!) and repaints the screen. 

 

If you want to change your labels' font size and weight dynamically, simply call your Manager's updateLayout() after setting the font. No need for custom managers!

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: labels are going out of the custom manager

yes..i able to resolve the issue. in the getPrefferedHeight() method, i was trying to get the height of the fields. there instead of getHeight(), i used getPrefferedHeight(). :Sweatdrop: But finally i able to figure it out.

 

And arkadyz, thanks for giving the reply. i know their are lots of flaws in the code. primaryly i just tried without overriding the sublayout also. but when the text length is more or labelfield content is more, it was not working. But again you have gave the hint to use updateLayout() method for the labelfield. Can you please tell me how it will work for my code. I mean in the where i need to change for that. If you edit my code and change with that method, it will really big help..

 

Thanks..

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: labels are going out of the custom manager

What was the problem when the text length is too big? If it was still one line where you'd expect two, you'd better switch from LabelField to RichTextField or something similar. LabelField never word-wraps and creates the second line only when there is a newline character in its text.

 

As for updateLayout - you put it after a change which requires the fields to recalculate their dimensions but might not cause them to do that automatically. For example, setFont on LabelFields does not cause them to re-layout (unlike setText which properly does). So you follow the setFont statement with updateLayout() - let's say these statements are in your Manager or Screen code:

 

yourLabelField.setFont(...);

updateLayout();

 

Just don't make the mistake of adding that updateLayout to your sublayout or paint code - you'll create an infinite loop! 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: labels are going out of the custom manager

Thanks for your reply. Now i am using Richtextfield only. Is updatelayout will work for that also??  I will try tomorrow..

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: labels are going out of the custom manager

updateLayout works for everything - it's defined for Field.

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