|
Java Platform 1.2 |
|||||||||
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).
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.
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
,
ViewFactory
, Serialized FormInner Class Summary | |
class |
JTextComponent.AccessibleJTextComponent
Accessibility implementation for JTextComponent. |
static class |
JTextComponent.KeyBinding
Binding record for creating key bindings. |
Inner classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent |
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 |
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(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 JComponent. |
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()
|
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. |
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. |
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 |
processComponentKeyEvent(KeyEvent e)
Processes any key events that the component itself recognizes. |
protected void |
processInputMethodEvent(InputMethodEvent e)
|
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 |
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(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(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,
print,
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()
public boolean isOpaque()
public void setOpaque(boolean o)
o
- true if should render a backgroundpublic 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.
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 String getText(int offs, int len) throws BadLocationException
offs
- the offset >= 0len
- the length >= 0public Rectangle modelToView(int pos) throws BadLocationException
pos
- the position >= 0TextUI.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.EditorKit.createDefaultDocument()
,
setDocument(javax.swing.text.Document)
,
PlainDocument
public void write(Writer out) throws IOException
out
- the output streampublic void removeNotify()
public 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.Component.isEnabled()
public boolean isFocusTraversable()
protected void processComponentKeyEvent(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(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()
setText(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()
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.
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.JScrollBar.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.
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.JScrollBar.setBlockIncrement(int)
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()
protected String paramString()
null
.
Overriding paramString() to provide information about the specific new aspects of the JFC components.
protected void processInputMethodEvent(InputMethodEvent e)
public InputMethodRequests getInputMethodRequests()
|
Java Platform 1.2 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |