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 Contributor
Posts: 3
Registered: ‎01-06-2011
My Device: Not Specified
Accepted Solution

Transparent ToolTip like default on button focus.

How can I implement a trasparent tooltip like the default one?

I refer to the white text that appears on the focus of the icons in the menu.

Thanx in advance.

Highlighted
Developer
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold
My Carrier: Telefonica

Re: Transparent ToolTip like default on button focus.

Hi, 

in this link: 

http://supportforums.blackberry.com/t5/Java-Development/Problem-with-Tooltip/m-p/102847/highlight/tr...

 

user peter_strange put a code for a tooltip:

 

I'm only copy & paste

 

 

package ..;

/*

Display a 'Tooltip" (String) at a specified location for s specified time
This Tooltip can also be removed.
Note that in my testing, the display time is not consistent.

Following example shows it being added and removed using focus events.

ButtonField bf = new ButtonField("Test") {
    MyTooltip _tooltip;
    protected void onFocus(int direction) {
        if ( _tooltip != null ) {
            _tooltip.removeToolTip();
            _tooltip = null;
        }
        // Display tooltip at 50,50 for 5 seconds
        _tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), "Press to test", 50, 50, 5);;
    }
    protected void onUnfocus() {
        if ( _tooltip != null ) {
            // We have displayed a Tooltip - remove it
            _tooltip.removeToolTip();
            _tooltip = null;
        }
    }
 };

*/


import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

class MyTooltip extends PopupScreen{
    int _x;
    int _y;
    TooltipThread _tooltipThread;

    private MyTooltip(Manager manager) {
        super(manager);
     }
    public void sublayout(int width, int height)    {
        super.sublayout(width,height);
        setPosition(_x,_y);
        System.out.println("Tooltip x: " + Integer.toString(_x) + ", y: " + Integer.toString(_y));
    }
    protected void applyTheme() {
        // Overriden to suppress Border etc.
    }
    public void removeToolTip() {
        if ( _tooltipThread != null ) {
            _tooltipThread.dismiss();
        }
    }
    private void display(UiApplication uiApp, int x, int y, int displayTime) {
        _x = x;
        _y = y;
        _tooltipThread = new TooltipThread(uiApp, this, displayTime);
        _tooltipThread.start();
    }

    public static MyTooltip addToolTip(UiApplication uiApp, String toolTipString, int x, int y, int displayTime) {
        VerticalFieldManager manager = new VerticalFieldManager(Manager.FIELD_VCENTER|Manager.NON_FOCUSABLE) {
            protected void paint(Graphics graphics) {   
                graphics.setColor(0x00FFFFFF); // White
                graphics.fillRect(0,0,getWidth(),getHeight());
                graphics.setColor(0x00000000); // Black
                graphics.drawRect(0,0,getWidth(),getHeight());
                super.paint(graphics);
            }
        };
        MyTooltip toolTip = new MyTooltip(manager);
        LabelField label = new LabelField(' ' + toolTipString + ' ', LabelField.NON_FOCUSABLE);
        label.setFont(Font.getDefault().derive(Font.PLAIN, 16));
        toolTip.add(label);
        toolTip.display(uiApp, x, y, displayTime);
        return toolTip;
    }

    class TooltipThread extends Thread {

        Object _notifyObject = new Object(); // Used to allow user to dismiss this Tooltip
        PopupScreen _tooltip; // Screen we are going to display
        UiApplication _ourApplication; // access to pushGlobalScreen and dismissStatus from our Application
        int _displayTime; // in seconds

        public TooltipThread(UiApplication ourApplication, PopupScreen tooltip, int displayTime) {
            _tooltip = tooltip;
            _ourApplication = ourApplication;
            _displayTime = displayTime;
        }
            
        public void run() {
            _ourApplication.pushGlobalScreen(_tooltip, 999, false);
            synchronized(_notifyObject) {
                try {
                    _notifyObject.wait(_displayTime * 1000);
                } catch (Exception e) {
                }
            };
            _ourApplication.dismissStatus(_tooltip);
        }

        public void dismiss() {
            // notify the waiting object to stop the Thread waiting
            synchronized(_notifyObject) {
                _notifyObject.notify();
            }
        }

    }


}

 

pd:sorry for my english.

 

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Transparent ToolTip like default on button focus.

 


andrax wrote:

