|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object mwt.Component mwt.Button
public class Button
A button is a component that can cause an action to happen when is pushed.
The action is determinated by the mwt.EventListener given during the button's creation:Button b = new Button(20, 4, 40, 30, "Press Me!", new EventListener() { public void processEvent(int eventType, Component c, Object[] args) { System.out.println("Pressed"); } }, 0);
This works just fine, however, the java compiler creates a new class for each event listener implementation. When using a lot of buttons, the jar size will increase significally.
A solution is to group buttons' actions in the same event listener implementation:class MyCanvas extends Canvas implements EventListener { Window win = ... static final int NEW_GAME = 0; static final int OPTIONS = 1; static final int EXIT = 2; MyCanvas() { win.add(new Button(4, 4, 30, 20, "New Game", this, NEW_GAME)); win.add(new Button(4,24, 30, 20, "Options", this, OPTION)); win.add(new Button(4,44, 30, 20, "Exit", this, EXIT)); } public void processEvent(int eventType, Component c, Object[] args) { switch(eventType) { case EVENT_ACTION: Button b = (Button) c; switch(c.getActionType()) { case NEW_GAME: System.out.println("New Game pressed: " + c); break; case OPTIONS: System.out.println("Options pressed: " + c); break; case EXIT: System.out.println("Exit pressed: " + c); break; } break; case EVENT_UNDEFINED: break; } } ... }
The component parameter is the button that is triggering the event, the args parameter is null.
See also mwt.EventListener.
A button's action is triggered when it has focus and its owner window
reports a Window.FOCUSACTION_FIRE
.
You can trigger the action programmatically also. See processAction()
.
A button has four different "styles", and for each one, a skin and a font.
When a button is created, a copy of the default skin and fonts is assigned.
You can set the default style using setDefaultSkin(int, Skin)
and
setDefaultFont(int, Font)
.
After the button is created, you can still change its style using
setSkin(int, Skin)
and setFont(int, Font)
.
The style value must be any of the STYLE_* constants:
STYLE_DEFAULT |
|
STYLE_FOCUSED |
|
STYLE_PRESSED |
|
STYLE_DISABLED |
Field Summary | |
---|---|
static int |
STYLE_DEFAULT
Constant value to get/set a skin/font. |
static int |
STYLE_DISABLED
Constant value to get/set a skin/font. |
static int |
STYLE_FOCUSED
Constant value to get/set a skin/font. |
static int |
STYLE_PRESSED
Constant value to get/set a skin/font. |
Fields inherited from class mwt.Component |
---|
ALIGN_BOTTOM_CENTER, ALIGN_BOTTOM_LEFT, ALIGN_BOTTOM_RIGHT, ALIGN_MIDDLE_CENTER, ALIGN_MIDDLE_LEFT, ALIGN_MIDDLE_RIGHT, ALIGN_TOP_CENTER, ALIGN_TOP_LEFT, ALIGN_TOP_RIGHT |
Constructor Summary | |
---|---|
Button(int x,
int y,
int width,
int height,
java.lang.String text,
EventListener action,
int actionType)
Creates a new button at the given position and size |
Method Summary | |
---|---|
void |
click()
Performs a "click" programmatically. |
int |
getActionType()
Gets the action type value given when this button was created. |
static Font |
getDefaultFont(int style)
Gets the default font for the given style. |
static Skin |
getDefaultSkin(int style)
Gets the default skin for the given style. |
Font |
getFont(int style)
Gets this button font for the given style. |
Skin |
getSkin(int style)
Gets this button skin for the given style. |
java.lang.String |
getText()
Gets this button's text. |
int |
getTextAlign()
Gets the text align. |
protected boolean |
keyEvent(long key,
Window window)
Processes a key event. |
protected void |
onClick()
Method permormed when the event listener is null. |
protected void |
paint(javax.microedition.lcdui.Graphics g,
Window window)
Paints this component. |
void |
processAction()
Deprecated. use click() instead |
static void |
setDefaultFont(int style,
Font font)
Sets the default font for the given style. |
static void |
setDefaultSkin(int style,
Skin skin)
Sets the default skin for the given style. |
void |
setFont(int style,
Font font)
Sets this button font for the given style. |
void |
setSkin(int style,
Skin skin)
Sets this button skin for the given style. |
void |
setText(java.lang.String text)
Sets this button's text. |
void |
setTextAlign(int textAlign)
Sets the text align. |
Methods inherited from class mwt.Component |
---|
acceptsFocus, add, add, focusGained, focusLost, getChild, getChild, getChild, getChildCount, getComponent, getComponentCount, getComponentIndex, getHeight, getId, getParent, getWidth, getX, getY, isContainer, isDoubleBuffered, isEnabled, isFocusable, isHierarchyEnabled, isHierarchyVisible, isVisible, paintChilds, remove, removeChild, setDoubleBuffered, setEnabled, setFocusable, setHeight, setId, setVisible, setWidth, setX, setY |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int STYLE_DEFAULT
public static final int STYLE_FOCUSED
public static final int STYLE_PRESSED
public static final int STYLE_DISABLED
Constructor Detail |
---|
public Button(int x, int y, int width, int height, java.lang.String text, EventListener action, int actionType)
Method Detail |
---|
public static final Skin getDefaultSkin(int style)
public static final void setDefaultSkin(int style, Skin skin)
public static final Font getDefaultFont(int style)
public static final void setDefaultFont(int style, Font font)
public Skin getSkin(int style)
public void setSkin(int style, Skin skin)
public Font getFont(int style)
public void setFont(int style, Font font)
public java.lang.String getText()
public void setText(java.lang.String text)
public int getTextAlign()
public void setTextAlign(int textAlign)
protected void onClick()
public final void click()
public final int getActionType()
public final void processAction()
click()
instead
protected boolean keyEvent(long key, Window window)
Component
The higher 32 bits of the key parameter determines the type of this event.
1 for key pressed events.
0 for key released events.
Otherwise is a key repeated event, in such case, the value represents how long the key code
was repeated. See also Keys.
The default implementation of this method returns false.
An implementation should return true if the key was consumed.
protected void processKey(long key, Window window) {
System.out.print("Keycode " + ((int) key));
switch(key >> 32) {
case 0: System.out.println(" released!"); break;
case 1: System.out.println(" pressed!"); break;
default: System.out.println(" repeated " + (key >> 32) + " times!"); break;
}
if(window.processInput(key) == Window.FOCUSACTION_FIRE
) ...
// let the window dispatch the key
if(...) window.dispatchKey(key);
}
keyEvent
in class Component
key
- the keywindow
- the window caller
protected void paint(javax.microedition.lcdui.Graphics g, Window window)
Component
The default implementation calls Component.paintChilds(Graphics, Window)
.
An implementation should call Component.paintChilds(Graphics, Window)
once (if the
component is a container).
The graphics object was previously transformed to the current x and y, and the clip was setted according to this component width and height.
paint
in class Component
g
- the graphics to draw the component intowindow
- the window caller
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |