mwt
Class Component

java.lang.Object
  extended by mwt.Component
Direct Known Subclasses:
Button, Label, Window

public class Component
extends java.lang.Object

A component is an object having a graphical representation that can be displayed on the screen (canvas) and can interact with the user.

Containers

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.

Position and Size

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.

Focus

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.

Events

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 Constants

This class contains aligns constant that can be used in different ways. For example, text alignment.
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

ALIGN_TOP_LEFT

public static final int ALIGN_TOP_LEFT
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_TOP_CENTER

public static final int ALIGN_TOP_CENTER
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_TOP_RIGHT

public static final int ALIGN_TOP_RIGHT
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_MIDDLE_LEFT

public static final int ALIGN_MIDDLE_LEFT
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_MIDDLE_CENTER

public static final int ALIGN_MIDDLE_CENTER
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_MIDDLE_RIGHT

public static final int ALIGN_MIDDLE_RIGHT
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_BOTTOM_LEFT

public static final int ALIGN_BOTTOM_LEFT
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_BOTTOM_CENTER

public static final int ALIGN_BOTTOM_CENTER
Constant for component/text alignment.

See Also:
Constant Field Values

ALIGN_BOTTOM_RIGHT

public static final int ALIGN_BOTTOM_RIGHT
Constant for component/text alignment.

See Also:
Constant Field Values
Constructor Detail

Component

public Component(int x,
                 int y,
                 int width,
                 int height,
                 boolean isContainer)
Creates a new component with the given position and size.
If the component should be a container, childs can be appended but it will cannot accept focus.

See Also:
Containers
Method Detail

getId

public final java.lang.String getId()
Gets the id.

Since:
1.2

setId

public final void setId(java.lang.String id)
Sets the id.

Since:
1.2

getX

public int getX()
Gets the x coordinate of this component within the parent.


setX

public void setX(int x)
Sets the x coordinate of this component within the parent.


getY

public int getY()
Gets the y coordinate of this component within the parent.


setY

public void setY(int y)
Sets the y coordinate of this component within the parent.


getWidth

public int getWidth()
Gets the width of this component, in pixels.


setWidth

public void setWidth(int width)
Sets the width of this component, in pixels.


getHeight

public int getHeight()
Gets the height of this component, in pixels.


setHeight

public void setHeight(int height)
Sets the height of this component, in pixels.


isVisible

public final boolean isVisible()
Checks if this component should be visible when its parent hierarchy is visible.

See Also:
isHierarchyVisible()

setVisible

public void setVisible(boolean visible)
Shows or hides this component. By default components are visible.


isEnabled

public final boolean isEnabled()
Checks if this component should be enabled when its parent hierarchy is enabled.

See Also:
isHierarchyEnabled()

setEnabled

public void setEnabled(boolean enabled)
Enables or disables this component. By default components are enabled.


isFocusable

public final boolean isFocusable()
Checks if this component is focusable.

See Also:
acceptsFocus()

setFocusable

public void setFocusable(boolean focusStop)
Enables or disables focusable.

See Also:
acceptsFocus()

isDoubleBuffered

public final boolean isDoubleBuffered()
Checks if this component should be double buffered.


setDoubleBuffered

public void setDoubleBuffered(boolean doubleBuffered)
Enables or disables the double buffer. By default components are not double buffered.


isHierarchyVisible

public final boolean isHierarchyVisible()
Checks if this component and its parent hierarchy is visible.

See Also:
isVisible()

isHierarchyEnabled

public final boolean isHierarchyEnabled()
Checks if this component and its parent hierarchy is enabled.

See Also:
isEnabled()

isContainer

public final boolean isContainer()
Checks if this component is a container.

See Also:
Containers

acceptsFocus

public final boolean acceptsFocus()
Checks if this component accepts focus.
A component accepts focus if:


add

public final void add(Component c)
Appends the given component as a child.
This method is the same as add(component,getComponentCount()). If isContainer() returns false, an exception may be thrown.

Parameters:
c - the component to be appended

add

public void add(Component c,
                int index)
Appends a child component at the given position.
The index must be a value greater than or equal to 0 and less than or equal to the current number of child components.
If isContainer() returns false, an exception may be thrown.

Parameters:
c - the component to be appended
index - where to insert the component

getChildCount

public final int getChildCount()
Gets the number of components currently added.

Returns:
the number of components currently added
Since:
1.2

getComponentCount

public final int getComponentCount()
Deprecated. use getChildCount() instead


getChild

public final Component getChild(int index)
Gets the component at the specified index.

Parameters:
index - The zero-based index of the component
Returns:
The component at the specified index
Since:
1.2

getComponent

public final Component getComponent(int index)
Deprecated. use getChild(int) instead


getChild

public final int getChild(Component c)
Gets the index of the specified component.

Parameters:
c - The component
Returns:
the index of the specified index, or -1 if it couldn't be found
Since:
1.2

getComponentIndex

public final int getComponentIndex(Component c)
Deprecated. use getChild(Component) instead


getChild

public final Component getChild(java.lang.String id,
                                boolean recursive)
Gets the child with the given id. If recursive is true, the search is performed beetween all childs.

Parameters:
id -
recursive -
Returns:
the component found or null
Since:
1.2

removeChild

public final void removeChild(int index)
Removes the component at the specified index.

Parameters:
index - The index of the component to remove
Since:
1.2

remove

public void remove(int index)
Deprecated. use removeChild(int) instead


getParent

public final Component getParent()
Gets the parent component container.


keyEvent

protected boolean keyEvent(long key,
                           Window window)
Processes a key event.

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);
 }
 

Parameters:
key - the key
window - the window caller
Returns:
true if the key was be consumed

focusGained

protected void focusGained()
Called when this component gains focus. By default, this method has an empty implementation.


focusLost

protected void focusLost()
Called when this component loses focus.
By default, this method has an empty implementation.


paint

protected void paint(javax.microedition.lcdui.Graphics g,
                     Window window)
Paints this component.

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.

Parameters:
g - the graphics to draw the component into
window - the window caller

paintChilds

protected final void paintChilds(javax.microedition.lcdui.Graphics g,
                                 Window window)
Paints the childs components into the given graphics object.
This method should be invoked from paint(Graphics, Window).



Copyright © 2007 MWT-Team. All Rights Reserved.