How can I implement a trasparent tooltip like the default one?

I refer to the white text that appears on the focus of the icons in the menu.

Thanx in advance.


 

Welcome to the forums!

 

Depending on what exactly you are trying to achieve, you might want a more specific or a more generic approach.

 

For example, if you want a screen where you could specify a tooltip for any field and it would display that, take a look here:

Tooltips class for BlackBerry

You might want to modify the code to get the exact effect you need, but that's a great start nonetheless. Take a look at xcoord and ycoord calculations, use graphics.setGlobalAlpha to change transparency before drawing text, decide if you want to keep or remove the rectangle around tooltip, etc.

 

If you want to create a Field which will show an image just like a BitmapField but know its tooltip and display it on focus, override Field or BitmapField and override its drawFocus and paint method. For example:

 

public class IconWithTooltip extends BitmapField {
  private String _tooltip = "";

  public IconWithTooltip(Bitmap bitmap, String tooltip) {
    super(bitmap);
    _tooltip = tooltip;
  }
  public IconWithTooltip(Bitmap bitmap, long style, String tooltip) {
    super(bitmap, style);
    _tooltip = tooltip;
  }
  public setTooltip(String tooltip) {
    _tooltip = tooltip;
    if (isFocus()) {
      invalidate();
    }
  }
  protected void drawFocus(Graphics g, boolean on) {
    paint(g);
  }
  protected void paint(Graphics g) {
    super.paint(g);
    if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) && (_tooltip != null)) {
      Font font = getFont();
      int fontHeight = font.getHeight();
      int tooltipWidth = font.getAdvance(_tooltip);
      int xPosition = (getWidth() - tooltipWidth ) / 2;
      int yPosition = (getHeight() - fontHeight) / 2;
      int oldAlpha = g.getGlobalAlpha();
      g.setGlobalAlpha(150);
      int oldColor = g.getColor();
      g.setColor(Graphics.FULL_WHITE);
      g.drawText(_tooltip, xPosition, yPosition);
      g.setColor(oldColor);
      g.setGlobalAlpha(oldAlpha);
    }
  }
}

 

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
New Contributor
Posts: 3
Registered: ‎01-06-2011
My Device: Not Specified

Re: Transparent ToolTip like default on button focus.

[ Edited ]

Thanks!!!

This is what I'm searching.

 

 

 

 

Regular Contributor
Posts: 52
Registered: ‎10-07-2010
My Device: BlackBerry Curve 8520
My Carrier: Digitel

Re: Transparent ToolTip like default on button focus.

Hey i'm using the code you accepted as solution but it's not working, when i run my app i get no errors no warnings but the app doesn't show up in the downloads folder. Any idea why this happens?

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Transparent ToolTip like default on button focus.

Look at the Packaging Console to see if it has an error in there. Sometimes you'll get random errors that won't be displayed in a popup but are listed there.

Regular Contributor
Posts: 52
Registered: ‎10-07-2010
My Device: BlackBerry Curve 8520
My Carrier: Digitel

Re: Transparent ToolTip like default on button focus.

I solved the problem by changing the PATH and setting it properly and replacing the deprecated methods with the new ones. Now i'm getting an IllegalStateException herE:

 

 

public void run() {
    	try{
			_ourApplication.pushGlobalScreen(_tooltip, 999, UiApplication.GLOBAL_SHOW_LOWER);					
    	} catch(Exception e){
    		System.out.println(e.getMessage());
    	}
        
        synchronized(_notifyObject) {
            try {
                _notifyObject.wait(_displayTime * 1000);
            } catch (Exception e) {
            	System.out.println(e.getMessage());
            }
        };
        _ourApplication.popScreen(_tooltip);
    }

 

it reads: Ui engine accessed without holding event lock, i've made no changes to the code except adding the try catch block

 

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Transparent ToolTip like default on button focus.

try adding a synchronize(UiApplication.getEventLock()) { } around your pushGlobalScreen() and popScreen() calls.

Regular Contributor
Posts: 52
Registered: ‎10-07-2010
My Device: BlackBerry Curve 8520
My Carrier: Digitel

Re: Transparent ToolTip like default on button focus.

It's a no go i still get the exception

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Transparent ToolTip like default on button focus.

Hmm, maybe try using UiApplication.getUiApplication().invokeLater() instead.