Create a custom field
Task Steps
Create a custom field. You can only add custom context menu items and custom layouts to a custom field.
> Extend the Field class, or one of its subclasses, implementing the DrawStyle interface to specify the characteristics of the custom field and turn on drawing styles.
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; }
Implement constructors to define the label, shape, and style of the custom button.
public CustomButtonField(String label) {
public CustomButtonField(String label, int shape) {
public CustomButtonField(String label, long style) {
this(label, RECTANGLE, style); }
public CustomButtonField(String label, int shape, long style) { supe r(style); _label = label; _shape = shape; _font = getFont(); _labelHeight = _font.getHeight();
Define the label, shape, > and style of the custom button.
Specify the 1. Implement layout(). Arrange field data so that you perform the most complex calculations in layout()
arrangement of the instead of in paint().
°bjects in the field. 2. Within your implementation, perform the following actions:
• To calculate the available width and height, invoke Math.minO to return the smaller of the specified width and height and the preferred width and height of the field.
• To set the required dimensions for the field, invoke setExtent(int, int).
• Recalculate the pixel layout, cached fonts, and locale strings. protected void layout(int width, int height) { _font = getFont(); _labelHeight = _font.getHeight(); _labelWidth = _font.getAdvance(_label); width = Math.min( width, getPreferredWidth() ); height = Math.min( height, getPreferredHeight() );
The manager of the field invokes layout() to determine how the field arranges its contents in the available space. > Implement getPreferredWidth(), using the relative dimensions to make sure that the label does not exceed the dimensions of the component.
public int getPreferredWidth() { switch(_shape) { case TRIANGLE:
if (_labelWidth < _labelHeight) { return _labelHeight << 2; } else {
case OCTAGON:
if (_labelWidth < _labelHeight) { return _labelHeight + 4; } else {
case RECTANGLE: default:
Define the preferred width of a custom component.
Task
Define the preferred height of a custom component.
Steps
Implement getPreferredHeight(), using the relative dimensions of the field label to determine the preferred height.
public int getPreferredHeight() { switch(_shape) { case TRIANGLE:
if (_labelWidth < _labelHeight) { return _labelHeight << 1; } else {
return _labelWidth; }
case RECTANGLE:
return _labelHeight + 4;
case OCTAGON:
return getPreferredWidth(); }
Define the appearance of the custom field.
1. Perform complex calculations in layout() instead of in paint().
2. Implement paint().
protected void paint(Graphics graphics) {
int textX, textY, textWidth;
case TRIANGLE:
textWidth = Math.min(_labelWidth,h);
break;
case OCTAGON: int x = 5*w/17; int x2 = w-x-1; int x3 = w-1;
graph graph graph graph graph graph graph graph cs.drawL cs.drawL cs.drawL cs.drawL cs.drawL cs.drawL cs.drawL
ine(x3, x, ine(x, 0, ine(x, x3, ine(0, x, ine(0, x2, ine(x2, 0, ics.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()); textX = 4; textY = 2; textWidth = w - 6;
graphics.drawText(_label, textX, textY, (int)( getStyle() & DrawStyle.ELLIPSIS |
DrawStyle.HALIGN_MASK ), textWidth ); }
The fields manager invokes paint() to redraw the field whenever an area of the field is marked as invalid.
|
Paint a field only within > |
Invoke Graphics.getClippingRect(). |
|
the visible region. | |
|
Manage focus events. > |
Use the Field.FOCUSABLE style and implement Field.moveFocus(). |
|
Change the appearance > |
Override drawFocus (). |
|
of the default focus | |
|
indicator. |
Add component > Implement the Field set() and get() methods. capabilities. public String getLabel() { public int getShape() { public void setLabel(String label) { _label = label; _labelWidth = _font.getAdvance(_label); updateLayout(); } public void setShape(int shape) { _shape = shape; updateLayout(); } |
Post a comment