I l@ve RuBoard |
![]() ![]() |
19.2 The JTextComponent ClassThe six concrete Swing text component classes have quite a bit in common. Consequently, they share a common base class, JTextComponent. Figure 19-2 shows the class hierarchy for the Swing text components. As you can see, the concrete text components are in the javax.swing package with the rest of the Swing component classes, but JTextComponent and all its supporting classes can be found in javax.swing.text. Figure 19-2. The Swing text components![]() JTextComponent is an abstract class that serves as the base class for all text-based Swing components. It defines a large number of properties and methods that apply to its subclasses. In this introductory chapter, we'll pass quickly over many of these properties, as they require an understanding of the underlying model and view aspects of the text framework. 19.2.1 PropertiesJTextComponent defines the properties and default values shown in Table 19-1. document is a reference to the Document model for the component, where the component's content data is stored. (We'll discuss the details of the Document interface in Chapter 22.) The UI property for all text components is a subclass of javax.swing.plaf.TextUI.
accessibleContext refers to an instance of the inner class AccessibleJTextComponent that implements the AccessibleText interface, which is described in Chapter 25. The actions property specifies a list of Actions (such as page-up, change-font, or paste-from-clipboard) that are available to the component. The actual list is created by the component's EditorKit, which we'll cover in detail in Chapter 23. This read-only property exists for the convenience of the text component and those who subclass it, and the presence or absence of an Action on the list does not mean that Action is or is not installed in the component's active Keymap or InputMap. caret represents the location at which data is inserted into the document (in other words, the cursor position; Swing uses the term caret instead of cursor). highlighter is an object responsible for drawing highlighted text. keymap allows you to specify, for example, the keystrokes that cause text to be cut (e.g., Ctrl-X) or pasted (e.g, Command-V). We'll discuss all three of these in Chapter 21. caretColor , disabledTextColor, selectionColor , and selectedTextColor simply specify the color used to render the caret, disabled text, selection background, and selected text, respectively.[1]
The dragEnabled property, new in SDK 1.4, should be set to true to enable automatic drag handling (if the L&F supports it). Because automatic drag could possibly change the way text-selection gestures are interpreted, the default value for this property is false. Note that the accessor method is named getDragEnabled( ), not isDragEnabled( ), as convention would dictate. The editable property indicates whether the document can be edited. If this property is set to false, characters typed into the component are not inserted—the component is used only for displaying. The focusAccelerator property specifies the key that can be used to give focus to the text component. The default value (\0) indicates that no focus accelerator has been set. The inherited layout property defaults to null because the layout of text components is handled by the View hierarchy (more on this later). The margin property specifies the distance between the component's text and its border (using an instance of java.awt.Insets). navigationFilter (if not null) is used by the caret to restrict cursor movement. The selectedText and text properties are managed by the component's Document while selectionStart, selectionEnd, and caretPosition delegate to the caret. The preferredScrollableViewportSize property (from the Scrollable interface) simply delegates to getPreferredSize( ). The values of the other two properties from the Scrollable interface, scrollableTracksViewportHeight and scrollableTracksViewport-Width, are determined by the size of the JViewport containing the component. If the viewport is larger than the preferred size of the component, these properties are true. Otherwise (or if the component's parent is not a JViewport), they are false. 19.2.2 EventsJTextComponent fires a CaretEvent any time the state of the component's Caret changes. So any time the cursor position changes, an event is fired. The only exception is that while a selection is being made (the mouse is being dragged), events are not fired as the cursor moves, only when the mouse is released and the selection has been created. The following standard methods are provided for working with caret events. (Note that the getCaretListeners( ) method was introduced in SDK 1.4.)
The CaretEvent class and CaretListener interface are described in detail in Chapter 21. In addition to firing CaretEvents, JTextComponents naturally fire property change events when the values of bound properties are changed. 19.2.3 ConstantsTable 19-2 defines the JTextComponent constants. There are no constants defined for any bound properties other than focusAccelerator, but there is nothing special about FOCUS_ACCELERATOR_KEY, and its solitary existence as a constant is most likely evidence of an oversight with respect to the other bound properties.
19.2.4 Constructor
19.2.5 Clipboard MethodsThese methods allow you to move data to and from the system clipboard:
19.2.6 Selection MethodsThese methods are concerned with selecting (highlighting) text:
19.2.7 View MethodsThese methods provide mappings between model and view coordinate systems (e.g., pixel (53,71) of the view might correspond to character 42 of the document). They also provide information necessary for scrolling the text component.
19.2.8 Working with KeymapsThe Swing text components allow you to map keystrokes to specified actions. This capability existed before the more flexible and universal InputMap and ActionMap mechanisms emerged. (These are discussed in Section 3.5.14.) The Keymap-based methods are still available for backward compatibility and were reimplemented using the new framework. The following methods are defined in the JTextComponent base class and are discussed in more detail in Chapter 21:
19.2.9 Other Methods
|
I l@ve RuBoard |
![]() ![]() |