7 * published by the Free Software Foundation. Oracle designates this
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 sun.swing.SwingUtilities2;
28
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.beans.*;
32 import javax.swing.text.*;
33 import javax.swing.plaf.*;
34 import javax.swing.event.*;
35 import javax.accessibility.*;
36
37 import java.io.ObjectOutputStream;
38 import java.io.ObjectInputStream;
39 import java.io.IOException;
40 import java.io.Serializable;
41
42 /**
43 * <code>JTextField</code> is a lightweight component that allows the editing
44 * of a single line of text.
45 * For information on and examples of using text fields,
46 * see
47 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>
48 * in <em>The Java Tutorial.</em>
49 *
50 * <p>
51 * <code>JTextField</code> is intended to be source-compatible
52 * with <code>java.awt.TextField</code> where it is reasonable to do so. This
53 * component has capabilities not found in the <code>java.awt.TextField</code>
54 * class. The superclass should be consulted for additional capabilities.
55 * <p>
56 * <code>JTextField</code> has a method to establish the string used as the
57 * command string for the action event that gets fired. The
58 * <code>java.awt.TextField</code> used the text of the field as the command
135 }
136 }
137 }
138
139 * </code></pre>
140 * <p>
141 * <strong>Warning:</strong> Swing is not thread safe. For more
142 * information see <a
143 * href="package-summary.html#threading">Swing's Threading
144 * Policy</a>.
145 * <p>
146 * <strong>Warning:</strong>
147 * Serialized objects of this class will not be compatible with
148 * future Swing releases. The current serialization support is
149 * appropriate for short term storage or RMI between applications running
150 * the same version of Swing. As of 1.4, support for long term storage
151 * of all JavaBeans™
152 * has been added to the <code>java.beans</code> package.
153 * Please see {@link java.beans.XMLEncoder}.
154 *
155 * @beaninfo
156 * attribute: isContainer false
157 * description: A component which allows for the editing of a single line of text.
158 *
159 * @author Timothy Prinzing
160 * @see #setActionCommand
161 * @see JPasswordField
162 * @see #addActionListener
163 * @since 1.2
164 */
165 @SuppressWarnings("serial") // Same-version serialization only
166 public class JTextField extends JTextComponent implements SwingConstants {
167
168 /**
169 * Constructs a new <code>TextField</code>. A default model is created,
170 * the initial string is <code>null</code>,
171 * and the number of columns is set to 0.
172 */
173 public JTextField() {
174 this(null, null, 0);
175 }
176
177 /**
178 * Constructs a new <code>TextField</code> initialized with the
179 * specified text. A default model is created and the number of
180 * columns is 0.
181 *
182 * @param text the text to be displayed, or <code>null</code>
183 */
184 public JTextField(String text) {
236 }
237 visibility = new DefaultBoundedRangeModel();
238 visibility.addChangeListener(new ScrollRepainter());
239 this.columns = columns;
240 if (doc == null) {
241 doc = createDefaultModel();
242 }
243 setDocument(doc);
244 if (text != null) {
245 setText(text);
246 }
247 }
248
249 /**
250 * Gets the class ID for a UI.
251 *
252 * @return the string "TextFieldUI"
253 * @see JComponent#getUIClassID
254 * @see UIDefaults#getUI
255 */
256 public String getUIClassID() {
257 return uiClassID;
258 }
259
260
261 /**
262 * Associates the editor with a text document.
263 * The currently registered factory is used to build a view for
264 * the document, which gets displayed by the editor after revalidation.
265 * A PropertyChange event ("document") is propagated to each listener.
266 *
267 * @param doc the document to display/edit
268 * @see #getDocument
269 * @beaninfo
270 * description: the text document model
271 * bound: true
272 * expert: true
273 */
274 public void setDocument(Document doc) {
275 if (doc != null) {
276 doc.putProperty("filterNewlines", Boolean.TRUE);
277 }
278 super.setDocument(doc);
279 }
280
281 /**
282 * Calls to <code>revalidate</code> that come from within the
283 * textfield itself will
284 * be handled by validating the textfield, unless the textfield
285 * is contained within a <code>JViewport</code>,
286 * in which case this returns false.
287 *
288 * @return if the parent of this textfield is a <code>JViewPort</code>
289 * return false, otherwise return true
290 *
291 * @see JComponent#revalidate
292 * @see JComponent#isValidateRoot
293 * @see java.awt.Container#isValidateRoot
315 return horizontalAlignment;
316 }
317
318 /**
319 * Sets the horizontal alignment of the text.
320 * Valid keys are:
321 * <ul>
322 * <li><code>JTextField.LEFT</code>
323 * <li><code>JTextField.CENTER</code>
324 * <li><code>JTextField.RIGHT</code>
325 * <li><code>JTextField.LEADING</code>
326 * <li><code>JTextField.TRAILING</code>
327 * </ul>
328 * <code>invalidate</code> and <code>repaint</code> are called when the
329 * alignment is set,
330 * and a <code>PropertyChange</code> event ("horizontalAlignment") is fired.
331 *
332 * @param alignment the alignment
333 * @exception IllegalArgumentException if <code>alignment</code>
334 * is not a valid key
335 * @beaninfo
336 * preferred: true
337 * bound: true
338 * description: Set the field alignment to LEFT, CENTER, RIGHT,
339 * LEADING (the default) or TRAILING
340 * enum: LEFT JTextField.LEFT CENTER JTextField.CENTER RIGHT JTextField.RIGHT
341 * LEADING JTextField.LEADING TRAILING JTextField.TRAILING
342 */
343 public void setHorizontalAlignment(int alignment) {
344 if (alignment == horizontalAlignment) return;
345 int oldValue = horizontalAlignment;
346 if ((alignment == LEFT) || (alignment == CENTER) ||
347 (alignment == RIGHT)|| (alignment == LEADING) ||
348 (alignment == TRAILING)) {
349 horizontalAlignment = alignment;
350 } else {
351 throw new IllegalArgumentException("horizontalAlignment");
352 }
353 firePropertyChange("horizontalAlignment", oldValue, horizontalAlignment);
354 invalidate();
355 repaint();
356 }
357
358 /**
359 * Creates the default implementation of the model
360 * to be used at construction if one isn't explicitly
361 * given. An instance of <code>PlainDocument</code> is returned.
362 *
365 protected Document createDefaultModel() {
366 return new PlainDocument();
367 }
368
369 /**
370 * Returns the number of columns in this <code>TextField</code>.
371 *
372 * @return the number of columns >= 0
373 */
374 public int getColumns() {
375 return columns;
376 }
377
378 /**
379 * Sets the number of columns in this <code>TextField</code>,
380 * and then invalidate the layout.
381 *
382 * @param columns the number of columns >= 0
383 * @exception IllegalArgumentException if <code>columns</code>
384 * is less than 0
385 * @beaninfo
386 * description: the number of columns preferred for display
387 */
388 public void setColumns(int columns) {
389 int oldVal = this.columns;
390 if (columns < 0) {
391 throw new IllegalArgumentException("columns less than zero.");
392 }
393 if (columns != oldVal) {
394 this.columns = columns;
395 invalidate();
396 }
397 }
398
399 /**
400 * Returns the column width.
401 * The meaning of what a column is can be considered a fairly weak
402 * notion for some fonts. This method is used to define the width
403 * of a column. By default this is defined to be the width of the
404 * character <em>m</em> for the font used. This method can be
405 * redefined to be some alternative amount
406 *
407 * @return the column width >= 1
459 * receives action events from this textfield.
460 *
461 * @param l the action listener to be removed
462 */
463 public synchronized void removeActionListener(ActionListener l) {
464 if ((l != null) && (getAction() == l)) {
465 setAction(null);
466 } else {
467 listenerList.remove(ActionListener.class, l);
468 }
469 }
470
471 /**
472 * Returns an array of all the <code>ActionListener</code>s added
473 * to this JTextField with addActionListener().
474 *
475 * @return all of the <code>ActionListener</code>s added or an empty
476 * array if no listeners have been added
477 * @since 1.4
478 */
479 public synchronized ActionListener[] getActionListeners() {
480 return listenerList.getListeners(ActionListener.class);
481 }
482
483 /**
484 * Notifies all listeners that have registered interest for
485 * notification on this event type. The event instance
486 * is lazily created.
487 * The listener list is processed in last to
488 * first order.
489 * @see EventListenerList
490 */
491 protected void fireActionPerformed() {
492 // Guaranteed to return a non-null array
493 Object[] listeners = listenerList.getListenerList();
494 int modifiers = 0;
495 AWTEvent currentEvent = EventQueue.getCurrentEvent();
496 if (currentEvent instanceof InputEvent) {
497 modifiers = ((InputEvent)currentEvent).getModifiers();
498 } else if (currentEvent instanceof ActionEvent) {
542 * <p>
543 * This method uses three other methods to set
544 * and help track the <code>Action</code>'s property values.
545 * It uses the <code>configurePropertiesFromAction</code> method
546 * to immediately change the textfield's properties.
547 * To track changes in the <code>Action</code>'s property values,
548 * this method registers the <code>PropertyChangeListener</code>
549 * returned by <code>createActionPropertyChangeListener</code>. The
550 * default {@code PropertyChangeListener} invokes the
551 * {@code actionPropertyChanged} method when a property in the
552 * {@code Action} changes.
553 *
554 * @param a the <code>Action</code> for the <code>JTextField</code>,
555 * or <code>null</code>
556 * @since 1.3
557 * @see Action
558 * @see #getAction
559 * @see #configurePropertiesFromAction
560 * @see #createActionPropertyChangeListener
561 * @see #actionPropertyChanged
562 * @beaninfo
563 * bound: true
564 * attribute: visualUpdate true
565 * description: the Action instance connected with this ActionEvent source
566 */
567 public void setAction(Action a) {
568 Action oldValue = getAction();
569 if (action==null || !action.equals(a)) {
570 action = a;
571 if (oldValue!=null) {
572 removeActionListener(oldValue);
573 oldValue.removePropertyChangeListener(actionPropertyChangeListener);
574 actionPropertyChangeListener = null;
575 }
576 configurePropertiesFromAction(action);
577 if (action!=null) {
578 // Don't add if it is already a listener
579 if (!isListener(ActionListener.class, action)) {
580 addActionListener(action);
581 }
582 // Reverse linkage:
583 actionPropertyChangeListener = createActionPropertyChangeListener(action);
584 action.addPropertyChangeListener(actionPropertyChangeListener);
585 }
586 firePropertyChange("action", oldValue, action);
695 protected void actionPropertyChanged(JTextField textField,
696 Action action,
697 PropertyChangeEvent e) {
698 if (AbstractAction.shouldReconfigure(e)) {
699 textField.configurePropertiesFromAction(action);
700 } else {
701 textField.actionPropertyChanged(action, e.getPropertyName());
702 }
703 }
704 }
705
706 /**
707 * Fetches the command list for the editor. This is
708 * the list of commands supported by the plugged-in UI
709 * augmented by the collection of commands that the
710 * editor itself supports. These are useful for binding
711 * to events, such as in a keymap.
712 *
713 * @return the command list
714 */
715 public Action[] getActions() {
716 return TextAction.augmentList(super.getActions(), defaultActions);
717 }
718
719 /**
720 * Processes action events occurring on this textfield by
721 * dispatching them to any registered <code>ActionListener</code> objects.
722 * This is normally called by the controller registered with
723 * textfield.
724 */
725 public void postActionEvent() {
726 fireActionPerformed();
727 }
728
729 // --- Scrolling support -----------------------------------
730
731 /**
732 * Gets the visibility of the text field. This can
733 * be adjusted to change the location of the visible
734 * area if the size of the field is greater than
735 * the area that was allocated to the field.
736 *
737 * <p>
738 * The fields look-and-feel implementation manages
739 * the values of the minimum, maximum, and extent
740 * properties on the <code>BoundedRangeModel</code>.
741 *
742 * @return the visibility
743 * @see BoundedRangeModel
744 */
745 public BoundedRangeModel getHorizontalVisibility() {
746 return visibility;
747 }
748
749 /**
750 * Gets the scroll offset, in pixels.
751 *
752 * @return the offset >= 0
753 */
754 public int getScrollOffset() {
755 return visibility.getValue();
756 }
757
758 /**
759 * Sets the scroll offset, in pixels.
760 *
761 * @param scrollOffset the offset >= 0
762 */
763 public void setScrollOffset(int scrollOffset) {
764 visibility.setValue(scrollOffset);
909 ",horizontalAlignment=" + horizontalAlignmentString;
910 }
911
912
913 /////////////////
914 // Accessibility support
915 ////////////////
916
917
918 /**
919 * Gets the <code>AccessibleContext</code> associated with this
920 * <code>JTextField</code>. For <code>JTextFields</code>,
921 * the <code>AccessibleContext</code> takes the form of an
922 * <code>AccessibleJTextField</code>.
923 * A new <code>AccessibleJTextField</code> instance is created
924 * if necessary.
925 *
926 * @return an <code>AccessibleJTextField</code> that serves as the
927 * <code>AccessibleContext</code> of this <code>JTextField</code>
928 */
929 public AccessibleContext getAccessibleContext() {
930 if (accessibleContext == null) {
931 accessibleContext = new AccessibleJTextField();
932 }
933 return accessibleContext;
934 }
935
936 /**
937 * This class implements accessibility support for the
938 * <code>JTextField</code> class. It provides an implementation of the
939 * Java Accessibility API appropriate to text field user-interface
940 * elements.
941 * <p>
942 * <strong>Warning:</strong>
943 * Serialized objects of this class will not be compatible with
944 * future Swing releases. The current serialization support is
945 * appropriate for short term storage or RMI between applications running
946 * the same version of Swing. As of 1.4, support for long term storage
947 * of all JavaBeans™
948 * has been added to the <code>java.beans</code> package.
|
7 * published by the Free Software Foundation. Oracle designates this
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 java.beans.JavaBean;
30 import java.beans.BeanProperty;
31 import java.beans.PropertyChangeEvent;
32 import java.beans.PropertyChangeListener;
33 import javax.swing.text.*;
34 import javax.swing.event.*;
35 import javax.accessibility.*;
36
37 import java.io.ObjectOutputStream;
38 import java.io.IOException;
39 import java.io.Serializable;
40
41 /**
42 * <code>JTextField</code> is a lightweight component that allows the editing
43 * of a single line of text.
44 * For information on and examples of using text fields,
45 * see
46 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>
47 * in <em>The Java Tutorial.</em>
48 *
49 * <p>
50 * <code>JTextField</code> is intended to be source-compatible
51 * with <code>java.awt.TextField</code> where it is reasonable to do so. This
52 * component has capabilities not found in the <code>java.awt.TextField</code>
53 * class. The superclass should be consulted for additional capabilities.
54 * <p>
55 * <code>JTextField</code> has a method to establish the string used as the
56 * command string for the action event that gets fired. The
57 * <code>java.awt.TextField</code> used the text of the field as the command
134 }
135 }
136 }
137
138 * </code></pre>
139 * <p>
140 * <strong>Warning:</strong> Swing is not thread safe. For more
141 * information see <a
142 * href="package-summary.html#threading">Swing's Threading
143 * Policy</a>.
144 * <p>
145 * <strong>Warning:</strong>
146 * Serialized objects of this class will not be compatible with
147 * future Swing releases. The current serialization support is
148 * appropriate for short term storage or RMI between applications running
149 * the same version of Swing. As of 1.4, support for long term storage
150 * of all JavaBeans™
151 * has been added to the <code>java.beans</code> package.
152 * Please see {@link java.beans.XMLEncoder}.
153 *
154 * @author Timothy Prinzing
155 * @see #setActionCommand
156 * @see JPasswordField
157 * @see #addActionListener
158 * @since 1.2
159 */
160 @JavaBean(defaultProperty = "UIClassID", description = "A component which allows for the editing of a single line of text.")
161 @SwingContainer(false)
162 @SuppressWarnings("serial") // Same-version serialization only
163 public class JTextField extends JTextComponent implements SwingConstants {
164
165 /**
166 * Constructs a new <code>TextField</code>. A default model is created,
167 * the initial string is <code>null</code>,
168 * and the number of columns is set to 0.
169 */
170 public JTextField() {
171 this(null, null, 0);
172 }
173
174 /**
175 * Constructs a new <code>TextField</code> initialized with the
176 * specified text. A default model is created and the number of
177 * columns is 0.
178 *
179 * @param text the text to be displayed, or <code>null</code>
180 */
181 public JTextField(String text) {
233 }
234 visibility = new DefaultBoundedRangeModel();
235 visibility.addChangeListener(new ScrollRepainter());
236 this.columns = columns;
237 if (doc == null) {
238 doc = createDefaultModel();
239 }
240 setDocument(doc);
241 if (text != null) {
242 setText(text);
243 }
244 }
245
246 /**
247 * Gets the class ID for a UI.
248 *
249 * @return the string "TextFieldUI"
250 * @see JComponent#getUIClassID
251 * @see UIDefaults#getUI
252 */
253 @BeanProperty(bound = false)
254 public String getUIClassID() {
255 return uiClassID;
256 }
257
258
259 /**
260 * Associates the editor with a text document.
261 * The currently registered factory is used to build a view for
262 * the document, which gets displayed by the editor after revalidation.
263 * A PropertyChange event ("document") is propagated to each listener.
264 *
265 * @param doc the document to display/edit
266 * @see #getDocument
267 */
268 @BeanProperty(expert = true, description
269 = "the text document model")
270 public void setDocument(Document doc) {
271 if (doc != null) {
272 doc.putProperty("filterNewlines", Boolean.TRUE);
273 }
274 super.setDocument(doc);
275 }
276
277 /**
278 * Calls to <code>revalidate</code> that come from within the
279 * textfield itself will
280 * be handled by validating the textfield, unless the textfield
281 * is contained within a <code>JViewport</code>,
282 * in which case this returns false.
283 *
284 * @return if the parent of this textfield is a <code>JViewPort</code>
285 * return false, otherwise return true
286 *
287 * @see JComponent#revalidate
288 * @see JComponent#isValidateRoot
289 * @see java.awt.Container#isValidateRoot
311 return horizontalAlignment;
312 }
313
314 /**
315 * Sets the horizontal alignment of the text.
316 * Valid keys are:
317 * <ul>
318 * <li><code>JTextField.LEFT</code>
319 * <li><code>JTextField.CENTER</code>
320 * <li><code>JTextField.RIGHT</code>
321 * <li><code>JTextField.LEADING</code>
322 * <li><code>JTextField.TRAILING</code>
323 * </ul>
324 * <code>invalidate</code> and <code>repaint</code> are called when the
325 * alignment is set,
326 * and a <code>PropertyChange</code> event ("horizontalAlignment") is fired.
327 *
328 * @param alignment the alignment
329 * @exception IllegalArgumentException if <code>alignment</code>
330 * is not a valid key
331 */
332 @BeanProperty(preferred = true, enumerationValues = {
333 "JTextField.LEFT",
334 "JTextField.CENTER",
335 "JTextField.RIGHT",
336 "JTextField.LEADING",
337 "JTextField.TRAILING"}, description
338 = "Set the field alignment to LEFT, CENTER, RIGHT, LEADING (the default) or TRAILING")
339 public void setHorizontalAlignment(int alignment) {
340 if (alignment == horizontalAlignment) return;
341 int oldValue = horizontalAlignment;
342 if ((alignment == LEFT) || (alignment == CENTER) ||
343 (alignment == RIGHT)|| (alignment == LEADING) ||
344 (alignment == TRAILING)) {
345 horizontalAlignment = alignment;
346 } else {
347 throw new IllegalArgumentException("horizontalAlignment");
348 }
349 firePropertyChange("horizontalAlignment", oldValue, horizontalAlignment);
350 invalidate();
351 repaint();
352 }
353
354 /**
355 * Creates the default implementation of the model
356 * to be used at construction if one isn't explicitly
357 * given. An instance of <code>PlainDocument</code> is returned.
358 *
361 protected Document createDefaultModel() {
362 return new PlainDocument();
363 }
364
365 /**
366 * Returns the number of columns in this <code>TextField</code>.
367 *
368 * @return the number of columns >= 0
369 */
370 public int getColumns() {
371 return columns;
372 }
373
374 /**
375 * Sets the number of columns in this <code>TextField</code>,
376 * and then invalidate the layout.
377 *
378 * @param columns the number of columns >= 0
379 * @exception IllegalArgumentException if <code>columns</code>
380 * is less than 0
381 */
382 @BeanProperty(bound = false, description
383 = "the number of columns preferred for display")
384 public void setColumns(int columns) {
385 int oldVal = this.columns;
386 if (columns < 0) {
387 throw new IllegalArgumentException("columns less than zero.");
388 }
389 if (columns != oldVal) {
390 this.columns = columns;
391 invalidate();
392 }
393 }
394
395 /**
396 * Returns the column width.
397 * The meaning of what a column is can be considered a fairly weak
398 * notion for some fonts. This method is used to define the width
399 * of a column. By default this is defined to be the width of the
400 * character <em>m</em> for the font used. This method can be
401 * redefined to be some alternative amount
402 *
403 * @return the column width >= 1
455 * receives action events from this textfield.
456 *
457 * @param l the action listener to be removed
458 */
459 public synchronized void removeActionListener(ActionListener l) {
460 if ((l != null) && (getAction() == l)) {
461 setAction(null);
462 } else {
463 listenerList.remove(ActionListener.class, l);
464 }
465 }
466
467 /**
468 * Returns an array of all the <code>ActionListener</code>s added
469 * to this JTextField with addActionListener().
470 *
471 * @return all of the <code>ActionListener</code>s added or an empty
472 * array if no listeners have been added
473 * @since 1.4
474 */
475 @BeanProperty(bound = false)
476 public synchronized ActionListener[] getActionListeners() {
477 return listenerList.getListeners(ActionListener.class);
478 }
479
480 /**
481 * Notifies all listeners that have registered interest for
482 * notification on this event type. The event instance
483 * is lazily created.
484 * The listener list is processed in last to
485 * first order.
486 * @see EventListenerList
487 */
488 protected void fireActionPerformed() {
489 // Guaranteed to return a non-null array
490 Object[] listeners = listenerList.getListenerList();
491 int modifiers = 0;
492 AWTEvent currentEvent = EventQueue.getCurrentEvent();
493 if (currentEvent instanceof InputEvent) {
494 modifiers = ((InputEvent)currentEvent).getModifiers();
495 } else if (currentEvent instanceof ActionEvent) {
539 * <p>
540 * This method uses three other methods to set
541 * and help track the <code>Action</code>'s property values.
542 * It uses the <code>configurePropertiesFromAction</code> method
543 * to immediately change the textfield's properties.
544 * To track changes in the <code>Action</code>'s property values,
545 * this method registers the <code>PropertyChangeListener</code>
546 * returned by <code>createActionPropertyChangeListener</code>. The
547 * default {@code PropertyChangeListener} invokes the
548 * {@code actionPropertyChanged} method when a property in the
549 * {@code Action} changes.
550 *
551 * @param a the <code>Action</code> for the <code>JTextField</code>,
552 * or <code>null</code>
553 * @since 1.3
554 * @see Action
555 * @see #getAction
556 * @see #configurePropertiesFromAction
557 * @see #createActionPropertyChangeListener
558 * @see #actionPropertyChanged
559 */
560 @BeanProperty(visualUpdate = true, description
561 = "the Action instance connected with this ActionEvent source")
562 public void setAction(Action a) {
563 Action oldValue = getAction();
564 if (action==null || !action.equals(a)) {
565 action = a;
566 if (oldValue!=null) {
567 removeActionListener(oldValue);
568 oldValue.removePropertyChangeListener(actionPropertyChangeListener);
569 actionPropertyChangeListener = null;
570 }
571 configurePropertiesFromAction(action);
572 if (action!=null) {
573 // Don't add if it is already a listener
574 if (!isListener(ActionListener.class, action)) {
575 addActionListener(action);
576 }
577 // Reverse linkage:
578 actionPropertyChangeListener = createActionPropertyChangeListener(action);
579 action.addPropertyChangeListener(actionPropertyChangeListener);
580 }
581 firePropertyChange("action", oldValue, action);
690 protected void actionPropertyChanged(JTextField textField,
691 Action action,
692 PropertyChangeEvent e) {
693 if (AbstractAction.shouldReconfigure(e)) {
694 textField.configurePropertiesFromAction(action);
695 } else {
696 textField.actionPropertyChanged(action, e.getPropertyName());
697 }
698 }
699 }
700
701 /**
702 * Fetches the command list for the editor. This is
703 * the list of commands supported by the plugged-in UI
704 * augmented by the collection of commands that the
705 * editor itself supports. These are useful for binding
706 * to events, such as in a keymap.
707 *
708 * @return the command list
709 */
710 @BeanProperty(bound = false)
711 public Action[] getActions() {
712 return TextAction.augmentList(super.getActions(), defaultActions);
713 }
714
715 /**
716 * Processes action events occurring on this textfield by
717 * dispatching them to any registered <code>ActionListener</code> objects.
718 * This is normally called by the controller registered with
719 * textfield.
720 */
721 public void postActionEvent() {
722 fireActionPerformed();
723 }
724
725 // --- Scrolling support -----------------------------------
726
727 /**
728 * Gets the visibility of the text field. This can
729 * be adjusted to change the location of the visible
730 * area if the size of the field is greater than
731 * the area that was allocated to the field.
732 *
733 * <p>
734 * The fields look-and-feel implementation manages
735 * the values of the minimum, maximum, and extent
736 * properties on the <code>BoundedRangeModel</code>.
737 *
738 * @return the visibility
739 * @see BoundedRangeModel
740 */
741 @BeanProperty(bound = false)
742 public BoundedRangeModel getHorizontalVisibility() {
743 return visibility;
744 }
745
746 /**
747 * Gets the scroll offset, in pixels.
748 *
749 * @return the offset >= 0
750 */
751 public int getScrollOffset() {
752 return visibility.getValue();
753 }
754
755 /**
756 * Sets the scroll offset, in pixels.
757 *
758 * @param scrollOffset the offset >= 0
759 */
760 public void setScrollOffset(int scrollOffset) {
761 visibility.setValue(scrollOffset);
906 ",horizontalAlignment=" + horizontalAlignmentString;
907 }
908
909
910 /////////////////
911 // Accessibility support
912 ////////////////
913
914
915 /**
916 * Gets the <code>AccessibleContext</code> associated with this
917 * <code>JTextField</code>. For <code>JTextFields</code>,
918 * the <code>AccessibleContext</code> takes the form of an
919 * <code>AccessibleJTextField</code>.
920 * A new <code>AccessibleJTextField</code> instance is created
921 * if necessary.
922 *
923 * @return an <code>AccessibleJTextField</code> that serves as the
924 * <code>AccessibleContext</code> of this <code>JTextField</code>
925 */
926 @BeanProperty(bound = false)
927 public AccessibleContext getAccessibleContext() {
928 if (accessibleContext == null) {
929 accessibleContext = new AccessibleJTextField();
930 }
931 return accessibleContext;
932 }
933
934 /**
935 * This class implements accessibility support for the
936 * <code>JTextField</code> class. It provides an implementation of the
937 * Java Accessibility API appropriate to text field user-interface
938 * elements.
939 * <p>
940 * <strong>Warning:</strong>
941 * Serialized objects of this class will not be compatible with
942 * future Swing releases. The current serialization support is
943 * appropriate for short term storage or RMI between applications running
944 * the same version of Swing. As of 1.4, support for long term storage
945 * of all JavaBeans™
946 * has been added to the <code>java.beans</code> package.
|