Overview | Package | Class | Tree | Deprecated | Index | Help |
Java Platform 1.1.7 |
||
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 | +--com.sun.java.swing.JComponent | +--com.sun.java.swing.text.JTextComponent
Text components provide a number of commands that can be used to manipulate the component. This is essentially the way that the component expresses its capabilities. These are expressed in terms of the swing Action interface, using the TextAction implementation. The set of commands supported by the text component can be found with the getActions method. These actions can be bound to key events, fired from buttons, etc.
To facilitate flexible use of the keyboard, support for creating keymaps and binding various keystrokes to some kind of action is provided. 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 use of the keymap is encouraged, but backward compatibilty with the awt mechanism is provided by giving the listeners a chance to steal the event by consuming it. Keyboard event distribution is handled in the following order, with each distribution capable of consuming the event.
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 text components have a model-view split. A text component pulls together the objects used to represent the model, view, and controller. The text document model may be shared by other views which act as observers of the model (e.g. a document may be shared by multiple components).
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.
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 Swing1.0 applications. It will not be possible to load serialized Swing1.0 objects with future releases of Swing. The JDK1.2 release of Swing will be the compatibility baseline for the serialized form of Swing objects.
Inner Class Summary | |
JTextComponent.AccessibleJTextComponent
Accessibility implementation for JTextComponent. |
|
static | JTextComponent.KeyBinding
Binding record for creating key bindings. |
Inner classes inherited from class com.sun.java.swing.JComponent | |
JComponent.AccessibleJComponent |
Field Summary | |
static java.lang.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 java.lang.String | FOCUS_ACCELERATOR_KEY
The bound property name for the focus accelerator. |
Fields inherited from class com.sun.java.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 |
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. |
static Keymap | addKeymap(java.lang.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. |
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 JComponent. |
Action[] | getActions()
Fetches the command list for the editor. |
java.awt.Color | getCaretColor()
Fetches the current color used to render the caret. |
Caret | getCaret()
Fetches the caret that allows text-oriented navigation over the view. |
int | getCaretPosition()
Returns the position of the text insertion caret for the text component. |
java.awt.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. |
Keymap | getKeymap()
Fetches the keymap currently active in this text component. |
static Keymap | getKeymap(java.lang.String nm)
Fetches a named keymap previously added to the document. |
java.awt.Insets | getMargin()
Returns the margin between the text component's border and its text. |
java.awt.Dimension | getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for a view component. |
int | getScrollableBlockIncrement(java.awt.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(java.awt.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. |
java.awt.Color | getSelectedTextColor()
Fetches the current color used to render the selected text. |
java.lang.String | getSelectedText()
Returns the selected text contained in this TextComponent. |
java.awt.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. |
java.lang.String | getText(int offs,
int len)
Fetches a portion of the text represented by the component. |
java.lang.String | getText()
Returns the text contained in this TextComponent. |
com.sun.java.swing.plaf.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. |
boolean | isOpaque()
Returns true if this component is completely opaque. |
static void | loadKeymap(Keymap map,
JTextComponent.KeyBinding[] bindings,
Action[] actions)
Loads a keymap with a bunch of bindings. |
java.awt.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. |
void | paste()
Transfers the contents of the system clipboard into the associated text model. |
void | processComponentKeyEvent(java.awt.event.KeyEvent e)
Processes any key events that the component itself recognizes. |
void | read(java.io.Reader in,
java.lang.Object desc)
Initializes from a stream. |
void | removeCaretListener(CaretListener listener)
Removes a caret listener. |
static Keymap | removeKeymap(java.lang.String nm)
Removes a named keymap previously added to the document. |
void | replaceSelection(java.lang.String content)
Replaces the currently selected content with new content represented by the given string. |
void | selectAll()
Selects all the text in the TextComponent. |
void | select(int selectionStart,
int selectionEnd)
Selects the text found between the specified start and end locations. |
void | setCaretColor(java.awt.Color c)
Sets the current color used to render the caret. |
void | setCaret(Caret c)
Sets the caret to be used. |
void | setCaretPosition(int position)
Sets the position of the text insertion caret for the TextComponent. |
void | setDisabledTextColor(java.awt.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 | setEnabled(boolean b)
Enables or disables this component, depending on the value of the parameter b . |
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(java.awt.Insets m)
Sets margin space between the text component's border and its text. |
void | setOpaque(boolean o)
Sets whether or not the UI should render a background. |
void | setSelectedTextColor(java.awt.Color c)
Sets the current color used to render the selected text. |
void | setSelectionColor(java.awt.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(java.lang.String t)
Sets the text of this TextComponent to the specified text. |
void | setUI(com.sun.java.swing.plaf.TextUI ui)
Sets the user-interface factory for this text-oriented editor |
void | updateUI()
Reloads the pluggable UI. The key used to fetch the new interface is getUIClassID(). |
int | viewToModel(java.awt.Point pt)
Converts the given place in the view coordinate system to the nearest representative location in the model. |
void | write(java.io.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, addNotify, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponentAt, getComponentAt, getComponent, getComponentCount, getComponents, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, paint, paramString, preferredSize, printComponents, print, processContainerEvent, processEvent, removeAll, remove, remove, removeContainerListener, removeNotify, setLayout, update, validate, validateTree |
Methods inherited from class java.awt.Component | |
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, 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, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paint, paramString, postEvent, preferredSize, prepareImage, prepareImage, printAll, print, processComponentEvent, processEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate |
Methods inherited from class java.lang.Object | |
clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String FOCUS_ACCELERATOR_KEY
public static final java.lang.String DEFAULT_KEYMAP
Constructor Detail |
public JTextComponent()
Method Detail |
public com.sun.java.swing.plaf.TextUI getUI()
public void setUI(com.sun.java.swing.plaf.TextUI ui)
ui
- the factorypublic void updateUI()
public boolean isOpaque()
public void setOpaque(boolean o)
o
- true if should render a backgroundpublic void addCaretListener(CaretListener listener)
listener
- the listenerpublic void removeCaretListener(CaretListener listener)
listener
- the listenerprotected void fireCaretUpdate(CaretEvent e)
e
- the eventpublic void setDocument(Document doc)
doc
- the document to display/editpublic Document getDocument()
public Action[] getActions()
public void setMargin(java.awt.Insets m)
m
- the space between the border and the textpublic java.awt.Insets getMargin()
public Caret getCaret()
public void setCaret(Caret c)
c
- the caretpublic Highlighter getHighlighter()
public void setHighlighter(Highlighter h)
h
- the highlighterpublic void setKeymap(Keymap map)
map
- the keymappublic Keymap getKeymap()
public static Keymap addKeymap(java.lang.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(java.lang.String nm)
nm
- the name of the keymap to removepublic static Keymap getKeymap(java.lang.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 keymap
bindings
- the bindings
actions
- the set of actionspublic java.awt.Color getCaretColor()
public void setCaretColor(java.awt.Color c)
c
- the colorpublic java.awt.Color getSelectionColor()
public void setSelectionColor(java.awt.Color c)
c
- the colorpublic java.awt.Color getSelectedTextColor()
public void setSelectedTextColor(java.awt.Color c)
c
- the colorpublic java.awt.Color getDisabledTextColor()
public void setDisabledTextColor(java.awt.Color c)
c
- the colorpublic void replaceSelection(java.lang.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.
If the component is not currently editable, beep and return. Then if the underlying model is null, do nothing.
content
- the content to replace the selection withpublic java.lang.String getText(int offs, int len) throws BadLocationException
offs
- the offset >= 0
len
- the length >= 0public java.awt.Rectangle modelToView(int pos) throws BadLocationException
pos
- the position >= 0public int viewToModel(java.awt.Point pt)
pt
- the location in the view to translatepublic void cut()
public void copy()
public void paste()
public void moveCaretPosition(int pos)
pos
- the positionpublic void setFocusAccelerator(char aKey)
aKey
- the keypublic char getFocusAccelerator()
public void read(java.io.Reader in, java.lang.Object desc) throws java.io.IOException
in
- The stream to read from
desc
- 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.public void write(java.io.Writer out) throws java.io.IOException
out
- the output streampublic void setEnabled(boolean b)
b
. An enabled component can respond to user
input and generate events. Components are enabled initially by default.
A repaint() is done after setting the new state.
b
- If true
, this component is
enabled; otherwise this component is disabled.public boolean isFocusTraversable()
protected void processComponentKeyEvent(java.awt.event.KeyEvent e)
This is implemented to take a default action, typically inserting the character into the document as content. Subclasses would normally override this method if they process some key events themselves. If the event is processed, it should be consumed.
e
- the eventpublic void setCaretPosition(int position)
position
- the positionpublic int getCaretPosition()
public void setText(java.lang.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 setpublic java.lang.String getText()
public java.lang.String getSelectedText()
public boolean isEditable()
public void setEditable(boolean b)
b
- the boolean to be setpublic 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 >= 0
selectionEnd
- the end position of the text >= 0public void selectAll()
public java.awt.Dimension getPreferredScrollableViewportSize()
public int getScrollableUnitIncrement(java.awt.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.
visibleRect
- The view area visible within the viewport
orientation
- Either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.
direction
- Less than zero to scroll up/left, greater than
zero for down/right.public int getScrollableBlockIncrement(java.awt.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.
visibleRect
- The view area visible within the viewport
orientation
- Either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.
direction
- Less than zero to scroll up/left, greater than zero
for down/right.public boolean getScrollableTracksViewportWidth()
Scrolling containers, like JViewport, will use this method each time they are validated.
public boolean getScrollableTracksViewportHeight()
Scrolling containers, like JViewport, will use this method each time they are validated.
public AccessibleContext getAccessibleContext()
Overview | Package | Class | Tree | Deprecated | Index | Help |
Java Platform 1.1.7 |
||
PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |