10-28-2009 01:35 AM - edited 10-28-2009 01:37 AM
I'm attempting to have something like this on the screen (4.7 simulator as a Storm)
Spacer|RichTextField|Spacer
Spacer|RichTextField|Spacer
Spacer|RichTextField|Spacer
Spacer|RichTextField|Spacer
Here is how I'm trying to do it
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
VerticalFieldManager vfm2 = new VerticalFieldManager();
for (int i = 1; i <= 4; i++) {
RichTextField rtf = new RichTextField("Hello World " + i);
vfm2.add(rtf);
}
hfm1.add(new LabelField("!"));
hfm1.add(vfm2);
hfm1.add(new LabelField("!"));
add(hfm1);
This displays
Spacer|RichTextField
and not the desired
Spacer|RichTextField|Spacer
What appears to be happening is that the RichTextFields are too long and I don't know how to fix this. Does anyone have any suggestions on what I'm doing wrong?
I've tried overriding getPreferredWidth() but it's not shortening it at all
for (int i = 1; i <= 10; i++)
{
RichTextField rtf = new RichTextField("Hello World " + i)
{
public int getPreferredWidth()
{
return (40);
}
};
vfm2.add(rtf);
}
If it helps, Display.getWidth() returns 360 without the above override when in portrait (Top-side-up).
Any help is appreciated.
Solved! Go to Solution.
10-28-2009 05:00 AM
Try This
VerticalFieldManager vfm = new VerticalFieldManager();
for(int i=0;i<=3;i++)
{
HorizontalFieldManager hmanger = new HorizontalFieldManager();
hmanger.add(new LabelField("!"));
RichTextField rtf = new RichTextField("Hello World " + i);
hmanger.add(rtf);
hmanger.add(new LabelField("!"));
vfm.add(hmanger);
}
add(vfm );
10-28-2009 05:37 AM - edited 10-28-2009 05:39 AM
That won't work either, you need to force the width of the RichTextField in the sublayout.
Try the following:
VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hmanger = new HorizontalFieldManager();
hmanger.add(new LabelField("!"));
RichTextField rtf = new RichTextField("Hello World ") {
public int getPreferredWidth() {
return 40;
}
protected void layout(int arg0, int height) {
super.layout(getPreferredWidth(), height);
setExtent(getPreferredWidth(), height);
}
};
hmanger.add(rtf);
hmanger.add(new LabelField("!"));
vfm.add(hmanger);
this.add(vfm);
Also, can I just complement the Original Poster on giving us a simple piece of code that worked without needing anything else and demonstrated the problem. I wish all posts of code were similarly brief and to the point.
10-28-2009 08:19 PM
Thank you both for the answers.
Overriding the layout() function appeared to work to fix the width. The one thing that did crop up was that the height of the RichTextField became the entire height of the screen. I replaced height with getPreferredHeight(). This appears to work fine, but in case I'm incorrect here it is.
RichTextField rtf = new RichTextField("Hello World " + i)
{
public int getPreferredWidth()
{
return (40);
}
protected void layout(int arg0, int height)
{
super.layout(getPreferredWidth(), getPreferredHeight());
setExtent(getPreferredWidth(), getPreferredHeight());
}
};
Please let me know otherwise, but this seems to work.
Thank you again.