1 /* JComponent.java -- Every component in swing inherits from this class.
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.AWTEvent;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.Container;
45 import java.awt.Dimension;
46 import java.awt.FlowLayout;
48 import java.awt.Graphics;
49 import java.awt.Image;
50 import java.awt.Insets;
51 import java.awt.Point;
52 import java.awt.Rectangle;
53 import java.awt.event.ActionEvent;
54 import java.awt.event.ActionListener;
55 import java.awt.event.ContainerEvent;
56 import java.awt.event.ContainerListener;
57 import java.awt.event.FocusEvent;
58 import java.awt.event.FocusListener;
59 import java.awt.event.KeyEvent;
60 import java.awt.event.MouseEvent;
61 import java.awt.geom.Rectangle2D;
62 import java.awt.image.ImageObserver;
63 import java.awt.peer.LightweightPeer;
64 import java.beans.PropertyChangeEvent;
65 import java.beans.PropertyChangeListener;
66 import java.beans.PropertyVetoException;
67 import java.beans.VetoableChangeListener;
68 import java.io.Serializable;
69 import java.util.EventListener;
70 import java.util.Hashtable;
71 import java.util.Locale;
73 import javax.accessibility.Accessible;
74 import javax.accessibility.AccessibleContext;
75 import javax.accessibility.AccessibleRole;
76 import javax.accessibility.AccessibleStateSet;
77 import javax.swing.border.Border;
78 import javax.swing.event.AncestorListener;
79 import javax.swing.event.EventListenerList;
80 import javax.swing.event.SwingPropertyChangeSupport;
81 import javax.swing.plaf.ComponentUI;
84 * Every component in swing inherits from this class (JLabel, JButton, etc).
85 * It contains generic methods to manage events, properties and sizes. Actual
86 * drawing of the component is channeled to a look-and-feel class that is
87 * implemented elsewhere.
89 * @author Ronald Veldema (rveldema&064;cs.vu.nl)
90 * @author Graydon Hoare (graydon&064;redhat.com)
92 public abstract class JComponent extends Container implements Serializable
94 private static final long serialVersionUID = -7908749299918704233L;
97 * Accessibility support is currently missing.
100 protected AccessibleContext accessibleContext;
102 public abstract class AccessibleJComponent
103 extends AccessibleAWTContainer
105 protected class AccessibleFocusHandler
106 implements FocusListener
108 protected AccessibleFocusHandler(){}
109 public void focusGained(FocusEvent event){}
110 public void focusLost(FocusEvent valevent){}
113 protected class AccessibleContainerHandler
114 implements ContainerListener
116 protected AccessibleContainerHandler() {}
117 public void componentAdded(ContainerEvent event) {}
118 public void componentRemoved(ContainerEvent valevent) {}
121 private static final long serialVersionUID = -7047089700479897799L;
123 protected ContainerListener accessibleContainerHandler;
124 protected FocusListener accessibleFocusHandler;
126 protected AccessibleJComponent() {}
127 public void addPropertyChangeListener(PropertyChangeListener listener) {}
128 public void removePropertyChangeListener(PropertyChangeListener listener) {}
129 public int getAccessibleChildrenCount() { return 0; }
130 public Accessible getAccessibleChild(int value0) { return null; }
131 public AccessibleStateSet getAccessibleStateSet() { return null; }
132 public String getAccessibleName() { return null; }
133 public String getAccessibleDescription() { return null; }
134 public AccessibleRole getAccessibleRole() { return null; }
135 protected String getBorderTitle(Border value0) { return null; }
139 * An explicit value for the component's preferred size; if not set by a
140 * user, this is calculated on the fly by delegating to the {@link
141 * ComponentUI.getPreferredSize} method on the {@link #ui} property.
143 Dimension preferredSize;
146 * An explicit value for the component's minimum size; if not set by a
147 * user, this is calculated on the fly by delegating to the {@link
148 * ComponentUI.getMinimumSize} method on the {@link #ui} property.
150 Dimension minimumSize;
153 * An explicit value for the component's maximum size; if not set by a
154 * user, this is calculated on the fly by delegating to the {@link
155 * ComponentUI.getMaximumSize} method on the {@link #ui} property.
157 Dimension maximumSize;
161 * A value between 0.0 and 1.0 indicating the preferred horizontal
162 * alignment of the component, relative to its siblings. The values
163 * {@link #LEFT_ALIGNMENT}, {@link #CENTER_ALIGNMENT}, and {@link
164 * #RIGHT_ALIGNMENT} can also be used, as synonyms for <code>0.0</code>,
165 * <code>0.5</code>, and <code>1.0</code>, respectively. Not all layout
166 * managers use this property.
168 * @see #getAlignmentX
169 * @see #setAlignmentX
170 * @see javax.swing.OverlayLayout
171 * @see javax.swing.BoxLayout
173 float alignmentX = 0.0f;
176 * A value between 0.0 and 1.0 indicating the preferred vertical
177 * alignment of the component, relative to its siblings. The values
178 * {@link #TOP_ALIGNMENT}, {@link #CENTER_ALIGNMENT}, and {@link
179 * #BOTTOM_ALIGNMENT} can also be used, as synonyms for <code>0.0</code>,
180 * <code>0.5</code>, and <code>1.0</code>, respectively. Not all layout
181 * managers use this property.
183 * @see #getAlignmentY
184 * @see #setAlignmentY
185 * @see javax.swing.OverlayLayout
186 * @see javax.swing.BoxLayout
188 float alignmentY = 0.0f;
191 * The border painted around this component.
198 * The text to show in the tooltip associated with this component.
200 * @see #setToolTipText
201 * @see #getToolTipText
206 * <p>Whether to double buffer this component when painting. This flag
207 * should generally be <code>false</code>, except for top level
208 * components such as {@link JFrame} or {@link JApplet}.</p>
210 * <p>All children of a double buffered component are painted into the
211 * double buffer automatically, so only the top widget in a window needs
212 * to be double buffered.</p>
214 * @see #setDoubleBuffered
215 * @see #isDoubleBuffered
219 boolean doubleBuffered = false;
222 * A set of flags indicating which debugging graphics facilities should
223 * be enabled on this component. The values should be a combination of
224 * {@link DebugGraphics.NONE_OPTION}, {@link DebugGraphics.LOG_OPTION},
225 * {@link DebugGraphics.FLASH_OPTION}, or {@link
226 * DebugGraphics.BUFFERED_OPTION}.
228 * @see setDebugGraphicsOptions
229 * @see getDebugGraphicsOptions
231 * @see getComponentGraphics
233 int debugGraphicsOptions;
236 * <p>This property controls two independent behaviors simultaneously.</p>
238 * <p>First, it controls whether to fill the background of this widget
239 * when painting its body. This affects calls to {@link
240 * JComponent#paintComponent}, which in turn calls {@link
241 * ComponentUI#update} on the component's {@link #ui} property. If the
242 * component is opaque during this call, the background will be filled
243 * before calling {@link ComponentUI#paint}. This happens merely as a
244 * convenience; you may fill the component's background yourself too,
245 * but there is no need to do so if you will be filling with the same
248 * <p>Second, it the opaque property informs swing's repaint system
249 * whether it will be necessary to paint the components "underneath" this
250 * component, in Z-order. If the component is opaque, it is considered to
251 * completely occlude components "underneath" it, so they will not be
252 * repainted along with the opaque component.</p>
254 * <p>The default value for this property is <code>false</code>, but most
255 * components will want to set it to <code>true</code> when installing UI
256 * defaults in {@link ComponentUI#installUI}.</p>
260 * @see #paintComponent
262 boolean opaque = false;
265 * The user interface delegate for this component. Event delivery and
266 * repainting of the component are usually delegated to this object.
272 protected ComponentUI ui;
275 * A hint to the focus system that this component should or should not
276 * get focus. If this is <code>false</code>, swing will not try to
277 * request focus on this component; if <code>true</code>, swing might
278 * try to request focus, but the request might fail. Thus it is only
279 * a hint guiding swing's behavior.
282 * @see #isRequestFocusEnabled
283 * @see #setRequestFocusEnabled
285 boolean requestFocusEnabled;
288 * Flag indicating behavior of this component when the mouse is dragged
289 * outside the component and the mouse <em>stops moving</em>. If
290 * <code>true</code>, synthetic mouse events will be delivered on regular
291 * timed intervals, continuing off in the direction the mouse exited the
292 * component, until the mouse is released or re-enters the component.
294 * @see setAutoscrolls
295 * @see getAutoscrolls
297 boolean autoscrolls = false;
300 * Listeners for events other than {@link PropertyChangeEvent} are
301 * handled by this listener list. PropertyChangeEvents are handled in
302 * {@link #changeSupport}.
304 protected EventListenerList listenerList = new EventListenerList();
307 * Support for {@link PropertyChangeEvent} events. This is constructed
308 * lazily when the component gets its first {@link
309 * PropertyChangeListener} subscription; until then it's an empty slot.
311 private SwingPropertyChangeSupport changeSupport;
315 * Storage for "client properties", which are key/value pairs associated
316 * with this component by a "client", such as a user application or a
317 * layout manager. This is lazily constructed when the component gets its
318 * first client property.
320 private Hashtable clientProperties;
322 private InputMap inputMap_whenFocused;
323 private InputMap inputMap_whenAncestorOfFocused;
324 private InputMap inputMap_whenInFocusedWindow;
325 private ActionMap actionMap;
326 private InputVerifier inputVerifier;
328 private TransferHandler transferHandler;
331 * A lock held during recursive painting; this is used to serialize
332 * access to the double buffer, and also to select the "top level"
333 * object which should acquire the double buffer in a given widget
334 * tree (which may have multiple double buffered children).
336 * @see #doubleBuffered
339 private static final Object paintLock = new Object();
343 * The default locale of the component.
345 * @see #getDefaultLocale
346 * @see #setDefaultLocale
348 private static Locale defaultLocale;
350 public static final String TOOL_TIP_TEXT_KEY = "ToolTipText";
353 * Constant used to indicate that no condition has been assigned to a
356 * @see #registerKeyboardAction
358 public static final int UNDEFINED_CONDITION = -1;
361 * Constant used to indicate that an action should be performed only when
362 * the component has focus.
364 * @see #registerKeyboardAction
366 public static final int WHEN_FOCUSED = 0;
369 * Constant used to indicate that an action should be performed only when
370 * the component is an ancestor of the component which has focus.
372 * @see #registerKeyboardAction
374 public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1;
377 * Constant used to indicate that an action should be performed only when
378 * the component is in the window which has focus.
380 * @see #registerKeyboardAction
382 public static final int WHEN_IN_FOCUSED_WINDOW = 2;
386 * Creates a new <code>JComponent</code> instance.
391 super.setLayout(new FlowLayout());
392 defaultLocale = Locale.getDefault();
393 debugGraphicsOptions = DebugGraphics.NONE_OPTION;
397 * Helper to lazily construct and return the client properties table.
399 * @return The current client properties table
401 * @see #clientProperties
402 * @see #getClientProperty
403 * @see #putClientProperty
405 private Hashtable getClientProperties()
407 if (clientProperties == null)
408 clientProperties = new Hashtable();
409 return clientProperties;
413 * Get a client property associated with this component and a particular
416 * @param key The key with which to look up the client property
418 * @return A client property associated with this object and key
420 * @see #clientProperties
421 * @see #getClientProperties
422 * @see #putClientProperty
424 public Object getClientProperty(Object key)
426 return getClientProperties().get(key);
430 * Add a client property <code>value</code> to this component, associated
431 * with <code>key</code>. If there is an existing client property
432 * associated with <code>key</code>, it will be replaced.
434 * @param key The key of the client property association to add
435 * @param value The value of the client property association to add
437 * @see #clientProperties
438 * @see #getClientProperties
439 * @see #getClientProperty
441 public void putClientProperty(Object key, Object value)
443 getClientProperties().put(key, value);
447 * Unregister an <code>AncestorListener</code>.
449 * @param listener The listener to unregister
451 * @see addAncestorListener
453 public void removeAncestorListener(AncestorListener listener)
455 listenerList.remove(AncestorListener.class, listener);
459 * Unregister a <code>PropertyChangeListener</code>.
461 * @param listener The listener to register
463 * @see #addPropertyChangeListener
464 * @see #changeSupport
466 public void removePropertyChangeListener(PropertyChangeListener listener)
468 if (changeSupport != null)
469 changeSupport.removePropertyChangeListener(listener);
473 * Unregister a <code>PropertyChangeListener</code>.
475 * @param propertyName The property name to unregister the listener from
476 * @param listener The listener to unregister
478 * @see #addPropertyChangeListener
479 * @see #changeSupport
481 public void removePropertyChangeListener(String propertyName,
482 PropertyChangeListener listener)
484 if (changeSupport != null)
485 changeSupport.removePropertyChangeListener(propertyName, listener);
489 * Unregister a <code>VetoableChangeChangeListener</code>.
491 * @param listener The listener to unregister
493 * @see #addVetoableChangeListener
495 public void removeVetoableChangeListener(VetoableChangeListener listener)
497 listenerList.remove(VetoableChangeListener.class, listener);
501 * Register an <code>AncestorListener</code>.
503 * @param listener The listener to register
505 * @see #removeVetoableChangeListener
507 public void addAncestorListener(AncestorListener listener)
509 listenerList.add(AncestorListener.class, listener);
513 * Register a <code>PropertyChangeListener</code>. This listener will
514 * receive any PropertyChangeEvent, regardless of property name. To
515 * listen to a specific property name, use {@link
516 * #addPropertyChangeListener(String,PropertyChangeListener)} instead.
518 * @param listener The listener to register
520 * @see #removePropertyChangeListener
521 * @see #changeSupport
523 public void addPropertyChangeListener(PropertyChangeListener listener)
525 if (changeSupport == null)
526 changeSupport = new SwingPropertyChangeSupport(this);
527 changeSupport.addPropertyChangeListener(listener);
531 * Register a <code>PropertyChangeListener</code> for a specific, named
532 * property. To listen to all property changes, regardless of name, use
533 * {@link #addPropertyChangeListener(PropertyChangeListener)} instead.
535 * @param propertyName The property name to listen to
536 * @param listener The listener to register
538 * @see #removePropertyChangeListener
539 * @see #changeSupport
541 public void addPropertyChangeListener(String propertyName,
542 PropertyChangeListener listener)
544 listenerList.add(PropertyChangeListener.class, listener);
548 * Register a <code>VetoableChangeListener</code>.
550 * @param listener The listener to register
552 * @see #removeVetoableChangeListener
555 public void addVetoableChangeListener(VetoableChangeListener listener)
557 listenerList.add(VetoableChangeListener.class, listener);
561 * Return all registered listeners of a particular type.
563 * @param listenerType The type of listener to return
565 * @return All listeners in the {@link #listenerList} which
566 * are of the specified type
570 public EventListener[] getListeners(Class listenerType)
572 return listenerList.getListeners(listenerType);
576 * Return all registered <code>AncestorListener</code> objects.
578 * @return The set of <code>AncestorListener</code> objects in {@link
581 public AncestorListener[] getAncestorListeners()
583 return (AncestorListener[]) getListeners(AncestorListener.class);
587 * Return all registered <code>VetoableChangeListener</code> objects.
589 * @return The set of <code>VetoableChangeListener</code> objects in {@link
592 public VetoableChangeListener[] getVetoableChangeListeners()
594 return (VetoableChangeListener[]) getListeners(VetoableChangeListener.class);
598 * Return all <code>PropertyChangeListener</code> objects registered to listen
599 * for a particular property.
601 * @param property The property to return the listeners of
603 * @return The set of <code>PropertyChangeListener</code> objects in
604 * {@link #changeSupport} registered to listen on the specified propert
606 public PropertyChangeListener[] getPropertyChangeListeners(String property)
608 return changeSupport == null ? new PropertyChangeListener[0]
609 : changeSupport.getPropertyChangeListeners(property);
613 * A variant of {@link #firePropertyChange(String,Object,Object)}
614 * for properties with <code>boolean</code> values.
616 public void firePropertyChange(String propertyName, boolean oldValue,
619 if (changeSupport != null)
620 changeSupport.firePropertyChange(propertyName, Boolean.valueOf(oldValue),
621 Boolean.valueOf(newValue));
625 * A variant of {@link #firePropertyChange(String,Object,Object)}
626 * for properties with <code>byte</code> values.
628 public void firePropertyChange(String propertyName, byte oldValue,
631 if (changeSupport != null)
632 changeSupport.firePropertyChange(propertyName, new Byte(oldValue),
637 * A variant of {@link #firePropertyChange(String,Object,Object)}
638 * for properties with <code>char</code> values.
640 public void firePropertyChange(String propertyName, char oldValue,
643 if (changeSupport != null)
644 changeSupport.firePropertyChange(propertyName, new Character(oldValue),
645 new Character(newValue));
649 * A variant of {@link #firePropertyChange(String,Object,Object)}
650 * for properties with <code>double</code> values.
652 public void firePropertyChange(String propertyName, double oldValue,
655 if (changeSupport != null)
656 changeSupport.firePropertyChange(propertyName, new Double(oldValue),
657 new Double(newValue));
661 * A variant of {@link #firePropertyChange(String,Object,Object)}
662 * for properties with <code>float</code> values.
664 public void firePropertyChange(String propertyName, float oldValue,
667 if (changeSupport != null)
668 changeSupport.firePropertyChange(propertyName, new Float(oldValue),
669 new Float(newValue));
673 * A variant of {@link #firePropertyChange(String,Object,Object)}
674 * for properties with <code>int</code> values.
676 public void firePropertyChange(String propertyName, int oldValue,
679 if (changeSupport != null)
680 changeSupport.firePropertyChange(propertyName, new Integer(oldValue),
681 new Integer(newValue));
685 * A variant of {@link #firePropertyChange(String,Object,Object)}
686 * for properties with <code>long</code> values.
688 public void firePropertyChange(String propertyName, long oldValue,
691 if (changeSupport != null)
692 changeSupport.firePropertyChange(propertyName, new Long(oldValue),
697 * Call {@link PropertyChangeListener#propertyChange} on all listeners
698 * registered to listen to a given property. Any method which changes
699 * the specified property of this component should call this method.
701 * @param propertyName The property which changed
702 * @param oldValue The old value of the property
703 * @param newValue The new value of the property
705 * @see #changeSupport
706 * @see #addPropertyChangeListener
707 * @see #removePropertyChangeListener
709 protected void firePropertyChange(String propertyName, Object oldValue,
712 if (changeSupport != null)
713 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
717 * A variant of {@link #firePropertyChange(String,Object,Object)}
718 * for properties with <code>short</code> values.
720 public void firePropertyChange(String propertyName, short oldValue,
723 if (changeSupport != null)
724 changeSupport.firePropertyChange(propertyName, new Short(oldValue),
725 new Short(newValue));
729 * Call {@link VetoableChangeListener#vetoableChange} on all listeners
730 * registered to listen to a given property. Any method which changes
731 * the specified property of this component should call this method.
733 * @param propertyName The property which changed
734 * @param oldValue The old value of the property
735 * @param newValue The new value of the property
737 * @throws PropertyVetoException if the change was vetoed by a listener
739 * @see addVetoableChangeListener
740 * @see removeVetoableChangeListener
742 protected void fireVetoableChange(String propertyName, Object oldValue,
744 throws PropertyVetoException
746 VetoableChangeListener[] listeners = getVetoableChangeListeners();
748 PropertyChangeEvent evt = new PropertyChangeEvent(this, propertyName, oldValue, newValue);
750 for (int i = 0; i < listeners.length; i++)
751 listeners[i].vetoableChange(evt);
755 * Get the value of the accessibleContext property for this component.
757 * @return the current value of the property
759 public AccessibleContext getAccessibleContext()
766 * Get the value of the {@link #alignmentX} property.
768 * @return The current value of the property.
770 * @see #setAlignmentX
773 public float getAlignmentX()
779 * Get the value of the {@link #alignmentY} property.
781 * @return The current value of the property.
783 * @see #setAlignmentY
786 public float getAlignmentY()
792 * Get the current value of the {@link #autoscrolls} property.
794 * @return The current value of the property
796 public boolean getAutoscrolls()
802 * Set the value of the {@link #border} property, revalidate
803 * and repaint this component.
805 * @param newBorder The new value of the property
809 public void setBorder(Border newBorder)
811 Border oldBorder = border;
813 firePropertyChange("border", oldBorder, newBorder);
819 * Get the value of the {@link #border} property.
821 * @return The property's current value
825 public Border getBorder()
831 * Get the component's current bounding box. If a rectangle is provided,
832 * use this as the return value (adjusting its fields in place);
833 * otherwise (of <code>null</code> is provided) return a new {@link
836 * @param rv Optional return value to use
838 * @return A rectangle bounding the component
840 public Rectangle getBounds(Rectangle rv)
843 return new Rectangle(getX(), getY(), getWidth(), getHeight());
846 rv.setBounds(getX(), getY(), getWidth(), getHeight());
852 * Prepares a graphics context for painting this object. If {@link
853 * #debugGraphicsOptions} is not equal to {@link
854 * DebugGraphics#NONE_OPTION}, produce a new {@link DebugGraphics} object
855 * wrapping the parameter. Otherwise configure the parameter with this
856 * component's foreground color and font.
858 * @param g The graphics context to wrap or configure
860 * @return A graphics context to paint this object with
862 * @see #debugGraphicsOptions
865 protected Graphics getComponentGraphics(Graphics g)
867 Graphics g2 = g.create();
868 g2.setFont(this.getFont());
869 g2.setColor(this.getForeground());
875 * Get the value of the {@link #debugGraphicsOptions} property.
877 * @return The current value of the property.
879 * @see #setDebugGraphicsOptions
880 * @see #debugGraphicsOptions
882 public int getDebugGraphicsOptions()
888 * Get the component's insets, which are calculated from
889 * the {@link #border} property. If the border is <code>null</code>,
890 * calls {@link Container#getInsets}.
892 * @return The component's current insets
894 public Insets getInsets()
897 return super.getInsets();
898 return getBorder().getBorderInsets(this);
902 * Get the component's insets, which are calculated from the {@link
903 * #border} property. If the border is <code>null</code>, calls {@link
904 * Container#getInsets}. The passed-in {@link Insets} value will be
905 * used as the return value, if possible.
907 * @param insets Return value object to reuse, if possible
909 * @return The component's current insets
911 public Insets getInsets(Insets insets)
913 Insets t = getInsets();
918 insets.left = t.left;
919 insets.right = t.right;
921 insets.bottom = t.bottom;
926 * Get the component's location. The passed-in {@link Point} value
927 * will be used as the return value, if possible.
929 * @param rv Return value object to reuse, if possible
931 * @return The component's current location
933 public Point getLocation(Point rv)
936 return new Point(getX(), getY());
938 rv.setLocation(getX(), getY());
943 * Get the component's maximum size. If the {@link #maximumSize} property
944 * has been explicitly set, it is returned. If the {@link #maximumSize}
945 * property has not been set but the {@link ui} property has been, the
946 * result of {@link ComponentUI#getMaximumSize} is returned. If neither
947 * property has been set, the result of {@link Container#getMaximumSize}
950 * @return The maximum size of the component
953 * @see #setMaximumSize
955 public Dimension getMaximumSize()
957 if (maximumSize != null)
962 Dimension s = ui.getMaximumSize(this);
967 Dimension p = super.getMaximumSize();
972 * Get the component's minimum size. If the {@link #minimumSize} property
973 * has been explicitly set, it is returned. If the {@link #minimumSize}
974 * property has not been set but the {@link ui} property has been, the
975 * result of {@link ComponentUI#getMinimumSize} is returned. If neither
976 * property has been set, the result of {@link Container#getMinimumSize}
979 * @return The minimum size of the component
982 * @see #setMinimumSize
984 public Dimension getMinimumSize()
986 if (minimumSize != null)
991 Dimension s = ui.getMinimumSize(this);
996 Dimension p = super.getMinimumSize();
1001 * Get the component's preferred size. If the {@link #preferredSize}
1002 * property has been explicitly set, it is returned. If the {@link
1003 * #preferredSize} property has not been set but the {@link ui} property
1004 * has been, the result of {@link ComponentUI#getPreferredSize} is
1005 * returned. If neither property has been set, the result of {@link
1006 * Container#getPreferredSize} is returned.
1008 * @return The preferred size of the component
1010 * @see #preferredSize
1011 * @see #setPreferredSize
1013 public Dimension getPreferredSize()
1015 if (preferredSize != null)
1016 return preferredSize;
1020 Dimension s = ui.getPreferredSize(this);
1024 Dimension p = super.getPreferredSize();
1029 * Checks if a maximum size was explicitely set on the component.
1031 * @return <code>true</code> if a maximum size was set,
1032 * <code>false</code> otherwise
1036 public boolean isMaximumSizeSet()
1038 return maximumSize != null;
1042 * Checks if a minimum size was explicitely set on the component.
1044 * @return <code>true</code> if a minimum size was set,
1045 * <code>false</code> otherwise
1049 public boolean isMinimumSizeSet()
1051 return minimumSize != null;
1055 * Checks if a preferred size was explicitely set on the component.
1057 * @return <code>true</code> if a preferred size was set,
1058 * <code>false</code> otherwise
1062 public boolean isPreferredSizeSet()
1064 return preferredSize != null;
1068 * Return the value of the {@link #nextFocusableComponent} property.
1070 * @return The current value of the property, or <code>null</code>
1071 * if none has been set.
1073 * @deprecated See {@link java.awt.FocusTraversalPolicy}
1075 public Component getNextFocusableComponent()
1081 * Return the set of {@link KeyStroke} objects which are registered
1082 * to initiate actions on this component.
1084 * @return An array of the registered keystrokes
1086 public KeyStroke[] getRegisteredKeyStrokes()
1092 * Returns the first ancestor of this component which is a {@link JRootPane}.
1093 * Equivalent to calling <code>SwingUtilities.getRootPane(this);</code>.
1095 * @return An ancestral JRootPane, or <code>null</code> if none exists.
1097 public JRootPane getRootPane()
1099 JRootPane p = SwingUtilities.getRootPane(this);
1104 * Get the component's size. The passed-in {@link Dimension} value
1105 * will be used as the return value, if possible.
1107 * @param rv Return value object to reuse, if possible
1109 * @return The component's current size
1111 public Dimension getSize(Dimension rv)
1114 return new Dimension(getWidth(), getHeight());
1117 rv.setSize(getWidth(), getHeight());
1123 * Return the {@link #toolTip} property of this component, creating it and
1124 * setting it if it is currently <code>null</code>. This method can be
1125 * overridden in subclasses which wish to control the exact form of
1128 * @return The current toolTip
1130 public JToolTip createToolTip()
1132 JToolTip toolTip = new JToolTip();
1133 toolTip.setComponent(this);
1134 toolTip.setTipText(toolTipText);
1140 * Return the location at which the {@link #toolTip} property should be
1141 * displayed, when triggered by a particular mouse event.
1143 * @param event The event the tooltip is being presented in response to
1145 * @return The point at which to display a tooltip, or <code>null</code>
1146 * if swing is to choose a default location.
1148 public Point getToolTipLocation(MouseEvent event)
1154 * Set the value of the {@link #toolTipText} property.
1156 * @param text The new property value
1158 * @see #getToolTipText
1160 public void setToolTipText(String text)
1164 ToolTipManager.sharedInstance().unregisterComponent(this);
1169 // XXX: The tip text doesn't get updated unless you set it to null
1170 // and then to something not-null. This is consistent with the behaviour
1171 // of Sun's ToolTipManager.
1173 String oldText = toolTipText;
1176 if (oldText == null)
1177 ToolTipManager.sharedInstance().registerComponent(this);
1181 * Get the value of the {@link #toolTipText} property.
1183 * @return The current property value
1185 * @see #setToolTipText
1187 public String getToolTipText()
1193 * Get the value of the {@link #toolTipText} property, in response to a
1194 * particular mouse event.
1196 * @param event The mouse event which triggered the tooltip
1198 * @return The current property value
1200 * @see #setToolTipText
1202 public String getToolTipText(MouseEvent event)
1204 return getToolTipText();
1208 * Return the top level ancestral container (usually a {@link
1209 * java.awt.Window} or {@link java.awt.Applet}) which this component is
1210 * contained within, or <code>null</code> if no ancestors exist.
1212 * @return The top level container, if it exists
1214 public Container getTopLevelAncestor()
1216 Container c = getParent();
1217 for (Container peek = c; peek != null; peek = peek.getParent())
1223 * Compute the component's visible rectangle, which is defined
1224 * recursively as either the component's bounds, if it has no parent, or
1225 * the intersection of the component's bounds with the visible rectangle
1228 * @param rect The return value slot to place the visible rectangle in
1230 public void computeVisibleRect(Rectangle rect)
1232 Component c = getParent();
1233 if (c != null && c instanceof JComponent)
1235 ((JComponent) c).computeVisibleRect(rect);
1236 rect.translate(-getX(), -getY());
1237 Rectangle2D.intersect(rect,
1238 new Rectangle(0, 0, getWidth(), getHeight()),
1242 rect.setRect(0, 0, getWidth(), getHeight());
1246 * Return the component's visible rectangle in a new {@link Rectangle},
1247 * rather than via a return slot.
1249 * @return The component's visible rectangle
1251 * @see #computeVisibleRect(Rectangle)
1253 public Rectangle getVisibleRect()
1255 Rectangle r = new Rectangle();
1256 computeVisibleRect(r);
1261 * <p>Requests that this component receive input focus, giving window
1262 * focus to the top level ancestor of this component. Only works on
1263 * displayable, focusable, visible components.</p>
1265 * <p>This method should not be called by clients; it is intended for
1266 * focus implementations. Use {@link Component#requestFocus} instead.</p>
1268 * @see {@link Component#requestFocus}
1270 public void grabFocus()
1275 * Get the value of the {@link #doubleBuffered} property.
1277 * @return The property's current value
1279 public boolean isDoubleBuffered()
1281 return doubleBuffered;
1285 * Return <code>true</code> if the provided component has no native peer;
1286 * in other words, if it is a "lightweight component".
1288 * @param c The component to test for lightweight-ness
1290 * @return Whether or not the component is lightweight
1292 public static boolean isLightweightComponent(Component c)
1294 return c.getPeer() instanceof LightweightPeer;
1298 * Return <code>true<code> if you wish this component to manage its own
1299 * focus. In particular: if you want this component to be sent
1300 * <code>TAB</code> and <code>SHIFT+TAB</code> key events, and to not
1301 * have its children considered as focus transfer targets. If
1302 * <code>true</code>, focus traversal around this component changes to
1303 * <code>CTRL+TAB</code> and <code>CTRL+SHIFT+TAB</code>.
1305 * @return <code>true</code> if you want this component to manage its own
1306 * focus, otherwise (by default) <code>false</code>
1308 * @deprecated 1.4 Use {@link Component.setFocusTraversalKeys(int,Set)} and
1309 * {@link Container.setFocusCycleRoot(boolean)} instead
1311 public boolean isManagingFocus()
1317 * Return the current value of the {@link opaque} property.
1319 * @return The current property value
1321 public boolean isOpaque()
1327 * Return <code>true</code> if the component can guarantee that none of its
1328 * children will overlap in Z-order. This is a hint to the painting system.
1329 * The default is to return <code>true</code>, but some components such as
1330 * {@link JLayeredPane} should override this to return <code>false</code>.
1332 * @return Whether the component tiles its children
1334 public boolean isOptimizedDrawingEnabled()
1340 * Return <code>true</code> if this component is currently painting a tile.
1342 * @return Whether the component is painting a tile
1344 public boolean isPaintingTile()
1350 * Get the value of the {@link #requestFocusEnabled} property.
1352 * @return The current value of the property
1354 public boolean isRequestFocusEnabled()
1356 return requestFocusEnabled;
1360 * Return <code>true</code> if this component is a validation root; this
1361 * will cause calls to {@link #invalidate} in this component's children
1362 * to be "captured" at this component, and not propagate to its parents.
1363 * For most components this should return <code>false</code>, but some
1364 * components such as {@link JViewPort} will want to return
1365 * <code>true</code>.
1367 * @return Whether this component is a validation root
1369 public boolean isValidateRoot()
1375 * <p>Paint the component. This is a delicate process, and should only be
1376 * called from the repaint thread, under control of the {@link
1377 * RepaintManager}. Client code should usually call {@link #repaint} to
1378 * trigger painting.</p>
1380 * <p>This method will acquire a double buffer from the {@link
1381 * RepaintManager} if the component's {@link #doubleBuffered} property is
1382 * <code>true</code> and the <code>paint</code> call is the
1383 * <em>first</em> recursive <code>paint</code> call inside swing.</p>
1385 * <p>The method will also modify the provided {@link Graphics} context
1386 * via the {@link #getComponentGraphics} method. If you want to customize
1387 * the graphics object used for painting, you should override that method
1388 * rather than <code>paint</code>.</p>
1390 * <p>The body of the <code>paint</code> call involves calling {@link
1391 * #paintComponent}, {@link #paintBorder}, and {@link #paintChildren} in
1392 * order. If you want to customize painting behavior, you should override
1393 * one of these methods rather than <code>paint</code>.</p>
1395 * <p>For more details on the painting sequence, see <a
1396 * href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">this
1399 * @param g The graphics context to paint with
1401 * @see #paintImmediately
1403 public void paint(Graphics g)
1406 Image doubleBuffer = null;
1407 RepaintManager rm = RepaintManager.currentManager(this);
1409 if (isDoubleBuffered()
1410 && (rm.isDoubleBufferingEnabled())
1411 && (! Thread.holdsLock(paintLock)))
1413 doubleBuffer = rm.getOffscreenBuffer(this, getWidth(), getHeight());
1416 synchronized (paintLock)
1418 if (doubleBuffer != null)
1420 g2 = doubleBuffer.getGraphics();
1421 g2.setClip(g.getClipBounds());
1424 g2 = getComponentGraphics(g2);
1429 if (doubleBuffer != null)
1430 g.drawImage(doubleBuffer, 0, 0, (ImageObserver) null);
1435 * Paint the component's border. This usually means calling {@link
1436 * Border#paintBorder} on the {@link #border} property, if it is
1437 * non-<code>null</code>. You may override this if you wish to customize
1438 * border painting behavior. The border is painted after the component's
1439 * body, but before the component's children.
1441 * @param g The graphics context with which to paint the border
1444 * @see #paintChildren
1445 * @see #paintComponent
1447 protected void paintBorder(Graphics g)
1449 if (getBorder() != null)
1450 getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
1454 * Paint the component's children. This usually means calling {@link
1455 * Container#paint}, which recursively calls {@link #paint} on any of the
1456 * component's children, with appropriate changes to coordinate space and
1457 * clipping region. You may override this if you wish to customize
1458 * children painting behavior. The children are painted after the
1459 * component's body and border.
1461 * @param g The graphics context with which to paint the children
1465 * @see #paintComponent
1467 protected void paintChildren(Graphics g)
1473 * Paint the component's body. This usually means calling {@link
1474 * ComponentUI#update} on the {@link #ui} property of the component, if
1475 * it is non-<code>null</code>. You may override this if you wish to
1476 * customize the component's body-painting behavior. The component's body
1477 * is painted first, before the border and children.
1479 * @param g The graphics context with which to paint the body
1483 * @see #paintChildren
1485 protected void paintComponent(Graphics g)
1492 * A variant of {@link #paintImmediately(Rectangle)} which takes
1493 * integer parameters.
1495 * @param x The left x coordinate of the dirty region
1496 * @param y The top y coordinate of the dirty region
1497 * @param w The width of the dirty region
1498 * @param h The height of the dirty region
1500 public void paintImmediately(int x, int y, int w, int h)
1502 paintImmediately(new Rectangle(x, y, w, h));
1506 * Transform the provided dirty rectangle for this component into the
1507 * appropriate ancestral {@link JRootPane} and call {@link #paint} on
1508 * that root pane. This method is called from the {@link RepaintManager}
1509 * and should always be called within the painting thread.
1511 * @param r The dirty rectangle to paint
1513 public void paintImmediately(Rectangle r)
1515 Component root = SwingUtilities.getRoot(this);
1516 if (root == null || ! root.isShowing())
1518 Graphics g = root.getGraphics();
1522 Rectangle clip = SwingUtilities.convertRectangle(this, r, root);
1529 * Return a string representation for this component, for use in
1532 * @return A string describing this component.
1534 protected String paramString()
1536 return "JComponent";
1540 * A variant of {@link
1541 * #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which
1542 * provides <code>null</code> for the command name.
1544 public void registerKeyboardAction(ActionListener act,
1548 registerKeyboardAction(act, null, stroke, cond);
1552 * There is some charmingly undocumented behavior sun seems to be using
1553 * to simulate the old register/unregister keyboard binding API. It's not
1554 * clear to me why this matters, but we shall endeavour to follow suit.
1556 * Two main thing seem to be happening when you do registerKeyboardAction():
1558 * - no actionMap() entry gets created, just an entry in inputMap()
1560 * - the inputMap() entry is a proxy class which invokes the the
1561 * binding's actionListener as a target, and which clobbers the command
1562 * name sent in the ActionEvent, providing the binding command name
1565 * This much you can work out just by asking the input and action maps
1566 * what they contain after making bindings, and watching the event which
1567 * gets delivered to the recipient. Beyond that, it seems to be a
1568 * sun-private solution so I will only immitate it as much as it matters
1569 * to external observers.
1572 private static class ActionListenerProxy
1573 extends AbstractAction
1575 ActionListener target;
1576 String bindingCommandName;
1578 public ActionListenerProxy(ActionListener li,
1582 bindingCommandName = cmd;
1585 public void actionPerformed(ActionEvent e)
1587 ActionEvent derivedEvent = new ActionEvent(e.getSource(),
1591 target.actionPerformed(derivedEvent);
1597 * An obsolete method to register a keyboard action on this component.
1598 * You should use <code>getInputMap</code> and <code>getActionMap</code>
1599 * to fetch mapping tables from keystrokes to commands, and commands to
1600 * actions, respectively, and modify those mappings directly.
1602 * @param anAction The action to be registered
1603 * @param aCommand The command to deliver in the delivered {@link
1604 * java.awt.ActionEvent}
1605 * @param aKeyStroke The keystroke to register on
1606 * @param aCondition One of the values {@link #UNDEFINED_CONDITION},
1607 * {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or
1608 * {@link #WHEN_IN_FOCUSED_WINDOW}, indicating the condition which must
1609 * be met for the action to be fired
1611 * @see #unregisterKeyboardAction
1612 * @see #getConditionForKeystroke
1613 * @see #resetKeyboardActiond
1615 public void registerKeyboardAction(ActionListener act,
1620 getInputMap(cond).put(stroke, new ActionListenerProxy(act, cmd));
1625 public final void setInputMap(int condition, InputMap map)
1627 enableEvents(AWTEvent.KEY_EVENT_MASK);
1631 inputMap_whenFocused = map;
1634 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
1635 inputMap_whenAncestorOfFocused = map;
1638 case WHEN_IN_FOCUSED_WINDOW:
1639 inputMap_whenInFocusedWindow = map;
1642 case UNDEFINED_CONDITION:
1644 throw new IllegalArgumentException();
1648 public final InputMap getInputMap(int condition)
1650 enableEvents(AWTEvent.KEY_EVENT_MASK);
1654 if (inputMap_whenFocused == null)
1655 inputMap_whenFocused = new InputMap();
1656 return inputMap_whenFocused;
1658 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
1659 if (inputMap_whenAncestorOfFocused == null)
1660 inputMap_whenAncestorOfFocused = new InputMap();
1661 return inputMap_whenAncestorOfFocused;
1663 case WHEN_IN_FOCUSED_WINDOW:
1664 if (inputMap_whenInFocusedWindow == null)
1665 inputMap_whenInFocusedWindow = new InputMap();
1666 return inputMap_whenInFocusedWindow;
1668 case UNDEFINED_CONDITION:
1674 public final InputMap getInputMap()
1676 return getInputMap(WHEN_FOCUSED);
1679 public final ActionMap getActionMap()
1681 if (actionMap == null)
1682 actionMap = new ActionMap();
1686 public final void setActionMap(ActionMap map)
1692 * Return the condition that determines whether a registered action
1693 * occurs in response to the specified keystroke.
1695 * @param aKeyStroke The keystroke to return the condition of
1697 * @return One of the values {@link #UNDEFINED_CONDITION}, {@link
1698 * #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or {@link
1699 * #WHEN_IN_FOCUSED_WINDOW}
1701 * @deprecated As of 1.3 KeyStrokes can be registered with multiple
1702 * simultaneous conditions.
1704 * @see #registerKeyboardAction
1705 * @see #unregisterKeyboardAction
1706 * @see #resetKeyboardActiond
1708 public int getConditionForKeyStroke(KeyStroke ks)
1710 if (inputMap_whenFocused != null
1711 && inputMap_whenFocused.get(ks) != null)
1712 return WHEN_FOCUSED;
1713 else if (inputMap_whenAncestorOfFocused != null
1714 && inputMap_whenAncestorOfFocused.get(ks) != null)
1715 return WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
1716 else if (inputMap_whenInFocusedWindow != null
1717 && inputMap_whenInFocusedWindow.get(ks) != null)
1718 return WHEN_IN_FOCUSED_WINDOW;
1720 return UNDEFINED_CONDITION;
1724 * Get the ActionListener (typically an {@link Action} object) which is
1725 * associated with a particular keystroke.
1727 * @param aKeyStroke The keystroke to retrieve the action of
1729 * @return The action associated with the specified keystroke
1731 * @deprecated Use {@link #getActionMap()}
1733 public ActionListener getActionForKeyStroke(KeyStroke ks)
1735 Object cmd = getInputMap().get(ks);
1738 if (cmd instanceof ActionListenerProxy)
1739 return (ActionListenerProxy) cmd;
1740 else if (cmd instanceof String)
1741 return getActionMap().get(cmd);
1747 * A hook for subclasses which want to customize event processing.
1749 protected void processComponentKeyEvent(KeyEvent e)
1754 * Override the default key dispatch system from Component to hook into
1755 * the swing {@link InputMap} / {@link ActionMap} system.
1758 * href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">this
1759 * report</a> for more details, it's somewhat complex.
1761 protected void processKeyEvent(KeyEvent e)
1763 processComponentKeyEvent(e);
1765 // FIXME: this needs to be elaborated significantly, to do all the
1766 // focus / ancestor / window searching for the various binding modes.
1767 if (! e.isConsumed() &&
1768 processKeyBinding(KeyStroke.getKeyStrokeForEvent(e),
1769 e, WHEN_FOCUSED, e.getID() == KeyEvent.KEY_PRESSED))
1773 protected boolean processKeyBinding(KeyStroke ks,
1781 InputMap map = getInputMap(condition);
1784 Object cmd = map.get(ks);
1787 if (cmd instanceof ActionListenerProxy)
1790 act = (Action) getActionMap().get(cmd);
1793 if (act != null && act.isEnabled())
1794 return SwingUtilities.notifyAction(act, ks, e, this, e.getModifiers());
1800 * Remove a keyboard action registry.
1802 * @param stroke The keystroke to unregister
1804 * @see #registerKeyboardAction
1805 * @see #getConditionForKeystroke
1806 * @see #resetKeyboardActiond
1808 public void unregisterKeyboardAction(KeyStroke aKeyStroke)
1814 * Reset all keyboard action registries.
1816 * @see #registerKeyboardAction
1817 * @see #unregisterKeyboardAction
1818 * @see #getConditionForKeystroke
1820 public void resetKeyboardActions()
1822 if (inputMap_whenFocused != null)
1823 inputMap_whenFocused.clear();
1824 if (inputMap_whenAncestorOfFocused != null)
1825 inputMap_whenAncestorOfFocused.clear();
1826 if (inputMap_whenInFocusedWindow != null)
1827 inputMap_whenInFocusedWindow.clear();
1828 if (actionMap != null)
1834 * Mark the described region of this component as dirty in the current
1835 * {@link RepaintManager}. This will queue an asynchronous repaint using
1836 * the system painting thread in the near future.
1839 * @param x coordinate of the region to mark as dirty
1840 * @param y coordinate of the region to mark as dirty
1841 * @param width dimension of the region to mark as dirty
1842 * @param height dimension of the region to mark as dirty
1844 public void repaint(long tm, int x, int y, int width, int height)
1846 Rectangle dirty = new Rectangle(x, y, width, height);
1847 Rectangle vis = getVisibleRect();
1848 dirty = dirty.intersection(vis);
1849 RepaintManager.currentManager(this).addDirtyRegion(this, dirty.x, dirty.y,
1855 * Mark the described region of this component as dirty in the current
1856 * {@link RepaintManager}. This will queue an asynchronous repaint using
1857 * the system painting thread in the near future.
1859 * @param r The rectangle to mark as dirty
1861 public void repaint(Rectangle r)
1863 repaint((long) 0, (int) r.getX(), (int) r.getY(), (int) r.getWidth(),
1864 (int) r.getHeight());
1868 * Request focus on the default component of this component's {@link
1869 * FocusTraversalPolicy}.
1871 * @return The result of {@link #requestFocus}
1873 * @deprecated Use {@link #requestFocus()} on the default component provided from
1874 * the {@link FocusTraversalPolicy} instead.
1876 public boolean requestDefaultFocus()
1882 * Queue a an invalidation and revalidation of this component, using
1883 * {@link RepaintManager#addInvalidComponent}.
1885 public void revalidate()
1888 RepaintManager.currentManager(this).addInvalidComponent(this);
1892 * Calls <code>scrollRectToVisible</code> on the component's parent.
1893 * Components which can service this call should override.
1895 * @param r The rectangle to make visible
1897 public void scrollRectToVisible(Rectangle r)
1899 Component p = getParent();
1900 if (p instanceof JComponent)
1901 ((JComponent) p).scrollRectToVisible(r);
1905 * Set the value of the {@link #alignmentX} property.
1907 * @param a The new value of the property
1909 public void setAlignmentX(float a)
1915 * Set the value of the {@link #alignmentY} property.
1917 * @param a The new value of the property
1919 public void setAlignmentY(float a)
1925 * Set the value of the {@link #autoscrolls} property.
1927 * @param a The new value of the property
1929 public void setAutoscrolls(boolean a)
1935 * Set the value of the {@link #debugGraphicsOptions} property.
1937 * @param debugOptions The new value of the property
1939 public void setDebugGraphicsOptions(int debugOptions)
1941 debugGraphicsOptions = debugOptions;
1945 * Set the value of the {@link #doubleBuffered} property.
1947 * @param db The new value of the property
1949 public void setDoubleBuffered(boolean db)
1951 doubleBuffered = db;
1955 * Set the value of the {@link #enabled} property, revalidate
1956 * and repaint this component.
1958 * @param enable The new value of the property
1960 public void setEnabled(boolean enable)
1962 boolean oldEnabled = isEnabled();
1963 super.setEnabled(enable);
1964 firePropertyChange("enabeld", oldEnabled, enable);
1970 * Set the value of the {@link #font} property, revalidate
1971 * and repaint this component.
1973 * @param f The new value of the property
1975 public void setFont(Font f)
1983 * Set the value of the {@link #background} property, revalidate
1984 * and repaint this component.
1986 * @param bg The new value of the property
1988 public void setBackground(Color bg)
1990 super.setBackground(bg);
1996 * Set the value of the {@link #foreground} property, revalidate
1997 * and repaint this component.
1999 * @param fg The new value of the property
2001 public void setForeground(Color fg)
2003 super.setForeground(fg);
2009 * Set the value of the {@link #maximumSize} property, revalidate
2010 * and repaint this component.
2012 * @param max The new value of the property
2014 public void setMaximumSize(Dimension max)
2016 Dimension oldMaximumSize = maximumSize;
2018 firePropertyChange("maximumSize", oldMaximumSize, maximumSize);
2024 * Set the value of the {@link #minimumSize} property, revalidate
2025 * and repaint this component.
2027 * @param min The new value of the property
2029 public void setMinimumSize(Dimension min)
2031 Dimension oldMinimumSize = minimumSize;
2033 firePropertyChange("minimumSize", oldMinimumSize, minimumSize);
2039 * Set the value of the {@link #preferredSize} property, revalidate
2040 * and repaint this component.
2042 * @param pref The new value of the property
2044 public void setPreferredSize(Dimension pref)
2046 Dimension oldPreferredSize = preferredSize;
2047 preferredSize = pref;
2048 firePropertyChange("preferredSize", oldPreferredSize, preferredSize);
2052 * Set the specified component to be the next component in the
2053 * focus cycle, overriding the {@link FocusTraversalPolicy} for
2056 * @param aComponent The component to set as the next focusable
2058 * @deprecated Use FocusTraversalPolicy instead
2060 public void setNextFocusableComponent(Component aComponent)
2065 * Set the value of the {@link #requestFocusEnabled} property.
2067 * @param e The new value of the property
2069 public void setRequestFocusEnabled(boolean e)
2071 requestFocusEnabled = e;
2075 * Get the value of the {@link #transferHandler} property.
2077 * @return The current value of the property
2079 * @see ComponentUI#setTransferHandler
2082 public TransferHandler getTransferHandler()
2084 return transferHandler;
2088 * Set the value of the {@link #transferHandler} property.
2090 * @param newHandler The new value of the property
2092 * @see ComponentUI#getTransferHandler
2095 void setTransferHandler (TransferHandler newHandler)
2097 transferHandler = newHandler;
2101 * Set the value of the {@link #opaque} property, revalidate and repaint
2104 * @param isOpaque The new value of the property
2106 * @see ComponentUI#update
2108 public void setOpaque(boolean isOpaque)
2110 boolean oldOpaque = opaque;
2112 firePropertyChange("opaque", oldOpaque, opaque);
2118 * Set the value of the visible property, and revalidate / repaint the
2121 * @param v The new value of the property
2123 public void setVisible(boolean v)
2125 super.setVisible(v);
2131 * Call {@link paint}.
2133 * @param g The graphics context to paint into
2135 public void update(Graphics g)
2141 * Get the value of the UIClassID property. This property should be a key
2142 * in the {@link UIDefaults} table managed by {@link UIManager}, the
2143 * value of which is the name of a class to load for the component's
2144 * {@link ui} property.
2146 * @return A "symbolic" name which will map to a class to use for the
2147 * component's UI, such as <code>"ComponentUI"</code>
2152 public String getUIClassID()
2154 return "ComponentUI";
2158 * Install a new UI delegate as the component's {@link ui} property. In
2159 * the process, this will call {@link ComponentUI.uninstallUI} on any
2160 * existing value for the {@link ui} property, and {@link
2161 * ComponentUI.installUI} on the new UI delegate.
2163 * @param newUI The new UI delegate to install
2166 * @see #getUIClassID
2168 protected void setUI(ComponentUI newUI)
2171 ui.uninstallUI(this);
2173 ComponentUI oldUI = ui;
2179 firePropertyChange("UI", oldUI, newUI);
2186 * This method should be overridden in subclasses. In JComponent, the
2187 * method does nothing. In subclasses, it should a UI delegate
2188 * (corresponding to the symbolic name returned from {@link
2189 * getUIClassID}) from the {@link UIManager}, and calls {@link setUI}
2190 * with the new delegate.
2192 public void updateUI()
2194 System.out.println("update UI not overwritten in class: " + this);
2197 public static Locale getDefaultLocale()
2199 return defaultLocale;
2202 public static void setDefaultLocale(Locale l)
2208 * Returns the currently set input verifier for this component.
2210 * @return the input verifier, or <code>null</code> if none
2212 public InputVerifier getInputVerifier()
2214 return inputVerifier;
2218 * Sets the input verifier to use by this component.
2220 * @param verifier the input verifier, or <code>null</code>
2222 public void setInputVerifier(InputVerifier verifier)
2224 InputVerifier oldVerifier = inputVerifier;
2225 inputVerifier = verifier;
2226 firePropertyChange("inputVerifier", oldVerifier, verifier);