|
JavaTM 2 Platform Std. Ed. v1.3 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.text.JTextComponent
JTextComponent is the base class for swing text components. It tries to be compatible with the java.awt.TextComponent class where it can reasonably do so. Also provided are other services for additional flexibility (beyond the pluggable UI and bean support). You can find information on how to use the functionality this class provides in General Rules for Using Text Components, a section in The Java Tutorial.
getActions()
method. These actions
can be bound to key events, fired from buttons, etc.
A Keymap
lets an application bind key strokes to actions.
In order to allow keymaps to be shared across multiple text components, they
can use actions that extend TextAction. TextAction can determine which
JTextComponent most recently has or had focus and therefore is the subject of
the action (In the case that the ActionEvent sent to the action doesn't contain
the target text component as its source).
The input method framework lets
text components interact with input methods, separate software components that
preprocess events to let users enter thousands of different characters using
keyboards with far fewer keys. JTextComponent is an active client of
the framework, so it implements the preferred user interface for interacting
with input methods. As a consequence, some key events do not reach the text
component because they are handled by an input method, and some text input
reaches the text component as committed text within an InputMethodEvent
instead of as a key event. The complete text
input is the combination of the characters in keyTyped key events and committed
text in input method events.
The AWT listener model lets applications attach event listeners to components in order to bind events to actions. Swing encourages the use of keymaps instead of listeners, but maintains compatibility with listeners by giving the listeners a chance to steal an event by consuming it.
Keyboard event and input method events are handled in the following stages, with each stage capable of consuming the event:
Stage | KeyEvent | InputMethodEvent |
1. | input methods | (generated here) |
2. | focus manager | |
3. | registered key listeners | registered input method listeners |
4. | input method handling in JTextComponent | |
5. | keymap handling using the current keymap | |
6. | keyboard handling in JComponent (e.g. accelerators, component navigation, etc.) |
To maintain compatibility with applications that listen to key events but are not aware of input method events, the input method handling in stage 4 provides a compatibility mode for components that do not process input method events. For these components, the committed text is converted to keyTyped key events and processed in the key event pipeline starting at stage 3 instead of in the input method event pipeline.
By default the component will create a keymap (named DEFAULT_KEYMAP) that is shared by all JTextComponent instances as the default keymap. Typically a look-and-feel implementation will install a different keymap that resolves to the default keymap for those bindings not found in the different keymap. The minimal bindings include:
The model is defined by the Document
interface.
This is intended to provide a flexible text storage mechanism
that tracks change during edits and can be extended to more sophisticated
models. The model interfaces are meant to capture the capabilities of
expression given by SGML, a system used to express a wide variety of
content.
Each modification to the document causes notification of the
details of the change to be sent to all observers in the form of a
DocumentEvent
which allows the views to stay up to date with the model.
This event is sent to observers that have implemented the
DocumentListener
interface and registered interest with the model being observed.
modelToView(int)
and viewToModel(java.awt.Point)
for determining this information.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. A future release of Swing will provide support for long term persistence.
Document
,
DocumentEvent
,
DocumentListener
,
Caret
,
CaretEvent
,
CaretListener
,
TextUI
,
View
,
/
, Serialized FormInner Class Summary | |
class |
JTextComponent.AccessibleJTextComponent
This class implements accessibility support for the JTextComponent class. |
static class |
JTextComponent.KeyBinding
Binding record for creating key bindings. |
Inner classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent |
Inner classes inherited from class java.awt.Container |
Container.AccessibleAWTContainer |
Inner classes inherited from class java.awt.Component |
Component.AccessibleAWTComponent |
Field Summary | |
static String |
DEFAULT_KEYMAP
This is the name of the default keymap that will be shared by all JTextComponent instances unless they have had a different keymap set. |
static String |
FOCUS_ACCELERATOR_KEY
The bound property name for the focus accelerator. |
Fields inherited from class javax.swing.JComponent |
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
JTextComponent()
Creates a new JTextComponent. |
Method Summary | |
void |
addCaretListener(CaretListener listener)
Adds a caret listener for notification of any changes to the caret. |
void |
addInputMethodListener(InputMethodListener l)
Adds the specified input method listener to receive input method events from this component. |
static Keymap |
addKeymap(String nm,
Keymap parent)
Adds a new keymap into the keymap hierarchy. |
void |
copy()
Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. |
void |
cut()
Transfers the currently selected range in the associated text model to the system clipboard, removing the contents from the model. |
protected void |
fireCaretUpdate(CaretEvent e)
Notifies all listeners that have registered interest for notification on this event type. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JTextComponent. |
Action[] |
getActions()
Fetches the command list for the editor. |
Caret |
getCaret()
Fetches the caret that allows text-oriented navigation over the view. |
Color |
getCaretColor()
Fetches the current color used to render the caret. |
int |
getCaretPosition()
Returns the position of the text insertion caret for the text component. |
Color |
getDisabledTextColor()
Fetches the current color used to render the selected text. |
Document |
getDocument()
Fetches the model associated with the editor. |
char |
getFocusAccelerator()
Returns the key accelerator that will cause the receiving text component to get the focus. |
Highlighter |
getHighlighter()
Fetches the object responsible for making highlights. |
InputMethodRequests |
getInputMethodRequests()
Gets the input method request handler which supports requests from input methods for this component. |
Keymap |
getKeymap()
Fetches the keymap currently active in this text component. |
static Keymap |
getKeymap(String nm)
Fetches a named keymap previously added to the document. |
Insets |
getMargin()
Returns the margin between the text component's border and its text. |
Dimension |
getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for a view component. |
int |
getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation. |
boolean |
getScrollableTracksViewportHeight()
Return true if a viewport should always force the height of this Scrollable to match the height of the viewport. |
boolean |
getScrollableTracksViewportWidth()
Return true if a viewport should always force the width of this Scrollable to match the width of the viewport. |
int |
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. |
String |
getSelectedText()
Returns the selected text contained in this TextComponent. |
Color |
getSelectedTextColor()
Fetches the current color used to render the selected text. |
Color |
getSelectionColor()
Fetches the current color used to render the selection. |
int |
getSelectionEnd()
Returns the selected text's end position. |
int |
getSelectionStart()
Returns the selected text's start position. |
String |
getText()
Returns the text contained in this TextComponent. |
String |
getText(int offs,
int len)
Fetches a portion of the text represented by the component. |
TextUI |
getUI()
Fetches the user-interface factory for this text-oriented editor. |
boolean |
isEditable()
Returns the boolean indicating whether this TextComponent is editable or not. |
boolean |
isFocusTraversable()
Returns true if the focus can be traversed. |
static void |
loadKeymap(Keymap map,
JTextComponent.KeyBinding[] bindings,
Action[] actions)
Loads a keymap with a bunch of bindings. |
Rectangle |
modelToView(int pos)
Converts the given location in the model to a place in the view coordinate system. |
void |
moveCaretPosition(int pos)
Moves the caret to a new position, leaving behind a mark defined by the last time setCaretPosition was called. |
protected String |
paramString()
Returns a string representation of this JTextComponent. |
void |
paste()
Transfers the contents of the system clipboard into the associated text model. |
protected void |
processInputMethodEvent(InputMethodEvent e)
Processes input method events occurring on this component by dispatching them to any registered InputMethodListener objects. |
void |
read(Reader in,
Object desc)
Initializes from a stream. |
void |
removeCaretListener(CaretListener listener)
Removes a caret listener. |
static Keymap |
removeKeymap(String nm)
Removes a named keymap previously added to the document. |
void |
removeNotify()
Notifies this component that it has been removed from its container. |
void |
replaceSelection(String content)
Replaces the currently selected content with new content represented by the given string. |
void |
select(int selectionStart,
int selectionEnd)
Selects the text found between the specified start and end locations. |
void |
selectAll()
Selects all the text in the TextComponent. |
void |
setCaret(Caret c)
Sets the caret to be used. |
void |
setCaretColor(Color c)
Sets the current color used to render the caret. |
void |
setCaretPosition(int position)
Sets the position of the text insertion caret for the TextComponent. |
void |
setDisabledTextColor(Color c)
Sets the current color used to render the disabled text. |
void |
setDocument(Document doc)
Associates the editor with a text document. |
void |
setEditable(boolean b)
Sets the specified boolean to indicate whether or not this TextComponent should be editable. |
void |
setFocusAccelerator(char aKey)
Sets the key accelerator that will cause the receiving text component to get the focus. |
void |
setHighlighter(Highlighter h)
Sets the highlighter to be used. |
void |
setKeymap(Keymap map)
Sets the keymap to use for binding events to actions. |
void |
setMargin(Insets m)
Sets margin space between the text component's border and its text. |
void |
setSelectedTextColor(Color c)
Sets the current color used to render the selected text. |
void |
setSelectionColor(Color c)
Sets the current color used to render the selection. |
void |
setSelectionEnd(int selectionEnd)
Sets the selection end to the specified position. |
void |
setSelectionStart(int selectionStart)
Sets the selection start to the specified position. |
void |
setText(String t)
Sets the text of this TextComponent to the specified text. |
void |
setUI(TextUI ui)
Sets the user-interface factory for this text-oriented editor |
void |
updateUI()
Reloads the pluggable UI. |
int |
viewToModel(Point pt)
Converts the given place in the view coordinate system to the nearest representative location in the model. |
void |
write(Writer out)
Stores the contents of the model into the given stream. |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final String FOCUS_ACCELERATOR_KEY
public static final String DEFAULT_KEYMAP
Constructor Detail |
public JTextComponent()
Method Detail |
public TextUI getUI()
public void setUI(TextUI ui)
ui
- the factorypublic void updateUI()
updateUI
in class JComponent
javax.swing.JComponent
JComponent.setUI(javax.swing.plaf.ComponentUI)
,
UIManager.getLookAndFeel()
,
UIManager.getUI(javax.swing.JComponent)
public void addCaretListener(CaretListener listener)
listener
- the listenerCaretEvent
public void removeCaretListener(CaretListener listener)
listener
- the listenerCaretEvent
protected void fireCaretUpdate(CaretEvent e)
e
- the eventEventListenerList
public void setDocument(Document doc)
doc
- the document to display/editgetDocument()
public Document getDocument()
public Action[] getActions()
public void setMargin(Insets m)
m
- the space between the border and the textpublic Insets getMargin()
public Caret getCaret()
public void setCaret(Caret c)
c
- the caretgetCaret()
public Highlighter getHighlighter()
public void setHighlighter(Highlighter h)
h
- the highlightergetHighlighter()
public void setKeymap(Keymap map)
map
- the keymapgetKeymap()
public Keymap getKeymap()
public static Keymap addKeymap(String nm, Keymap parent)
nm
- the name of the keymap (must be unique within the
collection of named keymaps in the document). The name may
be null if the keymap is unnamed, but the caller is responsible
for managing the reference returned as an unnamed keymap can't
be fetched by name.parent
- the parent keymap. This may be null if unspecified
bindings need not be resolved in some other keymap.public static Keymap removeKeymap(String nm)
nm
- the name of the keymap to removepublic static Keymap getKeymap(String nm)
nm
- the name of the keymappublic static void loadKeymap(Keymap map, JTextComponent.KeyBinding[] bindings, Action[] actions)
Loads a keymap with a bunch of bindings. This can be used to take a static table of definitions and load them into some keymap. The following example illustrates an example of binding some keys to the cut, copy, and paste actions associated with a JTextComponent. A code fragment to accomplish this might look as follows:
static final JTextComponent.KeyBinding[] defaultBindings = {
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
DefaultEditorKit.copyAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
DefaultEditorKit.pasteAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
DefaultEditorKit.cutAction),
};
JTextComponent c = new JTextPane();
Keymap k = c.getKeymap();
JTextComponent.loadKeymap(k, defaultBindings, c.getActions());
The sets of bindings and actions may be empty but must be non-null.map
- the keymapbindings
- the bindingsactions
- the set of actionspublic Color getCaretColor()
public void setCaretColor(Color c)
c
- the colorgetCaretColor()
public Color getSelectionColor()
public void setSelectionColor(Color c)
c
- the colorgetSelectionColor()
public Color getSelectedTextColor()
public void setSelectedTextColor(Color c)
c
- the colorgetSelectedTextColor()
public Color getDisabledTextColor()
public void setDisabledTextColor(Color c)
c
- the colorgetDisabledTextColor()
public void replaceSelection(String content)
This is the method that is used by the default implementation of the action for inserting content that gets bound to the keymap actions.
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
content
- the content to replace the selection withpublic String getText(int offs, int len) throws BadLocationException
offs
- the offset >= 0len
- the length >= 0BadLocationException
- if the offset or length are invalidpublic Rectangle modelToView(int pos) throws BadLocationException
pos
- the position >= 0BadLocationException
- if the given position does not
represent a valid location in the associated documentTextUI.modelToView(javax.swing.text.JTextComponent, int)
public int viewToModel(Point pt)
pt
- the location in the view to translateTextUI.viewToModel(javax.swing.text.JTextComponent, java.awt.Point)
public void cut()
public void copy()
public void paste()
replaceSelection(java.lang.String)
public void moveCaretPosition(int pos)
pos
- the positionsetCaretPosition(int)
public void setFocusAccelerator(char aKey)
aKey
- the keygetFocusAccelerator()
public char getFocusAccelerator()
public void read(Reader in, Object desc) throws IOException
in
- The stream to read fromdesc
- An object describing the stream. This
might be a string, a File, a URL, etc. Some kinds
of documents (such as html for example) might be
able to make use of this information. If non-null, it is
added as a property of the document.IOException
- as thrown by the stream being
used to initialize.EditorKit.createDefaultDocument()
,
setDocument(javax.swing.text.Document)
,
PlainDocument
public void write(Writer out) throws IOException
out
- the output streamIOException
- on any I/O errorpublic void removeNotify()
removeNotify
in class JComponent
javax.swing.JComponent
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)
public boolean isFocusTraversable()
isFocusTraversable
in class JComponent
public void setCaretPosition(int position)
position
- the positionpublic int getCaretPosition()
public void setText(String t)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
t
- the new text to be setgetText(int, int)
public String getText()
setText(java.lang.String)
public String getSelectedText()
IllegalArgumentException
- if the selection doesn't
have a valid mapping into the document for some reasonsetText(java.lang.String)
public boolean isEditable()
setEditable(boolean)
public void setEditable(boolean b)
b
- the boolean to be setisEditable()
public int getSelectionStart()
public void setSelectionStart(int selectionStart)
This is available for backward compatiblitity to code that called this method on java.awt.TextComponent. This is implemented to forward to the Caret implementation which is where the actual selection is maintained.
selectionStart
- the start position of the text >= 0public int getSelectionEnd()
public void setSelectionEnd(int selectionEnd)
This is available for backward compatiblitity to code that called this method on java.awt.TextComponent. This is implemented to forward to the Caret implementation which is where the actual selection is maintained.
selectionEnd
- the end position of the text >= 0public void select(int selectionStart, int selectionEnd)
selectionStart
- the start position of the text >= 0selectionEnd
- the end position of the text >= 0setCaretPosition(int)
,
moveCaretPosition(int)
public void selectAll()
public Dimension getPreferredScrollableViewportSize()
getPreferredScrollableViewportSize
in interface Scrollable
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
The default implementation of this is to simply return 10% of the visible area. Subclasses are likely to be able to provide a much more reasonable value.
getScrollableUnitIncrement
in interface Scrollable
visibleRect
- The view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left, greater than
zero for down/right.IllegalArgumentException
- for an invalid orientationJScrollBar.setUnitIncrement(int)
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
The default implementation of this is to simply return the visible area. Subclasses will likely be able to provide a much more reasonable value.
getScrollableBlockIncrement
in interface Scrollable
visibleRect
- The view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left, greater than zero
for down/right.IllegalArgumentException
- for an invalid orientationJScrollBar.setBlockIncrement(int)
public boolean getScrollableTracksViewportWidth()
Scrolling containers, like JViewport, will use this method each time they are validated.
getScrollableTracksViewportWidth
in interface Scrollable
public boolean getScrollableTracksViewportHeight()
Scrolling containers, like JViewport, will use this method each time they are validated.
getScrollableTracksViewportHeight
in interface Scrollable
public AccessibleContext getAccessibleContext()
getAccessibleContext
in interface Accessible
getAccessibleContext
in class JComponent
protected String paramString()
null
.
Overriding paramString() to provide information about the specific new aspects of the JFC components.
paramString
in class JComponent
protected void processInputMethodEvent(InputMethodEvent e)
Component
InputMethodListener
objects.
This method is not called unless input method events are enabled for this component. Input method events are enabled when one of the following occurs:
InputMethodListener
object is registered
via addInputMethodListener
.
enableEvents
.
processInputMethodEvent
in class Component
java.awt.Component
e
- the input method eventInputMethodEvent
,
InputMethodListener
,
Component.addInputMethodListener(java.awt.event.InputMethodListener)
,
Component.enableEvents(long)
public InputMethodRequests getInputMethodRequests()
Component
getInputMethodRequests
in class Component
java.awt.Component
Component.addInputMethodListener(java.awt.event.InputMethodListener)
public void addInputMethodListener(InputMethodListener l)
Component
addInputMethodListener
in class Component
java.awt.Component
l
- the input method listener.InputMethodEvent
,
InputMethodListener
,
Component.removeInputMethodListener(java.awt.event.InputMethodListener)
,
Component.getInputMethodRequests()
|
JavaTM 2 Platform Std. Ed. v1.3 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-2000 Sun Microsystems, Inc. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All Rights Reserved.