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
New Developer
Posts: 7
Registered: ‎11-12-2009
My Device: Not Specified

Field's touchEvent method is not triggering

Hi all,

 

Can anyone please help me spare my hair and let me know what is wrong and how I can resolve the following problem?

 

Problem.  No matter, if I click on the 'One' button or the 'Two' button, I always see 'one' being printed out.

 

 

/*
 * TestScreen.java
 */

package TestScreen;

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

class TestScreen extends UiApplication {
    
    class myScreen extends MainScreen {
        ButtonField field1 = new ButtonField("one") {
            public void run() {
                System.out.println("one");
            }
            
            public boolean touchEvent( TouchEvent message ) {
                switch ( message.getEvent() ) {
                    case TouchEvent.DOWN :
                        setFocus();    
                        return true;
                    case TouchEvent.UNCLICK:
                        run();
                        return true;
                }
                return false;
            }
        };
        ButtonField field2 = new ButtonField("two") {
            public void run() {
                System.out.println("two");
            }

            public boolean touchEvent( TouchEvent message ) {
                switch ( message.getEvent() ) {
                    case TouchEvent.DOWN :
                        setFocus();    
                        return true;
                    case TouchEvent.UNCLICK:
                        run();
                        return true;
                }
                return false;
            }
        };
        
        myScreen() {
            add(field1);
            add(field2);            
        }
    }
    
    TestScreen() {
        this.pushScreen( new myScreen() );
    }
    
    public static void main ( String[] args ) {
        TestScreen scr = new TestScreen();
        scr.enterEventDispatcher();        
    }
} 

Thank you in advance for any help you can provide.

 

New Developer
Posts: 7
Registered: ‎11-12-2009
My Device: Not Specified

Re: Field's touchEvent method is not triggering

[ Edited ]

I fixed the initial issue, by modifying it to this:

 

 

/*
* TestScreen.java
*/

package TestScreen;

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

class TestScreen extends UiApplication {

class myScreen extends MainScreen {
ButtonField field1 = new ButtonField("one") {
public void run() {
System.out.println("one");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field2 = new ButtonField("two") {
public void run() {
System.out.println("two");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};

myScreen() {
add(field1);
add(field2);
}
}

TestScreen() {
this.pushScreen( new myScreen() );
}

public static void main ( String[] args ) {
TestScreen scr = new TestScreen();
scr.enterEventDispatcher();
}
}

 

However, I then went a step further and created two Managers.

I added them to my screen and added two ButtonFields to each of the managers.

 

Now, I run into the exact same problem I had before.

So my request for help still stands ( unfortunately ). :smileysad:

 

 

/*
* TestScreen.java
*/

package TestScreen;

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

class TestScreen extends UiApplication {

class myScreen extends MainScreen {
ButtonField field1 = new ButtonField("one") {
public void run() {
System.out.println("one");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field2 = new ButtonField("two") {
public void run() {
System.out.println("two");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};

ButtonField field3 = new ButtonField("three") {
public void run() {
System.out.println("three");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field4 = new ButtonField("four") {
public void run() {
System.out.println("four");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};

myScreen() {
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
HorizontalFieldManager hfm2 = new HorizontalFieldManager();

hfm1.add(field1);
hfm1.add(field2);
hfm2.add(field3);
hfm2.add(field4);

add(hfm1);
add(hfm2);
}
}

TestScreen() {
this.pushScreen( new myScreen() );
}

public static void main ( String[] args ) {
TestScreen scr = new TestScreen();
scr.enterEventDispatcher();
}
}

The problem?  When I click 'One' or 'Two', the corresponding 'One' or 'Two' is printed in the output box.  However, if I click 'Three' or 'Four', nothing gets printed and I get a context menu.  Any ideas?

New Developer
Posts: 7
Registered: ‎11-12-2009
My Device: Not Specified

Re: Field's touchEvent method is not triggering

[ Edited ]

So, I believe I've fixed it.

 

My pet peeve.  If I want to consume a touchEvent in my field, why should I even have to touch my managers?  Better yet, can anyone explain to me, why it works in the following code?  As a Developer, I would expect to be able to implement the touchEvent method in my field and that's it.

 

 

/*
* TestScreen.java
*/

package TestScreen;

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

class TestScreen extends UiApplication {

class myScreen extends MainScreen {
ButtonField field1 = new ButtonField("one") {
public void run() {
System.out.println("one");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field2 = new ButtonField("two") {
public void run() {
System.out.println("two");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};

ButtonField field3 = new ButtonField("three") {
public void run() {
System.out.println("three");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field4 = new ButtonField("four") {
public void run() {
System.out.println("four");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};

ButtonField field5 = new ButtonField("five") {
public void run() {
System.out.println("five");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};
ButtonField field6 = new ButtonField("six") {
public void run() {
System.out.println("six");
}

public boolean touchEvent( TouchEvent message ) {
int x = message.getX(1);
int y = message.getY(1);
int w = getWidth();
int h = getHeight();
if ( x >= 0 && x <= w && y >= 0 && y <= h ) {
switch ( message.getEvent() ) {
case TouchEvent.DOWN :
setFocus();
return true;
case TouchEvent.UNCLICK:
run();
return true;
}
}
return false;
}
};


myScreen() {
HorizontalFieldManager hfm1 = new HorizontalFieldManager() {
public boolean touchEvent ( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
int fieldI = getFieldAtLocation(x, y);
Field field = getField( fieldI );
field.setFocus();
return super.touchEvent( message );
}
return false;
}
}
;
HorizontalFieldManager hfm2 = new HorizontalFieldManager() {
public boolean touchEvent ( TouchEvent message ) {
int x = message.getGlobalX(1);
int y = message.getGlobalY(1);
if ( getExtent().contains( x, y ) ) {
int fieldI = getFieldAtLocation(x, y);
Field field = getField( fieldI );
field.setFocus();
return super.touchEvent( message );
}
return false;
}
}
;

hfm1.add(field1);
hfm1.add(field2);
hfm1.add(field3);
hfm2.add(field4);
hfm2.add(field5);
hfm2.add(field6);


add(hfm1);
add(hfm2);
}
}

TestScreen() {
this.pushScreen( new myScreen() );
}

public static void main ( String[] args ) {
TestScreen scr = new TestScreen();
scr.enterEventDispatcher();
}
}

Notice that in the latest version, I added [2] additional fields, one to each Manager ( just for fun ).

It does work as expected though.

 

It's extremely interesting that I have to touch my managers to get this to work properly.  Is this a bug, or is this expected?  Hopefully it helps someone.