: Class LWTextComponent (original) (raw)
java.lang.Object | +--java.awt.Component | +--LWTextComponent
Direct Known Subclasses:
public class LWTextComponent
extends Component
implements KeyListener, FocusListener
Implements a very simple lightweight text editing component. It lets the user edit a single line of text using the keyboard. The only special character that it knows about is backspace; all other characters are added to the text. Selections are not supported, so there's only a simple caret indicating the insertion point. The component also displays a component name above the editable text line, and draws a black frame whose thickness indicates whether the component has the focus.
The component can be initialized to enable or disable input through input methods. Other than that, it doesn't do anything to support input methods, so input method interaction (if any) will occur in a separate composition window. However, the component is designed to be easily extended with full input method support. It distinguishes between "displayed text" and "committed text" - here, they're the same, but in a subclass that supports on-the-spot input, the displayed text would be the combination of committed text and composed text. The component also uses TextLayout to draw the text, so it can be easily extended to handle input method highlights.
See Also:
Constructor Summary |
---|
[LWTextComponent](LWTextComponent.html#LWTextComponent%28java.lang.String, boolean%29)(String name, boolean enableInputMethods) Constructs a LWTextComponent. |
Method Summary | |
---|---|
void | focusGained(FocusEvent event) Turns on drawing of the component's thicker frame and the caret. |
void | focusLost(FocusEvent event) Turns off drawing of the component's thicker frame and the caret. |
TextHitInfo | getCaret() Returns a text hit info indicating the current caret (insertion point). |
Rectangle | getCaretRectangle() Returns a 0-width caret rectangle. |
Rectangle | getCaretRectangle(TextHitInfo caret) Returns a 0-width caret rectangle for the given text index. |
AttributedCharacterIterator | getCommittedText() Returns the text that the user has entered and committed. |
AttributedCharacterIterator | [getCommittedText](LWTextComponent.html#getCommittedText%28int, int%29)(int beginIndex, int endIndex) Returns a subrange of the text that the user has entered and committed. |
int | getCommittedTextLength() Returns the length of the text that the user has entered and committed. |
AttributedCharacterIterator | getDisplayText() Returns the text that the user has entered. |
TextLayout | getTextLayout() Returns a text layout for the text that the user has entered. |
Point | getTextOrigin() Returns the origin of the text. |
void | insertCharacter(char c) Inserts the given character at the end of the text. |
void | invalidateTextLayout() Invalidates the cached text layout. |
void | keyPressed(KeyEvent event) Ignores key pressed events. |
void | keyReleased(KeyEvent event) Ignores key released events. |
void | keyTyped(KeyEvent event) Handles the key typed event. |
void | paint(Graphics g) Draws the component. |
void | setFontSize(int size) |
Methods inherited from class java.awt.Component |
---|
[action](../../../api/java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, [addPropertyChangeListener](../../../api/java/awt/Component.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), bounds, [checkImage](../../../api/java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../../api/java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../../api/java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), [contains](../../../api/java/awt/Component.html#contains%28int, int%29), contains, createImage, [createImage](../../../api/java/awt/Component.html#createImage%28int, int%29), deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, [firePropertyChange](../../../api/java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, [getComponentAt](../../../api/java/awt/Component.html#getComponentAt%28int, int%29), getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, [gotFocus](../../../api/java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../../api/java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../../api/java/awt/Component.html#inside%28int, int%29), invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, [keyDown](../../../api/java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../../api/java/awt/Component.html#keyUp%28java.awt.Event, int%29), layout, list, list, [list](../../../api/java/awt/Component.html#list%28java.io.PrintStream, int%29), list, [list](../../../api/java/awt/Component.html#list%28java.io.PrintWriter, int%29), [locate](../../../api/java/awt/Component.html#locate%28int, int%29), location, [lostFocus](../../../api/java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), minimumSize, [mouseDown](../../../api/java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../../api/java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../../api/java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../../api/java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../../api/java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../../api/java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../../api/java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, paramString, postEvent, preferredSize, [prepareImage](../../../api/java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../../api/java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), print, printAll, processComponentEvent, processEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, [removePropertyChangeListener](../../../api/java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../../api/java/awt/Component.html#repaint%28int, int, int, int%29), repaint, [repaint](../../../api/java/awt/Component.html#repaint%28long, int, int, int, int%29), requestFocus, [reshape](../../../api/java/awt/Component.html#reshape%28int, int, int, int%29), resize, [resize](../../../api/java/awt/Component.html#resize%28int, int%29), setBackground, [setBounds](../../../api/java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, [setLocation](../../../api/java/awt/Component.html#setLocation%28int, int%29), setLocation, setName, setSize, [setSize](../../../api/java/awt/Component.html#setSize%28int, int%29), setVisible, show, show, size, toString, transferFocus, update, validate |
LWTextComponent
public LWTextComponent(String name, boolean enableInputMethods)
Constructs a LWTextComponent.
Parameters:
name
- the component name to be displayed above the text
enableInputMethods
- whether to enable input methods for this component
setFontSize
public void setFontSize(int size)
paint
public void paint(Graphics g)
Draws the component. The following items are drawn:
- the component's background
- a frame, thicker if the component has the focus
- the component name
- the text that the user has entered
- the caret, if the component has the focus
Overrides:
getCommittedText
public AttributedCharacterIterator getCommittedText()
Returns the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Returns:
an AttributedCharacterIterator for the text that the user has entered and committed
getCommittedText
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex)
Returns a subrange of the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Parameters:
beginIndex
- the index of the first character of the subrange
endIndex
- the index of the character following the subrange
Returns:
an AttributedCharacterIterator for a subrange of the text that the user has entered and committed
getCommittedTextLength
public int getCommittedTextLength()
Returns the length of the text that the user has entered and committed. Since this component does not support on-the-spot input, there's no composed text, so all text that has been entered is committed.
Returns:
the length of the text that the user has entered and committed
getDisplayText
public AttributedCharacterIterator getDisplayText()
Returns the text that the user has entered. As TextLayout requires a font to be defined for each character, the default font is applied to the entire text. A subclass that supports on-the-spot input must override this method to include composed text.
Returns:
the text that the user has entered
getTextLayout
public TextLayout getTextLayout()
Returns a text layout for the text that the user has entered. This text layout is created from the text returned by getDisplayText. The text layout is cached until invalidateTextLayout is called.
Returns:
a text layout for the text that the user has entered, or null
See Also:
invalidateTextLayout(), getDisplayText()
invalidateTextLayout
public void invalidateTextLayout()
Invalidates the cached text layout. This must be called whenever the component's text is modified.
See Also:
getTextOrigin
public Point getTextOrigin()
Returns the origin of the text. This is the leftmost point on the baseline of the text.
Returns:
the origin of the text
getCaretRectangle
public Rectangle getCaretRectangle()
Returns a 0-width caret rectangle. This rectangle is derived from the caret returned by getCaret. getCaretRectangle returns null iff getCaret does.
Returns:
the caret rectangle, or null
See Also:
getCaretRectangle
public Rectangle getCaretRectangle(TextHitInfo caret)
Returns a 0-width caret rectangle for the given text index. It is calculated based on the text layout returned by getTextLayout, so this method can be used for the entire displayed text.
Parameters:
caret
- the text index for which to calculate a caret rectangle
Returns:
the caret rectangle
getCaret
public TextHitInfo getCaret()
Returns a text hit info indicating the current caret (insertion point). This class always returns a caret at the end of the text that the user has entered. Subclasses may return a different caret or null.
Returns:
the caret, or null
insertCharacter
public void insertCharacter(char c)
Inserts the given character at the end of the text.
Parameters:
c
- the character to be inserted
keyTyped
public void keyTyped(KeyEvent event)
Handles the key typed event. If the character is backspace, the last character is removed from the text that the user has entered. Otherwise, the character is appended to the text. Then, the text is redrawn.
Specified by:
keyTyped in interface KeyListener
keyPressed
public void keyPressed(KeyEvent event)
Ignores key pressed events.
Specified by:
keyPressed in interface KeyListener
keyReleased
public void keyReleased(KeyEvent event)
Ignores key released events.
Specified by:
keyReleased in interface KeyListener
focusGained
public void focusGained(FocusEvent event)
Turns on drawing of the component's thicker frame and the caret.
Specified by:
focusGained in interface FocusListener
focusLost
public void focusLost(FocusEvent event)
Turns off drawing of the component's thicker frame and the caret.
Specified by:
focusLost in interface FocusListener
Java, Java 2D, and JDBC are trademarks or registered trademarks of Oracle and/or its affiliates. in the US and other countries.
Copyright 1993-1999 Oracle and/or its affiliates. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All rights reserved.