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
Highlighted
New Contributor
Posts: 2
Registered: ‎08-06-2012
My Device: simulator
My Carrier: none
Accepted Solution

Setting button label from user choice of an element of objectlistfield

Hi,

 

I am a newbie and this is my first BB app. I have a problem and I tried to do this on my own, did quite a bit of research, but could not find any solution.

The thing is, I have a button added on main screen, by clicking this button I push a popupscreen on top of stack which contains an ObjectListField. Now I want to set the label of the button on the mainscreen as the name of the field selected in ObjectListField. I have attached my code on which I am experimenting and trying to learn(really enthusiastic about it Smiley Happy ),so would be grateful of any help in this regard. Thanks a ton Smiley Happy

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

public final class MyScreen extends MainScreen
{
    
	ButtonField btn;
    public MyScreen()
    {        
        // Set the displayed title of the screen       
    	super();
    	setTitle("MyTitle");
        
        
    	btn=new ButtonField("Label",ButtonField.CONSUME_CLICK);
    	btn.setChangeListener(new FieldChangeListener()
    	{
    		public void fieldChanged(Field field,int context){
    			
    			UiApplication.getUiApplication().pushScreen(new mypopup());
    			
    		}
    	});
    			
    			
    			add(btn);
    			
    	
    	
    }


    		/*public void inval(String lbl){
    			
    			this.btn.setLabel(lbl);
    			this.invalidate();
    		} */

}

class mypopup extends PopupScreen implements FieldChangeListener{
	
	String it;
	
ObjectListField obj=new ObjectListField(){
		
		public boolean navigationClick(int status,int time){
			
			int selectedindex=obj.getSelectedIndex();
			 it=(String)obj.get(obj, selectedindex);
			
			UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
			
		MyScreen my=new MyScreen();
	         my.btn.setLabel(it);
			my.invalidate();
			
			
	UiApplication.getUiApplication().invokeLater(new Runnable()
			{
				public void run(){
					
					Dialog.alert(it);
					
				}
			});
			
			
		  
			return true;		
			
			
		}
	};
	
	
	
	 public mypopup() {
		 
		 super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE);
	 
		 String[] type=new String[] {"a","b","c","d"};
	        obj.set(type);
	        add(obj);
	       
	 
	 }

	public void fieldChanged(Field field, int context) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
}

 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Setting button label from user choice of an element of objectlistfield

welcome to the support forums.

what you do is to create a new screen instance, but what you want to do is to change the button on the existing one.
btw, all operations like setLabel, setText etc include invalidate, no need to call it manually.

you cannot access btn in your popup as it is in another class, so you have several options that i will explain to you using the Dialog class (which also offers a solution for what you want to achieve):

- use a listener, like http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/ui/component/DialogClosedListe...
you register a listener from the screen on the popup, and it is called whenever something happens
- use a callback (give the popup your screen, or your button, or an interface that encapsulates it, basically). read about the pattern if you are not familiar with it
- you give your class a return method, like http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/ui/component/Dialog.html#ask(j... java.lang.Object[], int)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 2
Registered: ‎08-06-2012
My Device: simulator
My Carrier: none

Re: Setting button label from user choice of an element of objectlistfield

Hi Simon, first of all thanks a ton for replying.

I learnt a lot by reading the documentation and links you gave me.

 

But the thing is, I really wanted to use popupscreen class instead of dialog. So (with some help) I did little changes in my code and it seems to work now.

I am writing changes in my code for other users who might have the same problem..

I created a method setlabel (*not to be confused with setLabel), in MyScreen class and right where I implement popscreen in mypopup class, I did..

 

Screen scr = UiApplication.getUiApplication().getActiveScreen();
            if (scr instanceof MyScreen) {
                MyScreen my = (MyScreen) scr;
                my.setlabel(it);
                my.invalidate();

 

it finally seems to work now.

 

Cheers Smiley Happy