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
Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel
Accepted Solution

FieldChangeListener for custom field not working...

1738i3BB0D6C50A52DBFBhi All,

        i have searched the posts for it and tried nearly everything but  couldn't get the solution of it. here is my code. my problem is when i'll click on individual images the fieldchangelistener should get invoked and get the message.

 

package com.samples;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class CustomPaintingScreen extends MainScreen implements FieldChangeListener
{
	CustomClass[] custom;
	
	FieldChangeListener itemlistener = new FieldChangeListener() {
	
		public void fieldChanged(Field field, int context)
		{
			Dialog.alert("clicked");	
		}
	};
	
	public CustomPaintingScreen()
	{
		setTitle("Custom Painting ");
		custom = new CustomClass[10];		
		VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLLBAR|Manager.LEAVE_BLANK_SPACE);	
		
		for (int i = 0; i < custom.length; i++)
		{
			custom[i] = new CustomClass();
			custom[i].setChangeListener(itemlistener);			
			vfm.add(custom[i]);			
		}
		this.add(vfm);		
	}

	public void fieldChanged(Field field, int context)
	{
		if(field instanceof CustomClass)
		{
			Dialog.alert("clicked");
		}
	}
}

 

and here is my customclass

 

package com.samples;

import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;



public class CustomClass extends Field //implements FieldChangeListener
{
	private XYRect dest;
	private Bitmap bitmap;	
	private Vector vtr;
	private Bitmap next;

	public CustomClass()
	{		
		bitmap = Bitmap.getBitmapResource("laptop.png");
		next = Bitmap.getBitmapResource("next.png");
		dest = new XYRect(20,15,bitmap.getWidth(),bitmap.getHeight());
		//this.setChangeListener(this);		
		
		vtr = new Vector();
	}

	protected void layout(int width, int height)
	{
		height = getFont().getHeight()* 5;		
		setExtent(Display.getWidth(), height);		
	}

	protected void paint(Graphics graphics)
	{
		graphics.setColor(0xFF7F24);		
		graphics.fillRoundRect(5, 5, (getWidth()-10), (getFont().getHeight()*5), 30, 40);
		graphics.drawBitmap(dest, bitmap, 0, 0);
		graphics.setColor(Color.BLACK);
		int x = bitmap.getWidth()+25;
		int y = 15;
		graphics.setFont(getFont().derive(Font.BOLD));
		graphics.drawText("Dell Laptop", x, y, 0, getWidth());
		graphics.setFont(getFont().derive(Font.PLAIN,15));
		
		String str= "Discover Dell Inspiron and XPS Laptops with advanced technology.this is dell laptop";
		vtr = wrap(str, 300,graphics);		
		System.out.println(" "+vtr.toString());
		
		graphics.drawBitmap(335, 15, next.getWidth(), next.getHeight(), next, 0, 0);
	
//		graphics.drawText(vtr.toString(), x, (y+20), 0,300 );
	}
	
	private Vector wrap (String text, int width,Graphics graphics) 
	{
		int x = bitmap.getWidth()+25;
		int y = 35;
		
	    Vector result = new Vector ();
	    if (text == null)
	       return result;
	 
	    boolean hasMore = true;
	 
	    // The current index of the cursor
	    int current = 0;
	 
	    // The next line break index
	    int lineBreak = -1;
	 
	    // The space after line break
	    int nextSpace = -1;
	 
	    while (hasMore) 
	    {
	       //Find the line break
	       while (true) 
	       {
	           lineBreak = nextSpace;
	           if (lineBreak == text.length() - 1) 
	           {
	               // We have reached the last line
	               hasMore = false;
	               break;
	           } 
	           else 
	           {
	               nextSpace = text.indexOf(' ', lineBreak+1);
	               if (nextSpace == -1)
	                  nextSpace = text.length() -1;
	               int linewidth = this.getFont().getAdvance(text,current, nextSpace-current);
	               // If too long, break out of the find loop
	               if (linewidth > width) 
	                  break;
	           }
	      }
	      String line = text.substring(current, lineBreak + 1);
	      
	      graphics.drawText(line, x, y, 0,350 );
	      y=y+15;
	      
	      result.addElement(line);
	      result.addElement(new String("\n"));
	      current = lineBreak + 1;
	 }
	 return result;
	}
	
	protected boolean navigationClick(int status, int time)
	{
		fieldChangeNotify(1);
		return super.navigationClick(status, time);
	}
	
	protected void fieldChangeNotify(int context)
	{	
		super.fieldChangeNotify(context);
	}
}

 

 

also my images look like this

 

when i click on this individual images the dialog should appear. i tried a lot. give me some code. for  touchscreen i.e. jde4.7 and for jde4.5

Swapnil G.
Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: FieldChangeListener for custom field not working...

Since you are overriding the methods, why not call the fieldChangedListener directly? This is what I do:

 

if( getChageListener() != null ) getChangeListener().fieldChanged( this, value );

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: FieldChangeListener for custom field not working...

[ Edited ]

This appears to be a continuation of this Thread. 

http://supportforums.blackberry.com/t5/Java-Development/listener-with-custom-class-not-getting-invok...

If so, can you please "close" that Thread and indicate you have continued here?

 

If you are coding for TouchScreen devices I think you will need to override touchEvent() to pick up clicks on your Button.  navigationClick() is for trackball/trackpads.  This is easy to test, just put a break point in yout navigationClick and see if it is invoked when you clik on the screen. 

 

I suggest you do a search on the forum for other people who have written custom buttons and struggled to get them to work on touchscreen devices. 

Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: FieldChangeListener for custom field not working...

[ Edited ]

but Peter even for the trackpad/trackball it is not working i tried that. can you plz help me more. i tried it with jde4.5.

Swapnil G.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: FieldChangeListener for custom field not working...

I actually cut/pasted some of your code into one of my programs and it worked fine!

 

So for 4.5, the critical bits are:

 

1) You customer Field should have this:

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
}

 

2) You need a listener:

FieldChangeListener itemlistener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("clicked - context " + Integer.toString(context) );
}
};

 

3) You need to add that listener to the Field that can be clicked:

<filed>.setChangeListener(itemlistener);

 

Try doing all of these, but to a standard Field, for example:

 

RichTextField rtf = new RichTextField("Click on me") {

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
}

 

Add this to the screen:

this.add(rtf);

 

and set the change listener

rtf.setChangeListener(itemListener);

 

See if that works.

Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: FieldChangeListener for custom field not working...

hi peter, as per your guideline i exactly did that but could not get into it. is their any need to override for "onFocus" method??

Swapnil G.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: FieldChangeListener for custom field not working...

No need to concern yourself with focus.

 

Add a real button to the screen. add your listener as a listener to it, and see if your Dialog fires.  If it does, then put a break point in the navigationClick - make sure that it is calling fieldChangeNotify

Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: FieldChangeListener for custom field not working...

1765i8F502D1892613F55

hi peter is this that  i want to add listener on the above image and handle it. and the above image i have created using the graphics object. so is this the issue that i'm able to get the listener?

Swapnil G.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: FieldChangeListener for custom field not working...

The code I have pasted below works on trackball devices and should show you what you need to do.

 

public class TestScreen extends MainScreen {

    public TestScreen() {
        setTitle("Test click detection");

        FieldChangeListener itemlistener = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                Dialog.alert("Clicked");        
            }
        };

        this.add(new RichTextField("Click on images and note results"));
        BitmapField bmfClickable = new BitmapField(Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Field.FOCUSABLE) {
            protected boolean navigationClick(int status, int time) {
                fieldChangeNotify(1);
                return true;
            }
        };
        this.add(bmfClickable);
        bmfClickable.setChangeListener(itemlistener);

        BitmapField bmfNotClickableNoListener = new BitmapField(Bitmap.getPredefinedBitmap(Bitmap.QUESTION), Field.FOCUSABLE) {
            protected boolean navigationClick(int status, int time) {
                fieldChangeNotify(1);
                return true;
            }
        };
        this.add(bmfNotClickableNoListener);
        // Do not add Change Listener to this...

        BitmapField bmfNotClickableNoNavClick = new BitmapField(Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), Field.FOCUSABLE) {
        };
        this.add(bmfNotClickableNoNavClick);
        bmfNotClickableNoNavClick.setChangeListener(itemlistener);
        // Has change listener but nothing invokes it

        this.add(new RichTextField("End"));

    }

    public void close() {
        UiApplication.getUiApplication().popScreen(this);
    }

}

 

 

 

Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: FieldChangeListener for custom field not working...

thank you very very much for ur guidance peter.....

Swapnil G.