8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.text.*;
30 import javax.swing.plaf.*;
31 import javax.accessibility.*;
32
33 import java.util.Collections;
34 import java.util.Set;
35 import java.util.StringTokenizer;
36
37 import java.io.ObjectOutputStream;
38 import java.io.ObjectInputStream;
39 import java.io.IOException;
40
41 /**
42 * A <code>JTextArea</code> is a multi-line area that displays plain text.
43 * It is intended to be a lightweight component that provides source
44 * compatibility with the <code>java.awt.TextArea</code> class where it can
45 * reasonably do so.
46 * You can find information and examples of using all the text components in
47 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
48 * a section in <em>The Java Tutorial.</em>
49 *
50 * <p>
51 * This component has capabilities not found in the
52 * <code>java.awt.TextArea</code> class. The superclass should be
53 * consulted for additional capabilities.
54 * Alternative multi-line text classes with
55 * more capabilities are <code>JTextPane</code> and <code>JEditorPane</code>.
56 * <p>
57 * The <code>java.awt.TextArea</code> internally handles scrolling.
58 * <code>JTextArea</code> is different in that it doesn't manage scrolling,
99 * <dd>
100 * For a discussion on how newlines are handled, see
101 * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
102 * </dl>
103 *
104 * <p>
105 * <strong>Warning:</strong> Swing is not thread safe. For more
106 * information see <a
107 * href="package-summary.html#threading">Swing's Threading
108 * Policy</a>.
109 * <p>
110 * <strong>Warning:</strong>
111 * Serialized objects of this class will not be compatible with
112 * future Swing releases. The current serialization support is
113 * appropriate for short term storage or RMI between applications running
114 * the same version of Swing. As of 1.4, support for long term storage
115 * of all JavaBeans™
116 * has been added to the <code>java.beans</code> package.
117 * Please see {@link java.beans.XMLEncoder}.
118 *
119 * @beaninfo
120 * attribute: isContainer false
121 * description: A multi-line area that displays plain text.
122 *
123 * @author Timothy Prinzing
124 * @see JTextPane
125 * @see JEditorPane
126 * @since 1.2
127 */
128 @SuppressWarnings("serial") // Same-version serialization only
129 public class JTextArea extends JTextComponent {
130
131 /**
132 * @see #getUIClassID
133 * @see #readObject
134 */
135 private static final String uiClassID = "TextAreaUI";
136
137 /**
138 * Constructs a new TextArea. A default model is set, the initial string
139 * is null, and rows/columns are set to 0.
140 */
141 public JTextArea() {
142 this(null, null, 0, 0);
143 }
144
145 /**
146 * Constructs a new TextArea with the specified text displayed.
147 * A default model is created and rows/columns are set to 0.
220 if (columns < 0) {
221 throw new IllegalArgumentException("columns: " + columns);
222 }
223 LookAndFeel.installProperty(this,
224 "focusTraversalKeysForward",
225 JComponent.
226 getManagingFocusForwardTraversalKeys());
227 LookAndFeel.installProperty(this,
228 "focusTraversalKeysBackward",
229 JComponent.
230 getManagingFocusBackwardTraversalKeys());
231 }
232
233 /**
234 * Returns the class ID for the UI.
235 *
236 * @return the ID ("TextAreaUI")
237 * @see JComponent#getUIClassID
238 * @see UIDefaults#getUI
239 */
240 public String getUIClassID() {
241 return uiClassID;
242 }
243
244 /**
245 * Creates the default implementation of the model
246 * to be used at construction if one isn't explicitly
247 * given. A new instance of PlainDocument is returned.
248 *
249 * @return the default document model
250 */
251 protected Document createDefaultModel() {
252 return new PlainDocument();
253 }
254
255 /**
256 * Sets the number of characters to expand tabs to.
257 * This will be multiplied by the maximum advance for
258 * variable width fonts. A PropertyChange event ("tabSize") is fired
259 * when the tab size changes.
260 *
261 * @param size number of characters to expand to
262 * @see #getTabSize
263 * @beaninfo
264 * preferred: true
265 * bound: true
266 * description: the number of characters to expand tabs to
267 */
268 public void setTabSize(int size) {
269 Document doc = getDocument();
270 if (doc != null) {
271 int old = getTabSize();
272 doc.putProperty(PlainDocument.tabSizeAttribute, Integer.valueOf(size));
273 firePropertyChange("tabSize", old, size);
274 }
275 }
276
277 /**
278 * Gets the number of characters used to expand tabs. If the document is
279 * null or doesn't have a tab setting, return a default of 8.
280 *
281 * @return the number of characters
282 */
283 public int getTabSize() {
284 int size = 8;
285 Document doc = getDocument();
286 if (doc != null) {
287 Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
288 if (i != null) {
289 size = i.intValue();
290 }
291 }
292 return size;
293 }
294
295 /**
296 * Sets the line-wrapping policy of the text area. If set
297 * to true the lines will be wrapped if they are too long
298 * to fit within the allocated width. If set to false,
299 * the lines will always be unwrapped. A <code>PropertyChange</code>
300 * event ("lineWrap") is fired when the policy is changed.
301 * By default this property is false.
302 *
303 * @param wrap indicates if lines should be wrapped
304 * @see #getLineWrap
305 * @beaninfo
306 * preferred: true
307 * bound: true
308 * description: should lines be wrapped
309 */
310 public void setLineWrap(boolean wrap) {
311 boolean old = this.wrap;
312 this.wrap = wrap;
313 firePropertyChange("lineWrap", old, wrap);
314 }
315
316 /**
317 * Gets the line-wrapping policy of the text area. If set
318 * to true the lines will be wrapped if they are too long
319 * to fit within the allocated width. If set to false,
320 * the lines will always be unwrapped.
321 *
322 * @return if lines will be wrapped
323 */
324 public boolean getLineWrap() {
325 return wrap;
326 }
327
328 /**
329 * Sets the style of wrapping used if the text area is wrapping
330 * lines. If set to true the lines will be wrapped at word
331 * boundaries (whitespace) if they are too long
332 * to fit within the allocated width. If set to false,
333 * the lines will be wrapped at character boundaries.
334 * By default this property is false.
335 *
336 * @param word indicates if word boundaries should be used
337 * for line wrapping
338 * @see #getWrapStyleWord
339 * @beaninfo
340 * preferred: false
341 * bound: true
342 * description: should wrapping occur at word boundaries
343 */
344 public void setWrapStyleWord(boolean word) {
345 boolean old = this.word;
346 this.word = word;
347 firePropertyChange("wrapStyleWord", old, word);
348 }
349
350 /**
351 * Gets the style of wrapping used if the text area is wrapping
352 * lines. If set to true the lines will be wrapped at word
353 * boundaries (ie whitespace) if they are too long
354 * to fit within the allocated width. If set to false,
355 * the lines will be wrapped at character boundaries.
356 *
357 * @return if the wrap style should be word boundaries
358 * instead of character boundaries
359 * @see #setWrapStyleWord
360 */
361 public boolean getWrapStyleWord() {
362 return word;
363 }
371 * @exception BadLocationException thrown if the offset is
372 * less than zero or greater than the document length.
373 */
374 public int getLineOfOffset(int offset) throws BadLocationException {
375 Document doc = getDocument();
376 if (offset < 0) {
377 throw new BadLocationException("Can't translate offset to line", -1);
378 } else if (offset > doc.getLength()) {
379 throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
380 } else {
381 Element map = getDocument().getDefaultRootElement();
382 return map.getElementIndex(offset);
383 }
384 }
385
386 /**
387 * Determines the number of lines contained in the area.
388 *
389 * @return the number of lines > 0
390 */
391 public int getLineCount() {
392 Element map = getDocument().getDefaultRootElement();
393 return map.getElementCount();
394 }
395
396 /**
397 * Determines the offset of the start of the given line.
398 *
399 * @param line the line number to translate >= 0
400 * @return the offset >= 0
401 * @exception BadLocationException thrown if the line is
402 * less than zero or greater or equal to the number of
403 * lines contained in the document (as reported by
404 * getLineCount).
405 */
406 public int getLineStartOffset(int line) throws BadLocationException {
407 int lineCount = getLineCount();
408 if (line < 0) {
409 throw new BadLocationException("Negative line", -1);
410 } else if (line >= lineCount) {
514 }
515 }
516 }
517
518 /**
519 * Returns the number of rows in the TextArea.
520 *
521 * @return the number of rows >= 0
522 */
523 public int getRows() {
524 return rows;
525 }
526
527 /**
528 * Sets the number of rows for this TextArea. Calls invalidate() after
529 * setting the new value.
530 *
531 * @param rows the number of rows >= 0
532 * @exception IllegalArgumentException if rows is less than 0
533 * @see #getRows
534 * @beaninfo
535 * description: the number of rows preferred for display
536 */
537 public void setRows(int rows) {
538 int oldVal = this.rows;
539 if (rows < 0) {
540 throw new IllegalArgumentException("rows less than zero.");
541 }
542 if (rows != oldVal) {
543 this.rows = rows;
544 invalidate();
545 }
546 }
547
548 /**
549 * Defines the meaning of the height of a row. This defaults to
550 * the height of the font.
551 *
552 * @return the height >= 1
553 */
554 protected int getRowHeight() {
555 if (rowHeight == 0) {
556 FontMetrics metrics = getFontMetrics(getFont());
558 }
559 return rowHeight;
560 }
561
562 /**
563 * Returns the number of columns in the TextArea.
564 *
565 * @return number of columns >= 0
566 */
567 public int getColumns() {
568 return columns;
569 }
570
571 /**
572 * Sets the number of columns for this TextArea. Does an invalidate()
573 * after setting the new value.
574 *
575 * @param columns the number of columns >= 0
576 * @exception IllegalArgumentException if columns is less than 0
577 * @see #getColumns
578 * @beaninfo
579 * description: the number of columns preferred for display
580 */
581 public void setColumns(int columns) {
582 int oldVal = this.columns;
583 if (columns < 0) {
584 throw new IllegalArgumentException("columns less than zero.");
585 }
586 if (columns != oldVal) {
587 this.columns = columns;
588 invalidate();
589 }
590 }
591
592 /**
593 * Gets column width.
594 * The meaning of what a column is can be considered a fairly weak
595 * notion for some fonts. This method is used to define the width
596 * of a column. By default this is defined to be the width of the
597 * character <em>m</em> for the font used. This method can be
598 * redefined to be some alternative amount.
599 *
600 * @return the column width >= 1
663 return super.paramString() +
664 ",colums=" + columns +
665 ",columWidth=" + columnWidth +
666 ",rows=" + rows +
667 ",rowHeight=" + rowHeight +
668 ",word=" + wordString +
669 ",wrap=" + wrapString;
670 }
671
672 // --- Scrollable methods ----------------------------------------
673
674 /**
675 * Returns true if a viewport should always force the width of this
676 * Scrollable to match the width of the viewport. This is implemented
677 * to return true if the line wrapping policy is true, and false
678 * if lines are not being wrapped.
679 *
680 * @return true if a viewport should force the Scrollables width
681 * to match its own.
682 */
683 public boolean getScrollableTracksViewportWidth() {
684 return (wrap) ? true : super.getScrollableTracksViewportWidth();
685 }
686
687 /**
688 * Returns the preferred size of the viewport if this component
689 * is embedded in a JScrollPane. This uses the desired column
690 * and row settings if they have been set, otherwise the superclass
691 * behavior is used.
692 *
693 * @return The preferredSize of a JViewport whose view is this Scrollable.
694 * @see JViewport#getPreferredSize
695 */
696 public Dimension getPreferredScrollableViewportSize() {
697 Dimension size = super.getPreferredScrollableViewportSize();
698 size = (size == null) ? new Dimension(400,400) : size;
699 Insets insets = getInsets();
700
701 size.width = (columns == 0) ? size.width :
702 columns * getColumnWidth() + insets.left + insets.right;
703 size.height = (rows == 0) ? size.height :
704 rows * getRowHeight() + insets.top + insets.bottom;
705 return size;
706 }
707
708 /**
709 * Components that display logical rows or columns should compute
710 * the scroll increment that will completely expose one new row
711 * or column, depending on the value of orientation. This is implemented
712 * to use the values returned by the <code>getRowHeight</code> and
713 * <code>getColumnWidth</code> methods.
714 * <p>
715 * Scrolling containers, like JScrollPane, will use this method
749 if (count == 0 && ui != null) {
750 ui.installUI(this);
751 }
752 }
753 }
754
755 /////////////////
756 // Accessibility support
757 ////////////////
758
759
760 /**
761 * Gets the AccessibleContext associated with this JTextArea.
762 * For JTextAreas, the AccessibleContext takes the form of an
763 * AccessibleJTextArea.
764 * A new AccessibleJTextArea instance is created if necessary.
765 *
766 * @return an AccessibleJTextArea that serves as the
767 * AccessibleContext of this JTextArea
768 */
769 public AccessibleContext getAccessibleContext() {
770 if (accessibleContext == null) {
771 accessibleContext = new AccessibleJTextArea();
772 }
773 return accessibleContext;
774 }
775
776 /**
777 * This class implements accessibility support for the
778 * <code>JTextArea</code> class. It provides an implementation of the
779 * Java Accessibility API appropriate to text area user-interface
780 * elements.
781 * <p>
782 * <strong>Warning:</strong>
783 * Serialized objects of this class will not be compatible with
784 * future Swing releases. The current serialization support is
785 * appropriate for short term storage or RMI between applications running
786 * the same version of Swing. As of 1.4, support for long term storage
787 * of all JavaBeans™
788 * has been added to the <code>java.beans</code> package.
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import java.awt.*;
28 import java.beans.JavaBean;
29 import java.beans.BeanProperty;
30 import javax.swing.text.*;
31 import javax.accessibility.*;
32
33 import java.io.ObjectOutputStream;
34 import java.io.IOException;
35
36 /**
37 * A <code>JTextArea</code> is a multi-line area that displays plain text.
38 * It is intended to be a lightweight component that provides source
39 * compatibility with the <code>java.awt.TextArea</code> class where it can
40 * reasonably do so.
41 * You can find information and examples of using all the text components in
42 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
43 * a section in <em>The Java Tutorial.</em>
44 *
45 * <p>
46 * This component has capabilities not found in the
47 * <code>java.awt.TextArea</code> class. The superclass should be
48 * consulted for additional capabilities.
49 * Alternative multi-line text classes with
50 * more capabilities are <code>JTextPane</code> and <code>JEditorPane</code>.
51 * <p>
52 * The <code>java.awt.TextArea</code> internally handles scrolling.
53 * <code>JTextArea</code> is different in that it doesn't manage scrolling,
94 * <dd>
95 * For a discussion on how newlines are handled, see
96 * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
97 * </dl>
98 *
99 * <p>
100 * <strong>Warning:</strong> Swing is not thread safe. For more
101 * information see <a
102 * href="package-summary.html#threading">Swing's Threading
103 * Policy</a>.
104 * <p>
105 * <strong>Warning:</strong>
106 * Serialized objects of this class will not be compatible with
107 * future Swing releases. The current serialization support is
108 * appropriate for short term storage or RMI between applications running
109 * the same version of Swing. As of 1.4, support for long term storage
110 * of all JavaBeans™
111 * has been added to the <code>java.beans</code> package.
112 * Please see {@link java.beans.XMLEncoder}.
113 *
114 * @author Timothy Prinzing
115 * @see JTextPane
116 * @see JEditorPane
117 * @since 1.2
118 */
119 @JavaBean(defaultProperty = "UIClassID", description = "A multi-line area that displays plain text.")
120 @SwingContainer(false)
121 @SuppressWarnings("serial") // Same-version serialization only
122 public class JTextArea extends JTextComponent {
123
124 /**
125 * @see #getUIClassID
126 * @see #readObject
127 */
128 private static final String uiClassID = "TextAreaUI";
129
130 /**
131 * Constructs a new TextArea. A default model is set, the initial string
132 * is null, and rows/columns are set to 0.
133 */
134 public JTextArea() {
135 this(null, null, 0, 0);
136 }
137
138 /**
139 * Constructs a new TextArea with the specified text displayed.
140 * A default model is created and rows/columns are set to 0.
213 if (columns < 0) {
214 throw new IllegalArgumentException("columns: " + columns);
215 }
216 LookAndFeel.installProperty(this,
217 "focusTraversalKeysForward",
218 JComponent.
219 getManagingFocusForwardTraversalKeys());
220 LookAndFeel.installProperty(this,
221 "focusTraversalKeysBackward",
222 JComponent.
223 getManagingFocusBackwardTraversalKeys());
224 }
225
226 /**
227 * Returns the class ID for the UI.
228 *
229 * @return the ID ("TextAreaUI")
230 * @see JComponent#getUIClassID
231 * @see UIDefaults#getUI
232 */
233 @BeanProperty(bound = false)
234 public String getUIClassID() {
235 return uiClassID;
236 }
237
238 /**
239 * Creates the default implementation of the model
240 * to be used at construction if one isn't explicitly
241 * given. A new instance of PlainDocument is returned.
242 *
243 * @return the default document model
244 */
245 protected Document createDefaultModel() {
246 return new PlainDocument();
247 }
248
249 /**
250 * Sets the number of characters to expand tabs to.
251 * This will be multiplied by the maximum advance for
252 * variable width fonts. A PropertyChange event ("tabSize") is fired
253 * when the tab size changes.
254 *
255 * @param size number of characters to expand to
256 * @see #getTabSize
257 */
258 @BeanProperty(preferred = true, description
259 = "the number of characters to expand tabs to")
260 public void setTabSize(int size) {
261 Document doc = getDocument();
262 if (doc != null) {
263 int old = getTabSize();
264 doc.putProperty(PlainDocument.tabSizeAttribute, Integer.valueOf(size));
265 firePropertyChange("tabSize", old, size);
266 }
267 }
268
269 /**
270 * Gets the number of characters used to expand tabs. If the document is
271 * null or doesn't have a tab setting, return a default of 8.
272 *
273 * @return the number of characters
274 */
275 public int getTabSize() {
276 int size = 8;
277 Document doc = getDocument();
278 if (doc != null) {
279 Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
280 if (i != null) {
281 size = i.intValue();
282 }
283 }
284 return size;
285 }
286
287 /**
288 * Sets the line-wrapping policy of the text area. If set
289 * to true the lines will be wrapped if they are too long
290 * to fit within the allocated width. If set to false,
291 * the lines will always be unwrapped. A <code>PropertyChange</code>
292 * event ("lineWrap") is fired when the policy is changed.
293 * By default this property is false.
294 *
295 * @param wrap indicates if lines should be wrapped
296 * @see #getLineWrap
297 */
298 @BeanProperty(preferred = true, description
299 = "should lines be wrapped")
300 public void setLineWrap(boolean wrap) {
301 boolean old = this.wrap;
302 this.wrap = wrap;
303 firePropertyChange("lineWrap", old, wrap);
304 }
305
306 /**
307 * Gets the line-wrapping policy of the text area. If set
308 * to true the lines will be wrapped if they are too long
309 * to fit within the allocated width. If set to false,
310 * the lines will always be unwrapped.
311 *
312 * @return if lines will be wrapped
313 */
314 public boolean getLineWrap() {
315 return wrap;
316 }
317
318 /**
319 * Sets the style of wrapping used if the text area is wrapping
320 * lines. If set to true the lines will be wrapped at word
321 * boundaries (whitespace) if they are too long
322 * to fit within the allocated width. If set to false,
323 * the lines will be wrapped at character boundaries.
324 * By default this property is false.
325 *
326 * @param word indicates if word boundaries should be used
327 * for line wrapping
328 * @see #getWrapStyleWord
329 */
330 @BeanProperty(description
331 = "should wrapping occur at word boundaries")
332 public void setWrapStyleWord(boolean word) {
333 boolean old = this.word;
334 this.word = word;
335 firePropertyChange("wrapStyleWord", old, word);
336 }
337
338 /**
339 * Gets the style of wrapping used if the text area is wrapping
340 * lines. If set to true the lines will be wrapped at word
341 * boundaries (ie whitespace) if they are too long
342 * to fit within the allocated width. If set to false,
343 * the lines will be wrapped at character boundaries.
344 *
345 * @return if the wrap style should be word boundaries
346 * instead of character boundaries
347 * @see #setWrapStyleWord
348 */
349 public boolean getWrapStyleWord() {
350 return word;
351 }
359 * @exception BadLocationException thrown if the offset is
360 * less than zero or greater than the document length.
361 */
362 public int getLineOfOffset(int offset) throws BadLocationException {
363 Document doc = getDocument();
364 if (offset < 0) {
365 throw new BadLocationException("Can't translate offset to line", -1);
366 } else if (offset > doc.getLength()) {
367 throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
368 } else {
369 Element map = getDocument().getDefaultRootElement();
370 return map.getElementIndex(offset);
371 }
372 }
373
374 /**
375 * Determines the number of lines contained in the area.
376 *
377 * @return the number of lines > 0
378 */
379 @BeanProperty(bound = false)
380 public int getLineCount() {
381 Element map = getDocument().getDefaultRootElement();
382 return map.getElementCount();
383 }
384
385 /**
386 * Determines the offset of the start of the given line.
387 *
388 * @param line the line number to translate >= 0
389 * @return the offset >= 0
390 * @exception BadLocationException thrown if the line is
391 * less than zero or greater or equal to the number of
392 * lines contained in the document (as reported by
393 * getLineCount).
394 */
395 public int getLineStartOffset(int line) throws BadLocationException {
396 int lineCount = getLineCount();
397 if (line < 0) {
398 throw new BadLocationException("Negative line", -1);
399 } else if (line >= lineCount) {
503 }
504 }
505 }
506
507 /**
508 * Returns the number of rows in the TextArea.
509 *
510 * @return the number of rows >= 0
511 */
512 public int getRows() {
513 return rows;
514 }
515
516 /**
517 * Sets the number of rows for this TextArea. Calls invalidate() after
518 * setting the new value.
519 *
520 * @param rows the number of rows >= 0
521 * @exception IllegalArgumentException if rows is less than 0
522 * @see #getRows
523 */
524 @BeanProperty(bound = false, description
525 = "the number of rows preferred for display")
526 public void setRows(int rows) {
527 int oldVal = this.rows;
528 if (rows < 0) {
529 throw new IllegalArgumentException("rows less than zero.");
530 }
531 if (rows != oldVal) {
532 this.rows = rows;
533 invalidate();
534 }
535 }
536
537 /**
538 * Defines the meaning of the height of a row. This defaults to
539 * the height of the font.
540 *
541 * @return the height >= 1
542 */
543 protected int getRowHeight() {
544 if (rowHeight == 0) {
545 FontMetrics metrics = getFontMetrics(getFont());
547 }
548 return rowHeight;
549 }
550
551 /**
552 * Returns the number of columns in the TextArea.
553 *
554 * @return number of columns >= 0
555 */
556 public int getColumns() {
557 return columns;
558 }
559
560 /**
561 * Sets the number of columns for this TextArea. Does an invalidate()
562 * after setting the new value.
563 *
564 * @param columns the number of columns >= 0
565 * @exception IllegalArgumentException if columns is less than 0
566 * @see #getColumns
567 */
568 @BeanProperty(bound = false, description
569 = "the number of columns preferred for display")
570 public void setColumns(int columns) {
571 int oldVal = this.columns;
572 if (columns < 0) {
573 throw new IllegalArgumentException("columns less than zero.");
574 }
575 if (columns != oldVal) {
576 this.columns = columns;
577 invalidate();
578 }
579 }
580
581 /**
582 * Gets column width.
583 * The meaning of what a column is can be considered a fairly weak
584 * notion for some fonts. This method is used to define the width
585 * of a column. By default this is defined to be the width of the
586 * character <em>m</em> for the font used. This method can be
587 * redefined to be some alternative amount.
588 *
589 * @return the column width >= 1
652 return super.paramString() +
653 ",colums=" + columns +
654 ",columWidth=" + columnWidth +
655 ",rows=" + rows +
656 ",rowHeight=" + rowHeight +
657 ",word=" + wordString +
658 ",wrap=" + wrapString;
659 }
660
661 // --- Scrollable methods ----------------------------------------
662
663 /**
664 * Returns true if a viewport should always force the width of this
665 * Scrollable to match the width of the viewport. This is implemented
666 * to return true if the line wrapping policy is true, and false
667 * if lines are not being wrapped.
668 *
669 * @return true if a viewport should force the Scrollables width
670 * to match its own.
671 */
672 @BeanProperty(bound = false)
673 public boolean getScrollableTracksViewportWidth() {
674 return (wrap) ? true : super.getScrollableTracksViewportWidth();
675 }
676
677 /**
678 * Returns the preferred size of the viewport if this component
679 * is embedded in a JScrollPane. This uses the desired column
680 * and row settings if they have been set, otherwise the superclass
681 * behavior is used.
682 *
683 * @return The preferredSize of a JViewport whose view is this Scrollable.
684 * @see JViewport#getPreferredSize
685 */
686 @BeanProperty(bound = false)
687 public Dimension getPreferredScrollableViewportSize() {
688 Dimension size = super.getPreferredScrollableViewportSize();
689 size = (size == null) ? new Dimension(400,400) : size;
690 Insets insets = getInsets();
691
692 size.width = (columns == 0) ? size.width :
693 columns * getColumnWidth() + insets.left + insets.right;
694 size.height = (rows == 0) ? size.height :
695 rows * getRowHeight() + insets.top + insets.bottom;
696 return size;
697 }
698
699 /**
700 * Components that display logical rows or columns should compute
701 * the scroll increment that will completely expose one new row
702 * or column, depending on the value of orientation. This is implemented
703 * to use the values returned by the <code>getRowHeight</code> and
704 * <code>getColumnWidth</code> methods.
705 * <p>
706 * Scrolling containers, like JScrollPane, will use this method
740 if (count == 0 && ui != null) {
741 ui.installUI(this);
742 }
743 }
744 }
745
746 /////////////////
747 // Accessibility support
748 ////////////////
749
750
751 /**
752 * Gets the AccessibleContext associated with this JTextArea.
753 * For JTextAreas, the AccessibleContext takes the form of an
754 * AccessibleJTextArea.
755 * A new AccessibleJTextArea instance is created if necessary.
756 *
757 * @return an AccessibleJTextArea that serves as the
758 * AccessibleContext of this JTextArea
759 */
760 @BeanProperty(bound = false)
761 public AccessibleContext getAccessibleContext() {
762 if (accessibleContext == null) {
763 accessibleContext = new AccessibleJTextArea();
764 }
765 return accessibleContext;
766 }
767
768 /**
769 * This class implements accessibility support for the
770 * <code>JTextArea</code> class. It provides an implementation of the
771 * Java Accessibility API appropriate to text area user-interface
772 * elements.
773 * <p>
774 * <strong>Warning:</strong>
775 * Serialized objects of this class will not be compatible with
776 * future Swing releases. The current serialization support is
777 * appropriate for short term storage or RMI between applications running
778 * the same version of Swing. As of 1.4, support for long term storage
779 * of all JavaBeans™
780 * has been added to the <code>java.beans</code> package.
|