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
Posts: 15
Registered: ‎02-11-2009
My Device: Not Specified
Accepted Solution

Vertical Button Focus Issues

I have been at this for hours and cannot seem to figure out the problem.

What I want is simple - 6 skinny vertical buttons that stretch from the top of the screen to the bottom (done). I want them evenly spaced from left to right (done). Then I want to click on each for different actions. What is happening is the first button immediatly gains focus and appears to have focus on the entire screen. No matter where I click, the first button is the one that keeps focus. Help? Here is the app code below. The buttons extend Field and are modeled after PictureBackgroundButtonField from one of the examples.

 

public class MyApp extends UiApplication implements FieldChangeListener
{
    //------------------------- Class Attributes -------------------------------
    
    //------------------------- Class Methods ----------------------------------
    public static void main (String[] args)
    {
        MyApp theApp = new MyApp ();
        
        // Have this app enter the event queue
        theApp.enterEventDispatcher();        
    }    

     //------------------------- Attributes -------------------------------------
    private MainScreen mainScreen;
    
    private HorizontalFieldManager hfm;

    private VLineButtonField[] lineButtons= new VLineButtonField[6];

    
    //------------------------- Constructor ------------------------------------
    public GuitarTunerApp()
    {
        // MainScreen is the basic screen or frame class of the RIM UI.
        mainScreen = new MainScreen();
      
        lineButtons[0] = new GuitarStringButtonField(10, 480, Field.FOCUSABLE | Field.USE_ALL_HEIGHT, "E");
        lineButtons[0].setChangeListener(this);
 
        lineButtons[1] = new VLineButtonField(10, 480, Field.FOCUSABLE, "A");
        lineButtons[1].setChangeListener(this);
 
        lineButtons[2] = new VLineButtonField(10, 480, Field.FOCUSABLE, "B");
        lineButtons[2].setChangeListener(this);

        lineButtons[3] = new VLineButtonField(10, 480, Field.FOCUSABLE, "C");
        lineButtons[3].setChangeListener(this);
 
        lineButtons[4] = new VLineButtonField(10, 480, Field.FOCUSABLE, "D");
        lineButtons[4].setChangeListener(this);

        lineButtons[5] = new VLineButtonField(10, 480, Field.FOCUSABLE, "E");
        lineButtons[5].setChangeListener(this);
         
        // Add a Horizontal field manager containing sample custom button fields.
        hfm = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH |
                HorizontalFieldManager.NO_HORIZONTAL_SCROLL)
        {
            public int getPreferredWidth()
            {
                return getManager().getWidth();
            }
            
            protected void sublayout(int maxWidth, int maxHeight)
            {

                super.sublayout(maxWidth, maxHeight);

                //** If I remove this loop, the buttons crunch together on the left side with

          // right most buttons having focus on the rest of the open screen. *****

              for (int i=0; i < 6; i++)
                {
                    Field field = getField(i);
                    setPositionChild(field, 48 + 51*i, 0);
                }
            }
        };
        
        Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("funImage.png"));
        hfm.setBackground(bg);
        
        for (int i=0; i < 6; i++)
        {    
            hfm.add(lineButtons[i]);
        }
    
        mainScreen.add(hfm);

         pushScreen(mainScreen);
    }

     public void fieldChanged(Field field, int context)
    {

        String text = "Button";
        
        if (field instanceof GuitarStringButtonField)
        {
            text = ((VLineButtonField)field).getName();
        }
                 
        Dialog.inform(text + " was clicked.");
     
    }

Developer
Posts: 15
Registered: ‎02-11-2009
My Device: Not Specified

Re: Vertical Button Focus Issues

Never fails. Post something then solve it soon thereafter. Turns out the super.sublayout(maxWidth, maxHeight);

was causing me the issues. Removing that and setting the extent solved my problems.