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
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified
Accepted Solution

Unexpected menu appears in button listener

This is my first attempt at development for the BB storm, and I'm using JDE 4.7.

I started with an Hello World application I found and I modified it to add a simple button that says RED. When clicked I wanted the screen background to turn RED and change the button to say BLUE, when that is clicked it turns BLUE and changes the button back to RED again.

I implement this using a standard ButtonField listener (which I call bhandler in my code). The program kind of works, with the only weird effect on the simulator being that when I click on the button, the background color does change but a small menu appears. Anyone else seen this menu issue? I'm not expecting to see a menu and I haven't coded anything to ask for a menu to appear when I click this button.

Here is my source code. The handler is in bold text. Note that the blog messes with the code so it might be changed slightly by the smileys... Any answers as to why I see a menu would be most appreciated.....thanks.

 

/**
 * HelloWorld.java
 * Copyright (C) 2001-2003 Research In Motion Limited. All rights reserved.
 */
package com.rim.samples.helloworld;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.decor.*;


/*
 * BlackBerry applications that provide a user interface
 * must extend UiApplication.
 */
public class HelloWorld extends UiApplication
{
        public static void main(String[] args)
        {
                //create a new instance of the application
                //and start the application on the event thread
                HelloWorld theApp = new HelloWorld();
                theApp.enterEventDispatcher();
    }
        public HelloWorld()
        {
                //display a new screen
                pushScreen(new HelloWorldScreen());
        }
}

//create a new screen that extends MainScreen, which provides
//default standard behavior for BlackBerry applications
final class HelloWorldScreen extends MainScreen
{
        public HelloWorldScreen()
        {

                //invoke the MainScreen constructor
                super();

                //add a title to the screen
                LabelField title = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS
                                | LabelField.USE_ALL_WIDTH);
                setTitle(title);

                //add the text "Hello World!" to the screen
                add(new RichTextField("Hello World!":smileywink:);
                
                // First define button handler
                FieldChangeListener bhandler = new FieldChangeListener()
                {
                    public void fieldChanged(Field field, int context)
                    {
                        ButtonField buttonField = (ButtonField) field;
                        if (buttonField.getLabel() == "RED":smileywink:
                        {
                            Background newback = BackgroundFactory.createSolidBackground(Color.RED);
                            getMainManager().setBackground(newback);
                            buttonField.setLabel("BLUE":smileywink:;
                        }
                        else
                        {
                            Background newback = BackgroundFactory.createSolidBackground(Color.BLUE);
                            getMainManager().setBackground(newback);
                            buttonField.setLabel("RED":smileywink:;
                        }
                    }
                 };

                 // Define button itself to use handler above, this one called RED
                 ButtonField buttonField = new ButtonField("RED":smileywink:;     // Create RED button
                 buttonField.setChangeListener(bhandler);               // Set up button handler defined above
                 
                 add(buttonField);  // Add the button to the screen.
                 

        }

        //override the onClose() method to display a dialog box to the user
        //with "Goodbye!" when the application is closed
        public boolean onClose()
        {
            Dialog.alert("Goodbye!":smileywink:;
            System.exit(0);
            return true;
        }
}
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Unexpected menu appears in button listener

You need to add the CONSUME_CLICK to your button style, like this:

 

ButtonField buttonField = new ButtonField("RED",ButtonField.CONSUME_CLICK)

 

 

Developer
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: Unexpected menu appears in button listener

Thank you so much!

I just tried it and that solved it. I was wondering how to use that second field. How events are processed (obviously from front to back) is something I'm unfamiliar with this GUI yet. I'll have to look at these flags some more to see how these events pass from screen to screen or field to field.

 

Thanks again. Problem solved. I gave you the Kudos :-)

-Donald