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
Developer
Posts: 69
Registered: ‎08-25-2008
My Device: Not Specified

Re: Problem with Tooltip ?

Thanks alot Peter_strange.
Developer
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Re: Problem with Tooltip ?

peter_strange, thanks for the code snippet.  I tried running it an if I scroll a lot (i.e. rollover about 5 fields with tooltips back and forth) I get a too many threads exception.  Do you know how to prevent this?  I would think that there should never be many more than 1 thread running because each time a field is unfocused TooltipThread.dismiss should be called and the thread should be destroyed right?
New Developer
Posts: 124
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with Tooltip ?

hai simon,

any sample code for the timer task

kamala

Developer
Posts: 16,575
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Problem with Tooltip ?

the key element is a FocusChangeListener.
on FOCUS_LOST the overlay is removed and the timer cancelled.
on FOCUS_GAINED the timer is created, a timertask scheduled (with 500ms), the timertask pushes the tooltip and cancels the timer.
this means that only one thread is active as the timer is cancelled for unfocused buttons.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 124
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with Tooltip ?

hai simon,

 I used the code provided by peter,I get the tooltipon focusing a bimap field but I am not able to push a screen when i click on the bitmapfield.

any sample code help me out.

kamala

 

             

New Developer
Posts: 11
Registered: ‎06-29-2009
My Device: Not Specified

Re: Problem with Tooltip ?

[ Edited ]

Hello all, realise i'm late to the discussion but i needed to do this so came across this thread, i ended up using rexdougs suggestion as it's much simpler, i put all the logic in it's own screen class:

 

 

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;

public class TooltipScreen extends MainScreen {

TooltipScreen screen = this;
boolean doRedraw = false;//prevent infinte redrawing
Vector tooltips = new Vector();//vector to hold tooltip strings
private Timer tooltipTimer = new Timer();
private TimerTask tooltipTask;
boolean alive = false;//is the tooltip alive? used to pop it after our timeout
int count = 0;//used to calculate time tooltip is displayed
//tooltip popup colours:
int backgroundColour = 0xeeeeee;
int borderColour = 0xaaaaaa;
int fontColour = 0x666666;
//the tooltip:
String tooltip;
int tooltipWidth;
int yCoord;
int xCoord;
//region parameters:
XYRect contentArea;
int contentBottom;
int contentRight;

public TooltipScreen() {
super();

//when timeout reaches 100ms*20 ie. 2seconds set alive to false and redraw screen:
tooltipTask = new TimerTask() {

public void run() {
if (alive) {
count++;
if (count == 20) {
alive = false;
invalidate();
}
}
}
};

tooltipTimer.scheduleAtFixedRate(tooltipTask, 100, 100);

}

//override add method adds an empty string to tooltip vector:
public void add(Field field) {
tooltips.addElement("");
super.add(field);
}

//custom add method for fields with tooltip: add(myField, "myTooltip");
public void add(Field field, String tooltip) {
super.add(field);
tooltips.addElement(tooltip);
}

public void setColours(int backgroundColour, int borderColour, int fontColour) {
this.backgroundColour = backgroundColour;
this.borderColour = borderColour;
this.fontColour = fontColour;
}

//reset everything when user changes focus,
//possibly needs logic to check field has actually changed (for listfields, objectchoicefields etc etc)
protected boolean navigationMovement(int dx, int dy, int status, int time) {
count = 0;
alive = true;
doRedraw = true;
return super.navigationMovement(dx, dy, status, time);
}

protected void paint(Graphics graphics) {
super.paint(graphics);
if (alive) {
Field focusField = getFieldWithFocus();
tooltip = (String) tooltips.elementAt(screen.getFieldWithFocusIndex());

//don't do anything outside the norm unless this field has a tooltip:
if (!tooltip.equals("")) {
//get the field content region, this may fall inside the field actual region/coordinates:
contentArea = focusField.getContentRect();
contentBottom = contentArea.y + contentArea.height;
contentRight = contentArea.x + contentArea.width;

//+4 to accomodate 2 pixel padding on either side:
tooltipWidth = graphics.getFont().getAdvance(tooltip) + 4;


yCoord = contentBottom - focusField.getManager().getVerticalScroll();
//check the tooltip is being drawn fully inside the screen height:
if (yCoord > (getHeight() - 30)) {
yCoord = getHeight() - 30;
}

//check the tooltip doesn't get drawn off the right side of the screen:
if (contentRight + tooltipWidth < getWidth()) {
xCoord = contentRight;
} else {
xCoord = getWidth() - tooltipWidth;
}

//draw the tooltip
graphics.setColor(backgroundColour);
graphics.fillRect(xCoord, yCoord, tooltipWidth, 30);
graphics.setColor(borderColour);
graphics.drawRect(xCoord, yCoord, tooltipWidth, 30);
graphics.setColor(fontColour);
graphics.drawText(tooltip, xCoord + 2, yCoord);
}
}
//doRedraw logic prevents infinite loop
if (doRedraw) {
//System.out.println("redrawing screen: " + System.currentTimeMillis());
screen.invalidate();
doRedraw = false;
}
}
}

 Just create your screen like you normally would but extend TooltipScreen instead of MainScreen, in your screen instance add fields normally using add() or if you want a tooltip use:

 

 

add(myField, "My Tooltip!");

 

 Hope this helps, may need extra logic to cope with more complex fields but it's usable as-is, original post here (with better code formatting...)

 

 

 

 

Message Edited by roygbiv on 06-29-2009 04:06 PM
Message Edited by roygbiv on 06-29-2009 04:12 PM
http://naviina.eu
Contributor
Posts: 10
Registered: ‎11-28-2010
My Device: Not Specified

Re: Problem with Tooltip ?

great work.... is there a way to use this tooltip class with the horizontalfieldmanager? to have my bitmapfields arranged horizontally.... please assist

Developer
Posts: 55
Registered: ‎07-30-2009
My Device: Not Specified

Re: Problem with Tooltip ?

If you are overriding the paint method of your MainScreen to implement tooltip (just as above provided), then this paint will be invoke for all your child fields (including managers), and inside paint you can check for which field you need to display like this:

 

// inside the paint method:

 

Field fieldWithfocus = getLeafFieldWithFocus();

            if (fieldWithfocus instanceof MyCustomField)
            {
                tooltipText = ((MyCustomField) fieldWithfocus).getTooltipText();                                                                                     

                // don't do anything outside the norm unless this field has a tooltip
                
                if (!tooltipText.equals(""))
                {

 

// remaining drawing stuff

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

Re: Problem with Tooltip ?

I feel I need to add to this, and should have a long time ago, my current thinking on tooltips, as described here:

 

http://supportforums.blackberry.com/t5/Java-Development/Tooltip-for-Button/m-p/1884957#M207523

 

It seems that the code originally supplied, that I added to and that supplied by r0ygbiv has been replicated round the web, including here:

http://v4ks1n.wordpress.com/2011/01/28/tooltips-class-for-blackberry/

and here:

http://supportforums.blackberry.com/t5/Java-Development/Tooltip-for-Button/m-p/1884901#M207516

 

I apologize to anyone who has implemented this code in the belief that I endorsed it.  I don't have any problem with tooltips in general and if people want to implement, then I would use the Screen overwrite technique as noted.  But you should be aware that I have removed this code from all production applications that I implemented it in.