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
Contributor
Posts: 16
Registered: ‎09-10-2011
My Device: torch 9800
My Carrier: digicel
Accepted Solution

Implementing Listener for Custom ListField

I'm trying to implement a listener for a custom field I created which would launch a new screen when the field is clicked. However, nothing happens when I click the custom field. I'm using BlackBerry Java Plug-in for Eclipse, JDK 1.3 and JRE 6.0. All my code is attached. MyScreen.java contains the code where I try to implement a fieldChanged function.

 

//MyApp.Java

package mypackage;

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

/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class MyApp extends UiApplication
{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
    	CodeModuleManager.promptForResetIfRequired();
    	
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }
    

    /**
     * Creates a new MyApp object
     */
    public MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new MyScreen());
    }    
}
//MyScreen.java

package mypackage;

import net.rim.device.api.ui.container.*; //for vertical manager
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.Screen;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.decor.BackgroundFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
	private CustomField cField;
			
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("My New App");
    
        cField = new CustomField("4.PNG","This is my 4th custom field!!");
        ButtonClickListener listener = new ButtonClickListener();
        cField.setChangeListener(listener);
        add(cField);
        
    }//MyScreen function
    
   
    class ButtonClickListener implements FieldChangeListener
    {
        public void fieldChanged(Field field, int context)
         {
              //we need to determine which button was clicked

              if(field == cField)
            	  new SpeedBumpScreen();

         }
			
	}//ButtonClickListener
    
}
//CustomField.java

package mypackage;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;


class CustomField extends ListField implements ListFieldCallback {
	private Vector rows;
		

	public CustomField(String customImg, String customLabel) {
		super(0, ListField.MULTI_SELECT);
		setRowHeight(80);
		setEmptyString("Hooray, no items here!", DrawStyle.HCENTER);
		//setCallback(this);

		Bitmap p1 = Bitmap.getBitmapResource(customImg); 
			
		rows = new Vector();
		
		TableRowManager row = new TableRowManager();

		row.add(new BitmapField(p1));

		// SET THE item NAME LABELFIELD
		// if overdue, bold/underline
		LabelField item = new LabelField("item #" + customLabel,
			DrawStyle.ELLIPSIS);
				
		// overdue
		item.setFont(Font.getDefault().derive(
			Font.BOLD | Font.UNDERLINED));
		System.out.println("OVERDUE");
		
		row.add(item);

		// SET THE LIST NAME
		row.add(new LabelField("List Name #" + String.valueOf(1),
			DrawStyle.ELLIPSIS) {
			protected void paint(Graphics graphics) {
				graphics.setColor(0x00878999);
				super.paint(graphics);
			}
		});

		// SET THE DUE DATE/TIME
		row.add(new LabelField("Due Date #" + String.valueOf(1),
				DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
				| DrawStyle.RIGHT) {
			protected void paint(Graphics graphics) {
				graphics.setColor(0x00878787);
				super.paint(graphics);
			}
		});
				
		rows.addElement(row);
			
		setSize(rows.size());

	} //end public CustomField()

	
	
	// ListFieldCallback Implementation
	public void drawListRow(ListField listField, Graphics g, int index, int y,
			int width) {
		CustomField list = (CustomField) listField;
		TableRowManager rowManager = (TableRowManager) list.rows
			.elementAt(index);
		rowManager.drawRow(g, 0, y, width, list.getRowHeight());
	} //end drawListRow()

	private class TableRowManager extends Manager {
		
		public TableRowManager() {
			super(0);
		} //end pulic TableRowManager

		
		// Causes the fields within this row manager to be layed out then
		// painted.
		public void drawRow(Graphics g, int x, int y, int width, int height) {
			// Arrange the cell fields within this row manager.
			layout(width, height);

			// Place this row manager within its enclosing list.
			setPosition(x, y);

			// Apply a translating/clipping transformation to the graphics
			// context so that this row paints in the right area.
			g.pushRegion(getExtent());

			// Paint this manager's controlled fields.
			subpaint(g);

			g.setColor(0x00CACACA);
			g.drawLine(0, 0, getPreferredWidth(), 0);

			// Restore the graphics context.
			g.popContext();
		}//end drawRow()

		
		// Arranges this manager's controlled fields from left to right within
		// the enclosing table's columns.
		protected void sublayout(int width, int height) {
			// set the size and position of each field.
			int fontHeight = Font.getDefault().getHeight();
			int preferredWidth = getPreferredWidth();

			// start with the Bitmap Field of the priority icon
			Field field = getField(0);
			layoutChild(field, 32, 32);
			setPositionChild(field, 0, 0);

			// set the item name label field
			field = getField(1);
			layoutChild(field, preferredWidth - 16, fontHeight + 1);
			setPositionChild(field, 34, 3);

			// set the list name label field
			field = getField(2);
			layoutChild(field, 150, fontHeight + 1);
			setPositionChild(field, 34, fontHeight + 6);

			// set the due time name label field
			field = getField(3);
			layoutChild(field, 150, fontHeight + 1);
			setPositionChild(field, preferredWidth - 152, fontHeight + 6);

			setExtent(preferredWidth, getPreferredHeight());
		}//end sublayout()

		
		// The preferred width of a row is defined by the list renderer.
		public int getPreferredWidth() {
			return Graphics.BLACK;
		}
		

