I've created a ButtonField with an automatically-calculated width. The button's width will be determined based on how many other buttons are in it's parent's HorizontalFieldManager (HFM). So if there's 3 buttons, each button will be about 33% of the width. If there's 5 buttons, each one will be about 20% of the width, etc... Basically, all buttons have the same width.
The code below works fine under JDE 4.7 (tested with the Storm simulator). But when run under JDE 4.5 (the 8330 simulator), my getPreferredWidth() value seems to be ignored. The display width on both devices is 320 and I'm adding 4 buttons to the HFM. The width is calculated at 80 and each button is 80 under 4.7. Under 4.5, I get 3 buttons of about 96 and the last one is around 24.
The text in each button is different so this might be causing the issue. Here's the code:
public class AutoWidthButtonField extends ButtonField
{
AutoWidthButtonField(String label, long style) { super(label, style); }
public int getPreferredWidth()
{
Manager parent = this.getManager();
int fields = parent.getFieldCount();
int width = (Display.getWidth() / fields);
return width;
}
/*
protected void layout(int width, int height)
{
super.layout(width, height);
setExtent(getPreferredWidth(), getPreferredHeight());
}
*/
}
Right now layout is commented-out, but it doesn't change the behavior. I'm using it like this:
HorizontalFieldManager nav = new HorizontalFieldManager();
nav.add(new AutoWidthButtonField("B1", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("Opt 2", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("Test", ButtonField.CONSUME_CLICK));
nav.add(new AutoWidthButtonField("...", ButtonField.CONSUME_CLICK));
What am I doing wrong under 4.5? Thanks!