BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 03-15-2010, 05:44 PM   #1 (permalink)
New Member
 
Join Date: Mar 2010
Model: --
PIN: N/A
Carrier: --
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Context Menu not working

Please Login to Remove!

I'm using JDE 4.7 with 9530 Simulator on Windows 7.

My problem is that I can't manage to show Context Menu in my application :(

This is part of the code of the mainscreen of my little test application

Code:
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.component.Menu;

import net.rim.device.api.ui.FieldChangeListener;

import net.rim.device.api.ui.container.HorizontalFieldManager;

import net.rim.device.api.ui.UiApplication;

class UiFunMainScreen extends MainScreen implements FieldChangeListener
{
   // campi dell'interfaccia grafica
   private BitmapField bitmap;
   private EditField usernameField;
   private PasswordEditField passwordField;
   private CheckboxField rememberCheckbox;
   private ObjectChoiceField domainField;
   private ButtonField clearButton;
   private ButtonField loginButton;
   
   // Classi inner per la realizzazione dei Menu
   class LoginMenuItem extends MenuItem
   {
      public LoginMenuItem()
      {
         super("Login",20,10); // Testo, Ordinal = posizione, Priority = evidenziato per primo
      }
      
      public void run()
      {
         login();
      }
   
   }
   
   
   class ClearMenuItem extends MenuItem
   {
      public ClearMenuItem()
      {
         super("Clear",10,20);
      }
      
      public void run()
      {
         clearTextFields();
      }
   
   }

   // this work perfectly
   protected void makeMenu(Menu menu, int instance) 
   {
      super.makeMenu(menu, instance);
      menu.add(new LoginMenuItem()); 
      menu.add(new ClearMenuItem());
   }
   
  // this is never called! :( why?
   protected void makeContextMenu(ContextMenu contextMenu) 
   {
      super.makeContextMenu(contextMenu); 
      contextMenu.addItem(new LoginMenuItem()); 
      contextMenu.addItem(new ClearMenuItem());
   }

}

I overridden the makeContextMenu() method, but this is never called...(I tried to put a breakpoint on this method and then from the simulator I clicked on the field to get the context menu but the method NEVER gets called).

Why such beahavior?

I really can't manage to understand what I'm missing...maybe a stupid thing...I would appreciate any help
Offline  
Old 03-15-2010, 06:20 PM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2010
Model: N/A
PIN: N/A
Carrier: Fledge
Posts: 43
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Context menus are only shown when a field has focus. You typically would put the makeContextMenu method in a field that would get focus, then the context menu for that field will be displayed when the users clicks the menu button.

Screens don't really get focus, the only reason why MainScreen has MakeContextMenu is because it inherits it from Field.

For example, in this screenshot below, I added "Next 10", "View Map" from the mainscreens MakeMenu method. The "View Apartment" comes from the selected field's context menu... if that makes sense.


Last edited by jacob.tabak : 03-15-2010 at 06:24 PM.
Offline  
Old 03-15-2010, 06:43 PM   #3 (permalink)
New Member
 
Join Date: Mar 2010
Model: --
PIN: N/A
Carrier: --
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Context menus are only shown when a field has focus. You typically would put the makeContextMenu method in a field that would get focus, then the context menu for that field will be displayed when the users clicks the menu button.

Screens don't really get focus, the only reason why MainScreen has MakeContextMenu is because it inherits it from Field.
I know this, but see my image...when I click on the field while this has focus nothing happens :( why?

The link is here: img96.imageshack.us/img96/836/ catturafs.jpg
Offline  
Old 03-15-2010, 08:58 PM   #4 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2010
Model: N/A
PIN: N/A
Carrier: Fledge
Posts: 43
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

It looks like you're overriding makecontextmenu in your main screen rather than your focusable field.
Offline  
Old 03-16-2010, 04:07 AM   #5 (permalink)
New Member
 
Join Date: Mar 2010
Model: --
PIN: N/A
Carrier: --
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for your answers

Things I don't understand are 2:

1) Following the book I'm reading about Blackberry development, this method also shows up the context menu while clicking on a field, because we don't do any check on instance (I undrstood that in instance I have an int that tell me if the menu is contextual or main), so without any if-else I should see the contextual menu by clicking on a field (the images on the book show context menu!)
But in my simulator this code only work for main men¨ (clicking on berry button).

Why such behavior? I'm really getting frustrating reading one thing and seeing another...

Code:
 // this work perfectly
   protected void makeMenu(Menu menu, int instance) 
   {
      super.makeMenu(menu, instance);
      menu.add(new LoginMenuItem()); 
      menu.add(new ClearMenuItem());
   }
2) I wanted to try your suggest of

Quote:
you're overriding makecontextmenu in your main screen rather than your focusable field.
but where should I put my code? I only have this file (main screen) and another file (the application). I don't know where to put the code to override the method like you said...

I know maybe I'm asking easy things...but I need to understand these things
Offline  
Old 03-16-2010, 12:28 PM   #6 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2010
Model: N/A
PIN: N/A
Carrier: Fledge
Posts: 43
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pao,

No problems on the "simple" questions - it's tricky stuff, especially if you haven't worked extensively with Java and inner classes before.

Whenever you want to do something like add a context menu to a field, override the paint method to change the color of the text, etc, you will probably use an inner class. Look at the "myLabel" field in the example below, you should be able to just copy and paste this and be good to go.

Code:
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class Main extends UiApplication {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main app = new Main();
		app.enterEventDispatcher();
	}
	
	public Main() {
		MainScreen screen = new MainScreen();
		LabelField myLabel = new LabelField("Field with Context Menu", Field.FOCUSABLE) {
			protected void makeContextMenu(ContextMenu contextMenu) {
				contextMenu.addItem(new MenuItem("Context Menu", 10, 10) {
					public void run() {
						Dialog.alert("You performed the context action!");
					}
				});
			}
		};
		screen.add(myLabel);
		screen.add(new LabelField("Field w/o Context Menu", Field.FOCUSABLE));
		pushScreen(screen);
	}
}
Offline  
Old 03-16-2010, 05:30 PM   #7 (permalink)
New Member
 
Join Date: Mar 2010
Model: --
PIN: N/A
Carrier: --
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know inner classes and I worked with Java, but I think there should be a better method to obtain context menu...or do I really have to override contextMenu for *every field* I want to have the context menu?

However I tried like you said changing my code in this way:

Quote:
usernameField = new EditField("Username: ", "", 10, Field.FOCUSABLE){
protected void makeContextMenu(ContextMenu contextMenu)
{
super.makeContextMenu(contextMenu); //fondamentale per aggiungere le voci del men¨ di base

// aggiunta delle voci personalizzate, quando saranno cliccate verrÓ eseguito il metodo run() di queste classi (vedi sopra)
contextMenu.addItem(new LoginMenuItem());
contextMenu.addItem(new ClearMenuItem());
}
};
but the only result is that the voices are added in the Main Menu...no context menu is showing up...

I'm start thinking the problem is with my simulator... I will try to download another instead of the 9530 Storm Simulator...
Offline  
Old 03-16-2010, 06:03 PM   #8 (permalink)
New Member
 
Join Date: Mar 2010
Model: --
PIN: N/A
Carrier: --
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

It was the simulator...

I downloaded and installed 9700 simulator and my contextMenu shows up perfectly (without any of the modification you told me )... (you can see it here yfrog.com/5ccatturaxrj )

I'm wondering why does the Storm beahave in such a strange way? And how to show context menu in Storm?

I know I'm breaking your b***cks :D but could you try your code on a Stor simulator to see if it's only me that have this problems?

Thank you very much for your answers! ^_^

PS:

sorry for links but unless I reach 10 posts I cannot post directly images... :(

Last edited by paolinu84 : 03-16-2010 at 06:19 PM. Reason: adding image
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright ę 2004-2014 BlackBerryForums.com.
The names RIM ę and BlackBerry ę are registered Trademarks of BlackBerry Inc.