		// The preferred height of a row is the "row height" as defined in the
		// enclosing list.
		public int getPreferredHeight() {
			return getRowHeight();
		}
		
	}// private class TableRowManager extends Manager 

	public Object get(ListField listField, int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getPreferredWidth(ListField listField) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int indexOfList(ListField listField, String prefix, int start) {
		// TODO Auto-generated method stub
		return 0;
	}
		
} //end class CustomField extends ListField implements ListFieldCallback
//SpeedBumpScreen.java

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public final class SpeedBumpScreen extends MainScreen
{
    /**
     * Creates a new HelloWorldScreen object
     */
    SpeedBumpScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Speed bump screen");

        // Add a read only text field (RichTextField) to the screen.  The
        // RichTextField is focusable by default. Here we provide a style
        // parameter to make the field non-focusable.
        add(new RichTextField("This is the speed bump screen!", Field.NON_FOCUSABLE));
    }

}

 

 

 

 

Contributor
Posts: 16
Registered: ‎09-10-2011
My Device: torch 9800
My Carrier: digicel

Re: Implementing Listener for Custom ListField

Testing on Blackberry simulator for Torch 9800 on OS 6.0.0.313
Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: Implementing Listener for Custom ListField

in the fieldchangelistener you need to push the new screen...not just define it :

 

 public void fieldChanged(Field field, int context)
         {
              //we need to determine which button was clicked

              if(field == cField)
            	 pushScreen (new SpeedBumpScreen());

         }

 


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Implementing Listener for Custom ListField

In addition, there is nothing in the ListField that will actually generate an event. 

 

Here is a simple extension to the ListField that will make it 'clickable'.  In your FieldChangeListener,you can use getSelectedindex to determine which row has focus.

 

Please ask if this is not clear:

 

public class ClickableListField extends ListField {

    public ClickableListField(int numberOfRows) {
        super(numberOfRows);
    }

    protected boolean navigationClick(int status, int time) {
        this.fieldChangeNotify(2);
        return true;
    }

    protected boolean touchEvent(TouchEvent message) {
        int x = message.getX( 1 );
        int y = message.getY( 1 );
        if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
                // Outside the field
                return false;
        }
        // If click, process Field changed
        if ( message.getEvent() == TouchEvent.CLICK ) {
            this.fieldChangeNotify(2);
            return true;
        }
        return super.touchEvent(message);
    }

}

 

Visitor
Posts: 1
Registered: ‎09-21-2011
My Device: Torch 9800
My Carrier: ATT

Re: Implementing Listener for Custom ListField

Try this:

 

//we need to determine which button was clicked

              if(field == cField) 
UiApplication.getUiApplication().pushScreen(new SpeedBumpScreen());
New Developer
Posts: 2
Registered: ‎08-31-2013
My Device: 9780
My Carrier: Vodafone

Re: Implementing Listener for Custom ListField

Hi 

 

I want to build a CustumListField 

 

like :

----------------------------------------------------------------

#AutoRichtextField                            # Button

#text

 

----------------------------------------------------------------

#AutoRichtextField                            # Button

#text

 

----------------------------------------------------------------

 

I also created like this way.

 

But the problem I m facing 

#1 I m not getting focus on the button and not able to click the button

#2 I m not able click and open the hyperlink present on the AutoRichextField.

 

When I click any of the item(Button or links), list item clicked.

 

can any one please help.

 

 

 

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

Re: Implementing Listener for Custom ListField

[ Edited ]

Welcome to the forum.

 

It is generally recommended that you post a new question on a new Thread, rather than extend a solved Thread.  That is what you should do with your question. 

 

But before you do, I think you need to revisit what a ListField does for you.  If gives you a row that you can focus on.  In a ListField, unless you have some very special coding, there is no separately selectable, focusable, or clickable parts. 

 

In your case it would appear that you want to create a a number of rows, each of which has separately focusable parts.  In this case, you would not use ListField, you would just create a Manager for each row, and add the Fields you want in one row, to one Manager.  Then add all the Row Managers to the screen.

 

I hope this moves you along a little,  If you get stuck again, please start a new question. 

 

Good luck