Marquee Text on Blackberry - blackberry

I want to marquee some text in a BlackBerry app.

Here it is. Just make a class with name MarqueeLabel and copy-paste this code then you can use this class to display a marquee text:
package mypackage;
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
class MarqueLabel extends LabelField {
int currentChar = 0;
String currentText = null;
Font ourFont;
private Timer _scrollTimer;
private TimerTask _scrollTimerTask;
public MarqueLabel(String text, long style) {
super(text, style);
}
public void paint(Graphics graphics) {
currentText = this.getText();
if (currentChar < currentText.length()) {
currentText = currentText.substring(currentChar);
}
graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, Display.getWidth());
super.paint(graphics);
}
public void layout(int width, int height) {
ourFont = this.getFont();
setExtent(500, ourFont.getHeight());
}
protected void onDisplay() {
startScroll();
}
protected void onUnfocus() {
startScroll();
}
private void startScroll() {
// Start scrolling
final String fullText = this.getText();
if (_scrollTimer == null) {
_scrollTimer = new Timer();
_scrollTimerTask = new TimerTask() {
public void run() {
currentChar = currentChar + 2;
if (currentChar > fullText.length()) {
currentChar = 0;
}
invalidate();
}
};
_scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 450);
}
}
protected void onFocus(int direction) {
if (_scrollTimer != null) {
_scrollTimerTask.cancel();
_scrollTimer.cancel();
_scrollTimer = null;
_scrollTimerTask = null;
}
}
protected boolean navigationMovement(int dx, int dy,
int status, int time) {
currentText = this.getText();
int oldCurrentChar = currentChar;
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
currentChar = Math.min(currentText.length() - 1,
currentChar + 1);
} else if (dx < 0) {
currentChar = Math.max(0, currentChar - 1);
}
if (oldCurrentChar != currentChar) {
this.invalidate();
}
return true;
} else {
return super.navigationMovement(dx, dy, status, time);
}
}
}

Related

How Can I use blackberry browser in my view hiearchy?

