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: 21
Registered: ‎06-23-2011
My Device: 9700
My Carrier: Vodafone

Help with code and adding a timer to a current working bit of code.

Hello,

 

Im new to Java development and im having a problem with my code, can someone please advise me as to the problem and recommend me a fix?

 

Ive tried to implement a timer into my current piece of code (using images as buttons,) to add a short delay in the apperance of a tooltip (1 - 1.5 seconds). However I am coming up stumps. I could really do with a pointer into the correct direction.

 

I have tried to implement this code, however I get "cannot access g must be delared as final"

 

Timer timer = new Timer();                                                  
         timer.schedule(new TimerTask() {
                public void run() {
                    try
                    {
                        // your event function
                    }
                    catch(NullPointerException npe)
                    {
                        System.out.println(npe);
                    }
                }
        },1500 );

 Here is my complete class.

 

package logapp;

//required imports
import net.rim.device.api.system.Bitmap;  
import net.rim.device.api.system.Display;  
import net.rim.device.api.ui.*;  
import net.rim.device.api.ui.component.*;  
import net.rim.device.api.ui.container.*; 
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.ui.container.HorizontalFieldManager; 
import net.rim.device.api.ui.container.VerticalFieldManager; 
import logapp.MyCustomButton;
import java.util.*;

class home_screen extends MainScreen
{      
  
    private MyCustomButton log = null;
    private MyCustomButton view = null;
    private MyCustomButton ui = null;
    private MyCustomButton about = null;
    private LabelField optionselected = null;
    
    // constructor
    public fgw_home_screen()
    {  
        createui();
    }
    
    public void createui()
    {
        
        //Some styling pleassssssseeee!
        this.getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(0x00251c3b,0x00251c3b,0x003f4f9a,0x003f4f9a)); 
        VerticalFieldManager container = new VerticalFieldManager(FIELD_HCENTER); 
        VerticalFieldManager inner = new VerticalFieldManager(FIELD_HCENTER); 
        HorizontalFieldManager menu = new HorizontalFieldManager(FIELD_HCENTER);
        Bitmap borderBitmap = Bitmap.getBitmapResource("assets/rounded-border.png");
        inner.setBorder(BorderFactory.createBitmapBorder(new XYEdges(12,12,12,12), borderBitmap)); 
        
        
        //Lets add our custom classed buttons to our menu
        MyCustomButton log = new MyCustomButton("assets/log.png", "assets/logo.png")
        {
            protected void onFocus(int direction) {
                super.onFocus(direction);
                showTooltip("Log a new fault", this);
            }
            protected void onUnfocus() {
                super.onUnfocus();
                hideTooltip();
            }
        };
        log.setBorder(BorderFactory.createSimpleBorder(new XYEdges(5,15,5,15), 0x00000000));
        MyCustomButton view = new MyCustomButton("assets/view.png", "assets/viewo.png")
                {
            protected void onFocus(int direction) {
                super.onFocus(direction);
                showTooltip("View your logged faults", this);
            }
            protected void onUnfocus() {
                super.onUnfocus();
                hideTooltip();
            }
        };
        view.setBorder(BorderFactory.createSimpleBorder(new XYEdges(5,15,5,15), 0x00000000));
        MyCustomButton ui = new MyCustomButton("assets/ui.png", "assets/uio.png")
                {
            protected void onFocus(int direction) {
                super.onFocus(direction);
                showTooltip("Update your information", this);
            }
            protected void onUnfocus() {
                super.onUnfocus();
                hideTooltip();
            }
        };
        ui.setBorder(BorderFactory.createSimpleBorder(new XYEdges(5,15,5,15), 0x00000000));
        MyCustomButton about = new MyCustomButton("assets/about.png", "assets/abouto.png")
                {
            protected void onFocus(int direction) {
                super.onFocus(direction);
                showTooltip("About FGW Log App", this);
            }
            protected void onUnfocus() {
                super.onUnfocus();
                hideTooltip();
            }
        };
        about.setBorder(BorderFactory.createSimpleBorder(new XYEdges(5,15,5,15), 0x00000000));
                    
        //Lets now add these to our horizontal manager
        menu.add(log);
        menu.add(view);
        menu.add(ui);
        menu.add(about);
        
        //Add stuff to be displayed on the screen
        LabelField spacer = new LabelField("\n \n", FOCUSABLE);
        container.add(spacer);
        LabelField optionselected = new LabelField ("Please select an option... \n \n");
        inner.add(optionselected);
        inner.add(menu);  
            
