views:

155

answers:

1

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!

A: 

I tried your code on 4.7, 4.6, 4.5, and 4.3 and your getPreferredWidth() method returns what's expected. I am guessing that something is wrong with your simulator. As an example, I had issues connecting over HTTP with this simulator "BlackBerry Device Simulators v4.5.0.81" but had no issues with this one "BlackBerry Device Simulators v4.5.0.52 (8310)".

Note: I tested your code within Eclipse & the BB plug-in.

clafonta
Thanks for trying that out. Your results are interesting since I'm seeing it on two actual devices as well as the simulator. I'll keep digging to see if it's something with a specific version of the OS.Thanks!
Patrick Steele