Code sample Creating custom buttons
Example: CustomButtonField.java
* CustomButtonField.java
* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved. */
package com.rim.samples.docs.custombuttons;
import net.rim.device.api.ui.*; import net.rim.device.api.system.*;
* CustomButtonField is a class that creates button fields of various
* shapes. This sample demonstrates how to create custom UI fields. */
public class CustomButtonField extends Field implements DrawStyle { public static final int RECTANGLE = 1; public static final int TRIANGLE = 2; public static final int OCTAGON = 3;
private String _label;
private int _shape;
private Font _font;
private int _labelHeight;
private int _labelWidth;
/* Constructs a button with specified label, and the default style and shape. */ public CustomButtonField(String label) { this(label, RECTANGLE, 0);
/* Constructs a button with specified label and shape, and the default style. */ public CustomButtonField(String label, int shape) { this(label, shape, 0);
/* Constructs a button with specified public CustomButtonField(String label this(label, RECTANGLE, style);
/* Constructs a button with specified public CustomButtonField(String label super(style); _label = label; _shape = shape; _font = getFont(); _labelHeight = _font.getHeight(); _labelWidth = _font.getAdvance(_label);
label and style, and the default shape. long style) {
label, shape, and style */ int shape, long style) {
/* Method that draws the focus indicator for this button and * inverts the inside region of the shape. */
protected void drawFocus(Graphics graphics, boolean on) { switch(_shape) { case TRIANGLE:
int w = getWidth(); int h = w >> 1; for (int i=h-1; i>=2; graphics.invert(i
break; case RECTANGLE:
graphics.invert(1, 1, break; case OCTAGON:
int x3 = getWidth(); int x = 5 * x3 / 17; int x2 = x3 - x; x3 = x3 - 1; x2 = x2 - 1; graphics.invert(1, x, getWidth() - 2, getHeight() - 2);
graphics.invert(1+i, x-i, getWidth() - ((i+1)<<1), 1); graphics.invert(1+i, x2+i, getWidth() - ((i+1)<<1), 1);
break;
/* Returns the label. */ public String getLabel() { return _label;
return _shape;
/* Sets the label. */ public void setLabel(String label) { _label = label;
_labelWidth = _font.getAdvance(_label); updateLayout();
/* Sets the shape. */ public void setShape(int shape) { _shape = shape; updateLayout();
/* Retrieves the preferred width of the button. */ public int getPreferredWidth() { switch(_shape) { case TRIANGLE:
return _labelWidth << 1;
case OCTAGON:
return _labelWidth + 8;
case RECTANGLE: default:
return _labelWidth + 8;
/* Retrieves the preferred height of the button. */ public int getPreferredHeight() { switch(_shape) { case TRIANGLE:
return _labelWidth;
case RECTANGLE:
return _labelHeight + 4; case OCTAGON:
return getPreferredWidth();
return 0;
/* Lays out this button's contents.
* This field's manager invokes this method during the layout
* process to instruct this field to arrange its contents, given an
protected void layout(int width, int height) {
// Update the cached font in case it has been changed.
_labelHeight = _font.getHeight();
_labelWidth = _font.getAdvance(_label);
// Calculate width.
width = Math.min( width, getPreferredWidth() ); // Calculate height.
height = Math.min( height, getPreferredHeight() ); // Set dimensions. setExtent( width, height );
* Redraws this button. The field's manager invokes this method during the
* repainting process to instruct this field to repaint itself.
protected void paint(Graphics graphics) { int textX, textY, textWidth; int w = getWidth(); switch(_shape) { case TRIANGLE:
int h = (w>>1); int m = (w>>1)-1; graphics.drawLine(0, h-1, m, 0); graphics.drawLine(m, 0, w-1, h-1); graphics.drawLine(0, h-1, w-1, h-1);
textWidth = Math.min(_labelWidth,h); textX = (w - textWidth) >> 1; textY = h >> 1; break; case OCTAGON:
graphics.drawLine(0, x, 0, x2); graphics.drawLine(x3, x, x3, x2); graphics.drawLine(x, 0, x2, 0); graphics.drawLine(x, x3, x2, x3); graphics.drawLine(0, x, x, 0); graphics.drawLine(0, x2, x, x3); graphics.drawLine(x2, 0, x3, x); graphics.drawLine(x2, x3, x3, x2); textWidth = Math.min(_labelWidth, w - 6); textX = (w-textWidth) >> 1; textY = (w-_labelHeight) >> 1; break;
case RECTANGLE: default:
graphics.drawRect(0, 0, w, getHeight());
break;
graphics.drawText(_label, textX, textY, (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ), textWidth );
Post a comment