        container.add(inner);  
        add(container);
       
    }      

        }; 
        
        
     /*************************************************************
     *
     * This lines below used to implement tooltip functionality. *
     *
     *************************************************************/
    public void paint(Graphics g) {
        super.paint(g);
    /// Three lines below to show tooltip as needed.
        if (showTooltip) {
            drawTooltip(g);
        }
    }
    
    protected XYRect getFieldExtent(Field field) {
        int cy = field.getTop();
        int cx = field.getLeft();
        Manager m = field.getManager();
        while (m != null) {
            cy += m.getTop() - m.getVerticalScroll();
            cx += m.getLeft() - m.getHorizontalScroll();
            if (m instanceof Screen)
                break;
            m = m.getManager();
        }
        return new XYRect(cx, cy, field.getWidth(), field
                .getHeight());
    }
 
    private Font tooltipFont = getFont(); // initialized with default screen's font
    private String tooltipText;
    private int tooltipX, tooltipY, count, tooltipYDistance = 15;
    private boolean showTooltip;
    private int tooltipBgColor = 0x000000, tooltipBorderColor = 0x666666,
        tooltipFontColor = 0xFFFFFF, tooltipAlpha = 0xAA;
 
    protected void hideTooltip() {
        showTooltip = false;
        invalidate();
    }
 
    protected void showTooltip(String text, Field attachedTo) {
        
       if (showTooltip) hideTooltip();
 
        tooltipText = text;
        XYRect fieldPosition = getFieldExtent(attachedTo);
        tooltipX = fieldPosition.x + fieldPosition.width / 2;
        tooltipY = fieldPosition.y + attachedTo.getHeight();
        showTooltip = true;

        invalidate();
    }
 
    protected void drawTooltip(Graphics g) {                    
            
        int oldColor = g.getColor();
 
        Font font = tooltipFont;
 
        int padding = 8;
        int borderSize = 2;
 
        g.setFont(font);
        int x = tooltipX -
                (g.getFont().getAdvance(tooltipText) + padding) / 2;
        int y = tooltipY + tooltipYDistance;
        int width = font.getAdvance(tooltipText) + 2 * padding;
        int height = font.getHeight() + 2 * padding;
 
        if ((x + width) > Display.getWidth()) {
            x = Display.getWidth() - padding - width;
        }
        if (x < 0) {
            x = padding;
        }
 
        g.setGlobalAlpha(tooltipAlpha);
 
        g.setColor(tooltipBorderColor);
        g.fillRoundRect(x, y, width, height, font.getHeight(),
                font.getHeight());
        g.setColor(tooltipBgColor);
        g.fillRoundRect(x + borderSize, y + borderSize,
                width - 2 * borderSize, height - 2 * borderSize,
                font.getHeight() - borderSize,
                font.getHeight() - borderSize);
 
        // we make the text's color solid so user will see it clearly
        g.setGlobalAlpha(0xFF);
        g.setColor(tooltipFontColor);  
        g.drawText(tooltipText, x + padding, y + padding);
        g.setFont(font);
        g.setColor(oldColor);
          
    }
    
} 

 

Any help would be greatly appreciated, but also with descriptions as to how to successfully implement, Im here to learn afterall!

 

Also, another newbie question, but how would I load the screen with nothing focused or selected at all?

 

 

Many thanks in advance

 

Developer
Posts: 410
Registered: ‎06-03-2010
My Device: Z10 Red
My Carrier: Free

Re: Help with code and adding a timer to a current working bit of code.

Hi

 

When and where do you get the message "cannot access g must be delared as final" ?

What's the exact message? Is it given in the device Log or by eclipse? 

 

 

also, another newbie question, but how would I load the screen with nothing focused or selected at all?

 

You cannot. But you can create a  focusable  Field  in the top, with size 0px*0px to simulate that.

 

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

Re: Help with code and adding a timer to a current working bit of code.

I presume the 'must be final' message is generated at compile time.  Can you show us which line it is on.

 

While it is appropriate to have something focused, the Field focused need no necessarily show a focus indication.  The easiest Field for this is the NullField, which accepts focus, but is invisible.  So add a NullField to your screen and you might be able to have a screen with apparently nothing focused. 

Contributor
Posts: 21
Registered: ‎06-23-2011
My Device: 9700
My Carrier: Vodafone

Re: Help with code and adding a timer to a current working bit of code.

Thanks for your messages guys, Ive got the field in so that looks great - thanks!

 

So, the declared final thing is due to the following code... (as you can see its altered from the above working code example to try and put a timer on the action printing of the tooltip...

 

    public void paint(Graphics g) {
        super.paint(g);
    /// Three lines below to show tooltip as needed.
        if (showTooltip) {
             Timer timer = new Timer();                                                  
         timer.schedule(new TimerTask() {
                public void run() {
                    try
                    {
                        //its this thats causing the error - the g
                        drawTooltip(g);
                    }
                    catch(NullPointerException npe)
                    {
                        System.out.println(npe);
                    }
                }
        },1200 );
            
        }
    }

 

The exact error is

 

"local variable g is accessed from within inner class; needs to be declared final"

 

Ive got it working with just a "system.out.println("Test"); however a point I noticed was that it occurs on each roll over of a button, it doesnt "cancel" the first timer, then start a new instance of it...

 

Any advice is again greatly appriciated Smiley Happy



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

Re: Help with code and adding a timer to a current working bit of code.

Change

public void paint(Graphics g)

to

public void paint(final Graphics g)

 

to overcome this compile problem.

Highlighted
Contributor
Posts: 14
Registered: ‎05-18-2011
My Device: 9300, 9700
My Carrier: T-Mobile

Re: Help with code and adding a timer to a current working bit of code.

I have write code to implement using two classes TooltipScreen and TooltipListener

 

http://aliirawan-wen.blogspot.com/2011/06/using-tooltip-effect-in-blackberry-java.html

Until you spread your wings, You will have no idea how far you can fly
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Help with code and adding a timer to a current working bit of code.

Thanks for making this code available to us.  Could you perhaps put it on some Server that does not tell me I have a critical process problem and try to run a 'scan' of my system.  Alternatively, and perhaps easier, paste the code here?  Thanks