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
VBRefugee
Posts: 47
Registered: ‎02-26-2010
My Device: Torch 9850
Accepted Solution

Set Background code doesn't work

[ Edited ]

Hello.

 

I've placed the code shown below on the mainscreen page, just below the import statements.

 

The code is from a knowledge base article. It compiles and the debugger is happy but the screen color on the BlackBerry Simulator screen remains white.

 

I imported the required support APIs. What might I have missed?

 

class DecorTestScreen extends MainScreen {

    private VerticalFieldManager _manager;
    
    DecorTestScreen() {
        _manager=(VerticalFieldManager)getMainManager();
        
        Background bg = BackgroundFactory.createSolidBackground(Color.CYAN);
        _manager.setBackground(bg);
        
    }


}


Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Set Background code doesn't work

Is this the entire code for the screen class?

 

If so, you might want to set the title and add a field. The Manager class tends to assume only the size of it's contained field(s). With no fields, it's extent will likely be 0,0.

 

Also, you might want to try adding the background to the screen, rather than the Manager, just for grins.

 

Warning: take my suggestions with a grain of salt, as I've never used these background classes. Most of our code was originally written on 4.0, so we invented all of this stuff (way back when).

 

:smileyhappy:

 

 

 

 

 

Developer
jMarc
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Set Background code doesn't work

Your code works fine when I run it.  I'm using Component Package 4.6.0 (BackgroundFactory did not exist before 4.6.0) with the default simulator, happens to be a 9000. So whatever you're doing that isn't working, it's something you're not telling us.

 

RexDoug -- good thoughts, but I tried with and without Fields on the Screen, and it worked both ways.  The subject has come up before though: you really do need to set the background on the Manager, not the Screen!

Contributor
VBRefugee
Posts: 47
Registered: ‎02-26-2010
My Device: Torch 9850

Re: Set Background code doesn't work

[ Edited ]

RexDoug and Jmarc...

 

The code down to where the classes associated with the application calls start is this:

 

// Establish a way for the application to communicate

package com.avwx.ui;

// Import the APIs that will be needed.

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;


// Set the application's MainScreen Class and use the API holding
// MainScreen methods and functions plus implement the
// FieldChangeListener method from the net.rim.device.api.ui API.

public class AvWxMainScreen extends MainScreen implements FieldChangeListener {

// Change screen color of MainScreen background to Cyan.    
    
    class DecorTestScreen extends MainScreen {

        private VerticalFieldManager _manager;
        
        DecorTestScreen() {
            _manager=(VerticalFieldManager)getMainManager();
            
            Background bg = BackgroundFactory.createSolidBackground(Color.CYAN);
            _manager.setBackground(bg);
            
        }


    }

//Declare the Button Fields.    

    

Since the color changer's class extended MainScreen, I thought that was sufficient.

 

I apparently have to create/set something else that I haven't.

 

Pardon the lack of experience but could you give me a little bit more of a nudge in the direction of what I have to do?

 

Thanks.

 

 

Developer
ydaraishy
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Set Background code doesn't work

[ Edited ]

Don't you have to say something along the lines of this.add(_manager)? (My memory is rusty).

 

And why have you got a nested class? Or was some code snipped out?

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Set Background code doesn't work

...and when does this inner MainScreen class get instantiated and pushed onto the display stack?

 

 

Contributor
VBRefugee
Posts: 47
Registered: ‎02-26-2010
My Device: Torch 9850

Re: Set Background code doesn't work

[ Edited ]

I had the code above the main class instance and, when it didn't work, moved it to where it is -- I'll move it back if necessary but it doesn't work in a non-nested situation either.

 

The main screen instance is on another page called AvWxApp and thats where the pushScreen is also.

 

Here is that code:

 

// Establish a way for the application to communicate
// between classes, methods and events.

package com.avwx.ui;

// Import the needed API.

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

// Define the application's public class and extend the
// User Interface (UI) API functionality throughout the application.

public class AvWxApp extends UiApplication {

// Define the Main Class and create a screen handler mechanism
// and a way to capture and act upon user inputs.    
    
    public AvWxApp() {
        
        AvWxMainScreen mainScreen = new AvWxMainScreen();
        pushScreen(mainScreen);}
    
        public static void main(String[] args) {
        AvWxApp app = new AvWxApp();
        app.enterEventDispatcher();
        
    }

}

 

Thanks again -- even if you can't get the problem solution through my thick head.

 

 

 

 

Developer
jMarc
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Set Background code doesn't work

Well, sir, you led me a merry chase!

 

Not your fault.  I dropped some of your code into an existing project and sorta neglected to remember that I already had a main method in that project.  I got your symptoms (blank white screen), and lots of other things broke, too.  All irrelevant, an instance of allowing the symptoms to mislead me, looking for something complicated when the answer is simple.  You're doing the same thing.

 

Go back to the comments from ydaraishy and RexDoug: you have a nested class that is never instantiated and pushed on the Display stack.

 

In the code you've shown us, you create and push an instance of AvWxMainScreen but not of DecorTestScreen, and it's DecorTestScreen that has a Manager with a cyan background. Or would, if an instance of it existed.

 

Clearer?

Contributor
VBRefugee
Posts: 47
Registered: ‎02-26-2010
My Device: Torch 9850

Re: Set Background code doesn't work

[ Edited ]

jMarc...

 

Well, no good deed goes unpunished -- so helping me resulted in your punishment.

 

Yes it's clearer, I think.

 

If I place the manager where the AvWxMainScreen instance is created and rename DecorTestScreen to AvWxMainScreen, should it then work?

 

John

 

Developer
jMarc
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Set Background code doesn't work

Well, from what you've given us, I don't see why you have DecorTestScreen at all. Stripping out your variables just to keep the sample code shorter, you get this:

 

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

public class AvWxMainScreen extends MainScreen {
public AvWxMainScreen() {
getMainManager().setBackground(
 BackgroundFactory.createSolidBackground(Color.CYAN));

}
}

 This assumes two public classes in two modules; you can do it in one module by making the Screen a private inner class, of course.  I haven't actually run this so there may be errors, but this is essentially the code I ran.

 

Maybe you have a reason for a third class -- you want to play with it and then throw it away, you want to let the user start it with a button, whatever.  That's okay, too, but you have to instantiate it somewhere. Again, I have not run this, just ginned it up on the fly, and it's a skeleton -- for instance, you might need to do something about closing the Screens -- but here's the general idea:

 

public class AvWxMainScreen extends MainScreen
implements FieldChangeListener {
public AvWxMainScreen() {
getMainManager().setBackground(
BackgroundFactory.createSolidBackground(Color.CYAN));
ButtonField blueButton = new ButtonField("Go Blue!");
blueButton.setFieldChangeListener(this);
}

public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().push(new DecorTestScreen());
}

private class DecorTestScreen extends MainScreen {
public DecorTestScreen() {
getMainManager().setBackground(
BackgroundFactory.createSolidBackground(Color.BLUE));

}
}
}

Once you shake the bugs out, this should give you a cyan screen with a button on it.  Pressing the button should give you a blank blue screen.

 

Let me know if my sample is too broken to fix, but it should be enough to get you moving.