I need to show some links in my application's views. But I can't find anything about it. is it possible??
I want to place browsercontent(field) where gray area for os <5.00 ?
If you want to show a field which is a hyperlink use the following code.
package com.myApp.controls;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class HrefField extends Field {
private String content;
private Font fieldFont;
private int fieldWidth;
private int fieldHeight;
private boolean active = false;
private int backgroundColour = Color.WHITE;
private int textColour = Color.BLACK;
private int[] drawFocusColors;
public HrefField(String content) {
super(Field.FOCUSABLE);
this.content = content;
fieldFont = Font.getDefaultFont();
fieldWidth = fieldFont.getAdvance(content) + 2;
fieldHeight = fieldFont.getHeight() + 3;
drawFocusColors = new int[] { Color.ORANGE,
Color.ORANGE,Color.RED,
Color.RED};
}
public void setColours(int backgroundColour, int textColour) {
this.backgroundColour = backgroundColour;
this.textColour = textColour;
invalidate();
}
public void setBackgroundColour(int backgroundColour) {
this.backgroundColour = backgroundColour;
invalidate();
}
public void setTextColour(int textColour) {
this.textColour = textColour;
invalidate();
}
public void setMaskColour() {
invalidate();
}
public void setFont(Font fieldFont) {
this.fieldFont = fieldFont;
}
public int getPreferredWidth() {
return fieldWidth;
}
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int arg0, int arg1) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void paint(Graphics graphics) {
int[] X_PTS = new int[] { 0, fieldWidth, fieldWidth, 0 };
int[] Y_PTS = { 0, 0, fieldHeight, fieldHeight };
if (active) {
graphics.drawShadedFilledPath(X_PTS, Y_PTS, null, drawFocusColors,
null);
} else {
graphics.setColor(backgroundColour);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
graphics.setColor(textColour);
graphics.setFont(fieldFont);
graphics.drawText(content, 1, 1);
graphics.drawLine(1, fieldHeight - 2, fieldWidth - 2, fieldHeight - 2);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void onFocus(int direction) {
active = true;
invalidate();
}
protected void onUnfocus() {
active = false;
invalidate();
}
}

Blackberry custom ButtonField with icon and text

How would I go about creating a custom ButtonField that includes an icon? I have found several different sources for a ButtonField with a custom background Bitmap, but I want to have a button with an Icon and text under it. I can not find anyt informaiton about this, maybe someone here could help me out?
Cheers
Try this
package com.MYAPP.controls;
import com.MYAPP.bll.Log;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class ButtonControl extends Field {
private Bitmap selectedBitmap;
private Bitmap unSelectedBitmap;
private String _label;
protected boolean focus = false;
protected int bWidth = 0;
protected int bHeight = 0;
int padding = 0;
Font font=Font.getDefault();
public ButtonControl(String icon, String label) {
super(Field.FOCUSABLE);
_label = label;
try {
selectedBitmap = Bitmap.getBitmapResource(icon + "_focus.png");
unSelectedBitmap =Bitmap.getBitmapResource(icon + ".png");
padding = 0;
} catch (Exception ex) {
Log.Error(ex, "Cannot load icon {" + icon + "}");
}
bWidth = selectedBitmap.getWidth();
bHeight = selectedBitmap.getHeight();
}
protected void onFocus(int direction) {
focus = true;
invalidate();
}
protected void onUnfocus() {
focus = false;
invalidate();
}
public void drawFocus(Graphics g, boolean on) {
// do nothing... this is handled in paint...
}
protected int moveFocus(int amount, int status, int time) {
// This ensures background is redrawn properly if this control is
// the first or last in a series (eliminate white background issue)
invalidate();
return super.moveFocus(amount, status, time);
}
protected void moveFocus(int x, int y, int status, int time) {
// This ensures background is redrawn properly if this control is
// the first or last in a series (eliminate white background issue)
invalidate();
super.moveFocus(x, y, status, time);
}
int _labelWidth;
int posbitmapX=0;
int x=0;
int y = 0;
protected void paint(Graphics g) {
int oldColor = g.getColor();
Font oldFont = g.getFont();
g.setFont(getTextFont());
x = (getWidth() - bWidth) / 2;
if (focus) {
g.setColor(Color.WHITE);
g.drawBitmap(x, y, bWidth, bHeight, selectedBitmap, 0, 0);
} else {
g.setColor(Color.BLACK);
g.drawBitmap(x, y, bWidth, bHeight, unSelectedBitmap, 0, 0);
}
g.setFont(getTextFont());
_labelWidth = getTextFont().getAdvance(_label);
g.drawText(_label, 0, bHeight + 2, Graphics.HCENTER, getWidth());
g.setColor(oldColor);
g.setFont(oldFont);
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
private Font getTextFont() {
return font;
}
public int getPreferredWidth() {
return selectedBitmap.getWidth();
}
public int getPreferredHeight() {
return (bHeight + 2 + font.getHeight() + 2);
}
protected boolean keyChar(char key, int status, int time) {
if (key == Characters.ENTER) {
return this.navigationClick(status, time);
}
return super.keyChar(key, status, time);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void fieldChangeNotify(int context) {
try {
this.getChangeListener().fieldChanged(this, context);
} catch (Exception e) {
Log.Debug("Error responding to field change: " + e);
}
} }

Blackberry: custom ButtonField with disappearing focus highlight

I'm trying to create a custom ButtonField, whose focus (the blue highlight color) would disappear after few seconds of inactivity - like in original music player on BB phones with touchscreen.
I've almost succeeded in that with the following example:
Here are the east.png and west.png (courtesy of openclipart.org):
Here is my test code MyFocus.java:
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.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
add(new MyButton(MyButton.EAST));
add(new MyButton(MyButton.WEST));
add(NF);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL,
BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
// display red background on long touch and hold
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.super.onUnfocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
My problem is visible, when I first select a button and wait few seconds for its focus to disappear. Then I click on the track pad and while the button is pushed (verified that), you don't see anything at the screen - it doesn't turn blue or red.
I've tried all combinations of navigationClick() and trackwheelUnclick() but can not fix that.
Any help please?
Alex
UPDATE 1:
I've tried the following, but it doesn't work well (focus disappears forever, probably because button thinks it is in the needed visual state already):
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.this.setBorder(BorderFactory.createSimpleBorder(EDGES));
MyButton.this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
invalidate();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
UPDATE 2:
A try with a NullField, still not working properly:
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.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static NullField NF = new NullField();
HorizontalFieldManager hfm = new HorizontalFieldManager() {
int lastFocused = -1;
protected int firstFocus(int direction) {
lastFocused = super.firstFocus(direction);
System.out.println("XXX firstFocus: " + lastFocused);
return lastFocused;
}
protected int nextFocus(final int direction, final int axis) {
if (getFieldWithFocus() == NF) {
return lastFocused;
}
lastFocused = super.nextFocus(direction, axis);
System.out.println("XXX nextFocus: " + lastFocused);
return lastFocused;
}
};
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
hfm.add(new MyButton(MyButton.WEST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(NF);
add(hfm);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyScreen.NF.setFocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
I would probably ovverride the drawFocus() method to check a flag _showFocus before calling super.drawFocus(), then in onFocus(), set the flag to true and schedule the Runnable to change the _showFocus flag to false and invalidate (also setting it to false in onUnfocus()). Don't have to worry about canceling timers or anything this way either, as it is just going to change a flag and invalidate, keeping it in the appropriate state.

BasicEditField BlackBerry

I want a BasicEditField with rounded border and white background fill.
This is my code
public class BasicField extends BasicEditField {
XYEdges padding = new XYEdges(0,0,0,0);
int color = Color.BLACK;
int lineStyle = Border.STYLE_SOLID;
int Width, Height;
Border roundedBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
public BasicField()
{
super(BasicEditField.NO_NEWLINE);
//this.setPadding(2, 2, 2, 2);
//this.setBorder(roundedBorder);
}
public int getPreferredWidth()
{
int labelWidth = getFont().getAdvance(getLabel()) - 1;
Width = Graphics.getScreenWidth() -150;
return Width;
}
public int getPreferredHeight()
{
return 10;
}
public void paint(Graphics g) {
int currCol = g.getColor();
g.setBackgroundColor( Color.WHITE );
g.fillRect(0, 0, getPreferredWidth(), getPreferredHeight() );
g.clear();
g.setColor( Color.NAVY );
super.paint( g );
}
protected void layout( int maxWidth, int maxHeight )
{
super.layout( getPreferredWidth(), getPreferredHeight() );
}
}
Here you go -
Screenshot:
border.png:
MyEdit.java:
import net.rim.device.api.system.*;
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.*;
public class MyEdit extends UiApplication {
public static void main(String args[]) {
MyEdit app = new MyEdit();
app.enterEventDispatcher();
}
public MyEdit() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(20, 16, 27, 23),
Bitmap.getBitmapResource("border.png"));
BasicEditField myField = new BasicEditField(TextField.NO_NEWLINE) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText("Search", 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
public MyScreen() {
myField.setBorder(myBorder);
setTitle(myField);
}
}
Friend... try this code.....
class BasicEditScreen extends MainScreen
{
public BasicEditScreen()
{
add(new BasicField(200, 40, FIELD_LEFT, "Enter here..", "horizontal"));
}
}
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class BasicField extends Manager
{
private int managerWidth;
private int managerHeight;
private int inactiveBorderColor = Color.GRAY;
private int activeBorderColor = Color.GREENYELLOW;
private int borderColor = inactiveBorderColor;
private int backgroundColor = Color.WHITE;
private int arcWidth;
private VerticalFieldManager vfm ;
private EditField editField;
int nn =1;
int count=0;
public BasicField(int width, int height, long style, final String hint,String type_horizontal_vertical)
{
super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
if(type_horizontal_vertical.equalsIgnoreCase("Horizontal"))
vfm = new VerticalFieldManager(HORIZONTAL_SCROLL | USE_ALL_WIDTH );
if(type_horizontal_vertical.equalsIgnoreCase("Vertical"))
vfm = new VerticalFieldManager(VERTICAL_SCROLL| USE_ALL_WIDTH | USE_ALL_HEIGHT);
managerWidth = width;
managerHeight = height;
long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
if (innerStyle == 0)
{
innerStyle = FOCUSABLE;
}
if(style==EditField.FILTER_EMAIL)
{
innerStyle = EditField.FILTER_EMAIL;
}
if(style==EditField.FILTER_INTEGER)
{
innerStyle = EditField.FILTER_INTEGER;
}
editField = new EditField("", null, EditField.DEFAULT_MAXCHARS, innerStyle)
{
public void paint(Graphics g)
{
if(editField.getText().length()==0)
{
g.setColor(Color.GRAY);
g.drawText(hint,5,2);
super.paint(g);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; // make it even
add(vfm);
vfm.add(editField);
}
public void setFont(Font font)
{
super.setFont(font);
editField.setFont(font);
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
updateLayout();
}
public void setBorderColors(int inactive, int active)
{
inactiveBorderColor = inactive;
activeBorderColor = active;
invalidate();
}
public void setBackgroundColor(int bgColor)
{
backgroundColor = Color.AQUA;
invalidate();
}
public String getText()
{
return editField.getText();
}
public void setText(String newText)
{
editField.setText(newText);
}
public int getPreferredWidth()
{
return managerWidth;
}
public int getPreferredHeight()
{
return managerHeight;
}
protected void sublayout(int w, int h)
{
if (managerWidth == 0)
{
managerWidth = w;
}
if (managerHeight == 0)
{
managerHeight = h;
}
int actWidth = Math.min(managerWidth, w);
int actHeight = Math.min(managerHeight, h);
layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth);
setPositionChild(vfm, arcWidth / 2, arcWidth / 2);
setExtent(actWidth, actHeight);
}
protected void paint(Graphics g)
{
int prevColor = g.getColor();
int myWidth = managerWidth;
int myHeight = managerHeight;
g.setColor(backgroundColor);
g.fillRoundRect(0, 0, myWidth, myHeight, arcWidth, arcWidth);
g.setColor(borderColor);
g.drawRoundRect(0, 0, myWidth, myHeight, 10,10);
g.drawRoundRect(1, 1, myWidth - 2, myHeight - 2, 10-2, 10-2);
g.setColor(prevColor);
super.paint(g);
}
private void adjustBorderColor()
{
int nextColor;
if (editField.isFocus())
{
nextColor = activeBorderColor;
}
else
{
nextColor = inactiveBorderColor;
}
if (borderColor != nextColor)
{
borderColor = nextColor;
invalidate();
}
}
}

How can I display scroll text like marque in Blackberry using J2ME?

How can I display scroll like marquee text in Blackberry using J2ME? That moves from left to right or vertically?
Any help will be very much appreciated.
its really easy to display.
check the code below .
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class MarqueeApp extends UiApplication {
public MarqueeApp() {
pushScreen(new MarqueeScreen());
}
public static void main(String[] args) {
(new MarqueeApp()).enterEventDispatcher();
}
}
class MarqueeScreen extends MainScreen {
public MarqueeScreen() {
setTitle(new LabelField("hi"));
MarqueeLabel testLabel1 = new MarqueeLabel("This is a marquee",
Field.FOCUSABLE);
add(testLabel1);
MarqueeLabel testLabel2 = new MarqueeLabel("This is a long long " +
"long long long long long long long long long long long " +
"long long marquee", Field.FOCUSABLE);
add(testLabel2);
}
}
class MarqueeLabel extends LabelField {
int currentChar = 0;
String currentText = null;
Font ourFont;
private Timer _scrollTimer;
private TimerTask _scrollTimerTask;
public MarqueeLabel(String text, long style) {
super(text, style);
}
public void paint(Graphics graphics) {
currentText = this.getText();
if (currentChar < currentText.length()) {
currentText = currentText.substring(currentChar);
}
graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, 200);
super.paint(graphics);
}
public void layout(int width, int height) {
ourFont = this.getFont();
setExtent(500, ourFont.getHeight());
}
protected void onDisplay() {
startScroll();
}
protected void onUnfocus() {
startScroll();
}
private void startScroll() {
// Start scrolling
final String fullText = this.getText();
if (_scrollTimer == null) {
_scrollTimer = new Timer();
_scrollTimerTask = new TimerTask() {
public void run() {
currentChar = currentChar + 4;
if (currentChar > fullText.length()) {
currentChar = 0;
}
invalidate();
}
};
_scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 300);
}
}
protected void onFocus(int direction) {
if (_scrollTimer != null) {
_scrollTimerTask.cancel();
_scrollTimer.cancel();
_scrollTimer = null;
_scrollTimerTask = null;
}
}
protected boolean navigationMovement(int dx, int dy,
int status, int time) {
currentText = this.getText();
int oldCurrentChar = currentChar;
if (Math.abs(dx) > Math.abs(dy)) {
// horizontal scroll
if (dx > 0) {
currentChar = Math.min(currentText.length() - 1,
currentChar + 1);
} else if (dx < 0) {
currentChar = Math.max(0, currentChar - 1);
}
if (oldCurrentChar != currentChar) {
this.invalidate();
}
return true;
} else {
return super.navigationMovement(dx, dy, status, time);
}
}
}

Resources