|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object mwt.Component
public class Component
A component is an object having a graphical representation that can be displayed on the screen (canvas) and can interact with the user.
In MWT components can be containers, this means, that you could append child components.
This behavior is determined when the component is created. It cannot be changed later.
Examples of no-container components are buttons, checkboxes and labels. Examples of
container components are the panels and frames/forms.
Components added are tracked in a list. The order of the list will define the components'
front-to-back stacking order within the container component.
If no index is specified when adding a component to a container, it will be added to the
end of the list (and hence to the bottom of the stacking order).
Trying to append childs to a no-container component will throw an exception.
A component position (x and y coordinates) is relative to its parent container.
The size is defined by its width and height.
When a component is being painted, the Graphics
object is
translated and clipped in order to make it relative to component's position, and disable painting
outside its bounds.
Note: you can change the graphics clip if you want, using Graphics.setClip(int, int, int, int)
)
howewer, this trick is not MWT related.
A component accepts focus when is visible, enabled, focusable and is not a container.
Components are visible and enabled by default.
A component may be hidden (not visible) or disabled even if isVisible()
and/or
isEnabled()()
returns true. This is because the component's parent hierarchy
affects this properties. In other words, if a container is hidden or disabled, all
its child hierarchy is hidden and disabled too.
Use isHierarchyVisible()
and isHierarchyEnabled()
to know if a component and all
its hierarchy is visible and/or enabled.
The focusable property determines if a component should not accept focus regardless of its visible and enabled properties.
Note that if you switch any of these properties to false whenever a component or one of its childs has focus, you may want change its container window's focus "manually", otherwise it will remains focus until the next window key event.
Component's events can be handled overriding methods.
Appearance | paint(Graphics, Window) |
Behavior | add(Component, int) |
remove(int) | |
Focus | focusGained() |
focusLost() | |
Key | keyEvent(long, Window) |
Property Changed | setX(int) |
setY(int) | |
setWidth(int) | |
setHeight(int) | |
setVisible(boolean) | |
setEnabled(boolean) | |
setFocusable(boolean) | |
setDoubleBuffered(boolean) | |
Getters | getX() |
getY() | |
getWidth() | |
getHeight() |
Normally the appearance and key methods are overriden. This way, you can handle the component's input and output.
The focus methods can be used in different ways. For example, you can validate a component when it loses focus, if the validation fails you can set the focus back.
Avoid overriding setters (property changed methods) and getters unless is absolutely necessary.
Otherwise the java virtual machine will call this methods indirectly via a virtual table,
instead of calling them directly (which -may- impact performance significally).
ALIGN_TOP_LEFT |
|
ALIGN_TOP_CENTER |
|
ALIGN_TOP_RIGHT |
|
ALIGN_MIDDLE_LEFT |
|
ALIGN_MIDDLE_CENTER |
|
ALIGN_MIDDLE_RIGHT |
|
ALIGN_BOTTOM_LEFT |
|
ALIGN_BOTTOM_CENTER |
|
ALIGN_BOTTOM_RIGHT |
Field Summary | |
---|---|
static int |
ALIGN_BOTTOM_CENTER
Constant for component/text alignment. |
static int |
ALIGN_BOTTOM_LEFT
Constant for component/text alignment. |
static int |
ALIGN_BOTTOM_RIGHT
Constant for component/text alignment. |
static int |
ALIGN_MIDDLE_CENTER
Constant for component/text alignment. |
static int |
ALIGN_MIDDLE_LEFT
Constant for component/text alignment. |
static int |
ALIGN_MIDDLE_RIGHT
Constant for component/text alignment. |
static int |
ALIGN_TOP_CENTER
Constant for component/text alignment. |
static int |
ALIGN_TOP_LEFT
Constant for component/text alignment. |
static int |
ALIGN_TOP_RIGHT
Constant for component/text alignment. |
Constructor Summary | |
---|---|
Component(int x,
int y,
int width,
int height,
boolean isContainer)
Creates a new component with the given position and size. |
Method Summary | |
---|---|
boolean |
acceptsFocus()
Checks if this component accepts focus. |
void |
add(Component c)
Appends the given component as a child. |
void |
add(Component c,
int index)
Appends a child component at the given position. |
protected void |
focusGained()
Called when this component gains focus. |
protected void |
focusLost()
Called when this component loses focus. |
int |
getChild(Component c)
Gets the index of the specified component. |
Component |
getChild(int index)
Gets the component at the specified index. |
Component |
getChild(java.lang.String id,
boolean recursive)
Gets the child with the given id. |
int |
getChildCount()
Gets the number of components currently added. |
Component |
getComponent(int index)
Deprecated. use getChild(int) instead |
int |
getComponentCount()
Deprecated. use getChildCount() instead |
int |
getComponentIndex(Component c)
Deprecated. use getChild(Component) instead |
int |
getHeight()
Gets the height of this component, in pixels. |
java.lang.String |
getId()
Gets the id. |
Component |
getParent()
Gets the parent component container. |
int |
getWidth()
Gets the width of this component, in pixels. |
int |
getX()
Gets the x coordinate of this component within the parent. |
int |
getY()
Gets the y coordinate of this component within the parent. |
boolean |
isContainer()
Checks if this component is a container. |
boolean |
isDoubleBuffered()
Checks if this component should be double buffered. |
boolean |
isEnabled()
Checks if this component should be enabled when its parent hierarchy is enabled. |
boolean |
isFocusable()
Checks if this component is focusable. |
boolean |
isHierarchyEnabled()
Checks if this component and its parent hierarchy is enabled. |
boolean |
isHierarchyVisible()
Checks if this component and its parent hierarchy is visible. |
boolean |
isVisible()
Checks if this component should be visible when its parent hierarchy is visible. |
protected boolean |
keyEvent(long key,
Window window)
Processes a key event. |
protected void |
paint(javax.microedition.lcdui.Graphics g,
Window window)
Paints this component. |
protected void |
paintChilds(javax.microedition.lcdui.Graphics g,
Window window)
Paints the childs components into the given graphics object. |
void |
remove(int index)
Deprecated. use removeChild(int) instead |
void |
removeChild(int index)
Removes the component at the specified index. |
void |
setDoubleBuffered(boolean doubleBuffered)
Enables or disables the double buffer. |
void |
setEnabled(boolean enabled)
Enables or disables this component. |
void |
setFocusable(boolean focusStop)
Enables or disables focusable. |
void |
setHeight(int height)
Sets the height of this component, in pixels. |
void |
setId(java.lang.String id)
Sets the id. |
void |
setVisible(boolean visible)
Shows or hides this component. |
void |
setWidth(int width)
Sets the width of this component, in pixels. |
void |
setX(int x)
Sets the x coordinate of this component within the parent. |
void |
setY(int y)
Sets the y coordinate of this component within the parent. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int ALIGN_TOP_LEFT
public static final int ALIGN_TOP_CENTER
public static final int ALIGN_TOP_RIGHT
public static final int ALIGN_MIDDLE_LEFT
public static final int ALIGN_MIDDLE_CENTER
public static final int ALIGN_MIDDLE_RIGHT
public static final int ALIGN_BOTTOM_LEFT
public static final int ALIGN_BOTTOM_CENTER
public static final int ALIGN_BOTTOM_RIGHT
Constructor Detail |
---|
public Component(int x, int y, int width, int height, boolean isContainer)
Method Detail |
---|
public final java.lang.String getId()
public final void setId(java.lang.String id)
public int getX()
public void setX(int x)
public int getY()
public void setY(int y)
public int getWidth()
public void setWidth(int width)
public int getHeight()
public void setHeight(int height)
public final boolean isVisible()
isHierarchyVisible()
public void setVisible(boolean visible)
public final boolean isEnabled()
isHierarchyEnabled()
public void setEnabled(boolean enabled)
public final boolean isFocusable()
acceptsFocus()
public void setFocusable(boolean focusStop)
acceptsFocus()
public final boolean isDoubleBuffered()
public void setDoubleBuffered(boolean doubleBuffered)
public final boolean isHierarchyVisible()
isVisible()
public final boolean isHierarchyEnabled()
isEnabled()
public final boolean isContainer()
public final boolean acceptsFocus()
isHierarchyVisible()
returns trueisHierarchyEnabled()
returns trueisContainer()
returns falseisFocusable()()
returns true
public final void add(Component c)
add(component,getComponentCount())
.
If isContainer()
returns false, an exception may be thrown.
c
- the component to be appendedpublic void add(Component c, int index)
isContainer()
returns false, an exception may be thrown.
c
- the component to be appendedindex
- where to insert the componentpublic final int getChildCount()
public final int getComponentCount()
getChildCount()
instead
public final Component getChild(int index)
index
- The zero-based index of the component
public final Component getComponent(int index)
getChild(int)
instead
public final int getChild(Component c)
c
- The component
public final int getComponentIndex(Component c)
getChild(Component)
instead
public final Component getChild(java.lang.String id, boolean recursive)
id
- recursive
-
public final void removeChild(int index)
index
- The index of the component to removepublic void remove(int index)
removeChild(int)
instead
public final Component getParent()
protected boolean keyEvent(long key, Window window)
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);
}
key
- the keywindow
- the window caller
protected void focusGained()
protected void focusLost()
protected void paint(javax.microedition.lcdui.Graphics g, Window window)
The default implementation calls paintChilds(Graphics, Window)
.
An implementation should call 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.
g
- the graphics to draw the component intowindow
- the window callerprotected final void paintChilds(javax.microedition.lcdui.Graphics g, Window window)
paint(Graphics, Window)
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |