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
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
Accepted Solution

returning int value using get

I know this is probably a simple question but I could not find an answer after a lot of searching. In the following code it says the problem is in the  " t2.getHelp4(hello);" line but it isn't giving me an approriate fix. I simply want to get an int from another class. Thanks for any help guys! 

 

final class mainUIScreen extends MainScreen {
   
    public mainUIScreen()
    {
        super();
       
        final VerticalFieldManager Vman = new VerticalFieldManager();

        final int t2 = 0;
       
        LabelField t1 = new LabelField("Main screen"+t2);

        Vman.add(t1);

        ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER) {
        };

        FieldChangeListener lnextBut = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {   
                t2.getHelp4(hello);
                LabelField t3 = new LabelField("hi: "+ t2);
                Vman.add(t3);
            }
        }
        ;
        nextBut.setChangeListener(lnextBut);

        Vman.add(nextBut);
}}

 

public class Help4 {

    public static int getHelp4() {
        int hello = 5;
        return hello;
    }
   
}

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: returning int value using get

well...you seems to be java newbee..

use .. t2 = Help4.getHelp4();

it will retun 5 and assign to t2. Why are you passing undeclared 'hello' to getHelp4() which doesn't accept any argument. It will be good if you read about java basics.
Please use plain text.
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: returning int value using get

Thanks that worked but the purpose of it was to pass the int outside of the button, like this:

 

final class mainUIScreen extends MainScreen {
   
    public mainUIScreen()
    {
        super();
       
        final VerticalFieldManager Vman = new VerticalFieldManager();

        ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER) {
        };

        FieldChangeListener lnextBut = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {   
                int t2 = Help4.getHelp4();
                LabelField t3 = new LabelField("hi: "+ t2);
                Vman.add(t3);
            }
        }
        ;
        nextBut.setChangeListener(lnextBut);

        Vman.add(nextBut);
       
        LabelField t1 = new LabelField("Main screen"+t2); // this t2 'cannot be resolved to a variable'

        Vman.add(t1);
}}

 

So the t2 isn't being passed outside of the FieldChangeListener...Why is that and how can I fix it? Thanks for your help, I know I'm new but I learn best by doing so please bare with me.

 

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: returning int value using get

Scope of int t2 is local which means life of t2 is limited between { }. t2 is not available outside it's brackets. To access t2 outside this bracket, u need to declare it in outside bracket. So somewhere in beginning (after super()) of your mainUIScreen() constructor, declare int t2;

It will be helpful to do some reading on java first as it may save you lots of time and headache on this type of basic errors.
Please use plain text.
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: returning int value using get

I have tried that already...I rewrote it and the same result. The labelField shows up as '0' not as 5. If I put only 'int t2;' it tells me to initialize it. So, how can I do it?

 

final class mainUIScreen extends MainScreen {
   
    public mainUIScreen()
    {
        super();
        int t2=0;
       
        final VerticalFieldManager Vman = new VerticalFieldManager();
       
        ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER) {
        };

        FieldChangeListener lnextBut = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {   
                int t2 = Help4.getHelp4();
                LabelField t3 = new LabelField("hi: "+ t2);
                Vman.add(t3);
            }
        }
        ;
        nextBut.setChangeListener(lnextBut);

        Vman.add(nextBut);
       
        LabelField t1 = new LabelField("Main screen"+t2);

        Vman.add(t1);
        add(Vman);
}}

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: returning int value using get

assigning any value to variable is initialized. So when you do int t2 = 0;
0 is assigned to t2 which you are reading in labelfield.
In your listener you are creating a new variable int t2 which has again local scope. So remove "int" before t2.This will assign value to your previously created t2.
Please use plain text.
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: returning int value using get

I appreciate the help indusBull, but it is still not working. Perhaps you could try the code or show me another example? This 'simple' problem has been frustrating me all day. It is underlining 't2' inside the listener and saying "The final local variable t2 cannot be assigned, since it is defined in an enclosing type" Thanks for your help.

 

public class mainScreen extends UiApplication {
   
    public static void main(String[] args){
   
        mainScreen main = new mainScreen();
        main.enterEventDispatcher();
    }
   
    public mainScreen()
    {
        pushScreen(new mainUIScreen());
    }

}

final class mainUIScreen extends MainScreen {
   
    public mainUIScreen()
    {
        super();
        final int t2 = 0;    //// I tried 'int t2 = 0; and it told me I should make it final, either way does not work
       
        final VerticalFieldManager Vman = new VerticalFieldManager();
       
        ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER) {
        };

        FieldChangeListener lnextBut = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {   
                t2 = Help4.getHelp4();
                LabelField t3 = new LabelField("hi: "+ t2);
                Vman.add(t3);
                }
        }
        ;
        nextBut.setChangeListener(lnextBut);

        Vman.add(nextBut);
       
        LabelField t1 = new LabelField("Main screen"+t2);

        Vman.add(t1);
        add(Vman);
}}

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: returning int value using get

Create t2 as member variable of class mainUIScreen.

final class mainUIScreen extends MainScreen {

int t2 = 0;

.............
................
....
}

Would be good if you can do same thing for all field variables like vertical field manager, button field etc..
Please use plain text.
Developer
jhw1701
Posts: 176
Registered: ‎11-06-2008
My Device: Not Specified

Re: returning int value using get

try this

public class mainScreen extends UiApplication { public static void main(String[] args) { mainScreen main = new mainScreen(); main.enterEventDispatcher(); } public mainScreen() { pushScreen(new mainUIScreen()); } } final class mainUIScreen extends MainScreen implements FieldChangeListener { private ButtonField nextBut; private int t2 = 0; private VerticalFieldManager Vman; public mainUIScreen() { super(); Vman = new VerticalFieldManager(); nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER); nextBut.setChangeListener(this); Vman.add(nextBut); LabelField t1 = new LabelField("Main screen" + t2); Vman.add(t1); add(Vman); } public void fieldChanged(Field field, int context) { if (field == nextBut) { t2 = Help4.getHelp4(); LabelField t3 = new LabelField("hi: " + t2); Vman.add(t3); } } } class Help4 { public static int getHelp4() { int hello = 5; return hello; } }

 

Please use plain text.
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: returning int value using get

Thank you for showing me that work around, it's very useful. One last question, how can I replace the

LabelField t1 = new LabelField("Main screen" + t2);

to overwrite it instead of it always displaying 0. I assume I could use    setText ? Thanks again.

Please use plain text.