2004-12-31 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / javax / swing / JComponent.java
blob66f0a3aea8ffb0c018baa328ad4d6015505b60d5
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
47 import java.awt.Font;
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.AccessibleKeyBinding;
76 import javax.accessibility.AccessibleRole;
77 import javax.accessibility.AccessibleStateSet;
78 import javax.swing.border.Border;
79 import javax.swing.event.AncestorListener;
80 import javax.swing.event.EventListenerList;
81 import javax.swing.event.SwingPropertyChangeSupport;
82 import javax.swing.plaf.ComponentUI;
84 /**
85 * Every component in swing inherits from this class (JLabel, JButton, etc).
86 * It contains generic methods to manage events, properties and sizes. Actual
87 * drawing of the component is channeled to a look-and-feel class that is
88 * implemented elsewhere.
90 * @author Ronald Veldema (rveldema&064;cs.vu.nl)
91 * @author Graydon Hoare (graydon&064;redhat.com)
93 public abstract class JComponent extends Container implements Serializable
95 private static final long serialVersionUID = -7908749299918704233L;
97 /**
98 * Accessibility support is currently missing.
101 protected AccessibleContext accessibleContext;
103 public abstract class AccessibleJComponent
104 extends AccessibleAWTContainer
106 protected class AccessibleFocusHandler
107 implements FocusListener
109 protected AccessibleFocusHandler(){}
110 public void focusGained(FocusEvent event){}
111 public void focusLost(FocusEvent valevent){}
114 protected class AccessibleContainerHandler
115 implements ContainerListener
117 protected AccessibleContainerHandler() {}
118 public void componentAdded(ContainerEvent event) {}
119 public void componentRemoved(ContainerEvent valevent) {}
122 private static final long serialVersionUID = -7047089700479897799L;
124 protected ContainerListener accessibleContainerHandler;
125 protected FocusListener accessibleFocusHandler;
127 protected AccessibleJComponent() {}
128 public void addPropertyChangeListener(PropertyChangeListener listener) {}
129 public void removePropertyChangeListener(PropertyChangeListener listener) {}
130 public int getAccessibleChildrenCount() { return 0; }
131 public Accessible getAccessibleChild(int value0) { return null; }
132 public AccessibleStateSet getAccessibleStateSet() { return null; }
133 public String getAccessibleName() { return null; }
134 public String getAccessibleDescription() { return null; }
135 public AccessibleRole getAccessibleRole() { return null; }
136 protected String getBorderTitle(Border value0) { return null; }
137 public String getToolTipText() { return null; }
138 public String getTitledBorderText() { return null; }
139 public AccessibleKeyBinding getAccessibleKeyBinding() { return null; }
142 /**
143 * An explicit value for the component's preferred size; if not set by a
144 * user, this is calculated on the fly by delegating to the {@link
145 * ComponentUI.getPreferredSize} method on the {@link #ui} property.
147 Dimension preferredSize;
149 /**
150 * An explicit value for the component's minimum size; if not set by a
151 * user, this is calculated on the fly by delegating to the {@link
152 * ComponentUI.getMinimumSize} method on the {@link #ui} property.
154 Dimension minimumSize;
156 /**
157 * An explicit value for the component's maximum size; if not set by a
158 * user, this is calculated on the fly by delegating to the {@link
159 * ComponentUI.getMaximumSize} method on the {@link #ui} property.
161 Dimension maximumSize;
165 * A value between 0.0 and 1.0 indicating the preferred horizontal
166 * alignment of the component, relative to its siblings. The values
167 * {@link #LEFT_ALIGNMENT}, {@link #CENTER_ALIGNMENT}, and {@link
168 * #RIGHT_ALIGNMENT} can also be used, as synonyms for <code>0.0</code>,
169 * <code>0.5</code>, and <code>1.0</code>, respectively. Not all layout
170 * managers use this property.
172 * @see #getAlignmentX
173 * @see #setAlignmentX
174 * @see javax.swing.OverlayLayout
175 * @see javax.swing.BoxLayout
177 float alignmentX = 0.0f;
180 * A value between 0.0 and 1.0 indicating the preferred vertical
181 * alignment of the component, relative to its siblings. The values
182 * {@link #TOP_ALIGNMENT}, {@link #CENTER_ALIGNMENT}, and {@link
183 * #BOTTOM_ALIGNMENT} can also be used, as synonyms for <code>0.0</code>,
184 * <code>0.5</code>, and <code>1.0</code>, respectively. Not all layout
185 * managers use this property.
187 * @see #getAlignmentY
188 * @see #setAlignmentY
189 * @see javax.swing.OverlayLayout
190 * @see javax.swing.BoxLayout
192 float alignmentY = 0.0f;
194 /**
195 * The border painted around this component.
197 * @see #paintBorder
199 Border border;
201 /**
202 * The text to show in the tooltip associated with this component.
204 * @see #setToolTipText
205 * @see #getToolTipText
207 String toolTipText;
209 /**
210 * <p>Whether to double buffer this component when painting. This flag
211 * should generally be <code>false</code>, except for top level
212 * components such as {@link JFrame} or {@link JApplet}.</p>
214 * <p>All children of a double buffered component are painted into the
215 * double buffer automatically, so only the top widget in a window needs
216 * to be double buffered.</p>
218 * @see #setDoubleBuffered
219 * @see #isDoubleBuffered
220 * @see #paintLock
221 * @see #paint
223 boolean doubleBuffered = false;
226 * A set of flags indicating which debugging graphics facilities should
227 * be enabled on this component. The values should be a combination of
228 * {@link DebugGraphics.NONE_OPTION}, {@link DebugGraphics.LOG_OPTION},
229 * {@link DebugGraphics.FLASH_OPTION}, or {@link
230 * DebugGraphics.BUFFERED_OPTION}.
232 * @see setDebugGraphicsOptions
233 * @see getDebugGraphicsOptions
234 * @see DebugGraphics
235 * @see getComponentGraphics
237 int debugGraphicsOptions;
239 /**
240 * <p>This property controls two independent behaviors simultaneously.</p>
242 * <p>First, it controls whether to fill the background of this widget
243 * when painting its body. This affects calls to {@link
244 * JComponent#paintComponent}, which in turn calls {@link
245 * ComponentUI#update} on the component's {@link #ui} property. If the
246 * component is opaque during this call, the background will be filled
247 * before calling {@link ComponentUI#paint}. This happens merely as a
248 * convenience; you may fill the component's background yourself too,
249 * but there is no need to do so if you will be filling with the same
250 * color.</p>
252 * <p>Second, it the opaque property informs swing's repaint system
253 * whether it will be necessary to paint the components "underneath" this
254 * component, in Z-order. If the component is opaque, it is considered to
255 * completely occlude components "underneath" it, so they will not be
256 * repainted along with the opaque component.</p>
258 * <p>The default value for this property is <code>false</code>, but most
259 * components will want to set it to <code>true</code> when installing UI
260 * defaults in {@link ComponentUI#installUI}.</p>
262 * @see #setOpaque
263 * @see #isOpaque
264 * @see #paintComponent
266 boolean opaque = false;
268 /**
269 * The user interface delegate for this component. Event delivery and
270 * repainting of the component are usually delegated to this object.
272 * @see #setUI
273 * @see #getUI
274 * @see #updateUI
276 protected ComponentUI ui;
279 * A hint to the focus system that this component should or should not
280 * get focus. If this is <code>false</code>, swing will not try to
281 * request focus on this component; if <code>true</code>, swing might
282 * try to request focus, but the request might fail. Thus it is only
283 * a hint guiding swing's behavior.
285 * @see #requestFocus
286 * @see #isRequestFocusEnabled
287 * @see #setRequestFocusEnabled
289 boolean requestFocusEnabled;
292 * Flag indicating behavior of this component when the mouse is dragged
293 * outside the component and the mouse <em>stops moving</em>. If
294 * <code>true</code>, synthetic mouse events will be delivered on regular
295 * timed intervals, continuing off in the direction the mouse exited the
296 * component, until the mouse is released or re-enters the component.
298 * @see setAutoscrolls
299 * @see getAutoscrolls
301 boolean autoscrolls = false;
304 * Listeners for events other than {@link PropertyChangeEvent} are
305 * handled by this listener list. PropertyChangeEvents are handled in
306 * {@link #changeSupport}.
308 protected EventListenerList listenerList = new EventListenerList();
310 /**
311 * Support for {@link PropertyChangeEvent} events. This is constructed
312 * lazily when the component gets its first {@link
313 * PropertyChangeListener} subscription; until then it's an empty slot.
315 private SwingPropertyChangeSupport changeSupport;
318 /**
319 * Storage for "client properties", which are key/value pairs associated
320 * with this component by a "client", such as a user application or a
321 * layout manager. This is lazily constructed when the component gets its
322 * first client property.
324 private Hashtable clientProperties;
326 private InputMap inputMap_whenFocused;
327 private InputMap inputMap_whenAncestorOfFocused;
328 private InputMap inputMap_whenInFocusedWindow;
329 private ActionMap actionMap;
330 /** @since 1.3 */
331 private boolean verifyInputWhenFocusTarget;
332 private InputVerifier inputVerifier;
334 private TransferHandler transferHandler;
336 /**
337 * A lock held during recursive painting; this is used to serialize
338 * access to the double buffer, and also to select the "top level"
339 * object which should acquire the double buffer in a given widget
340 * tree (which may have multiple double buffered children).
342 * @see #doubleBuffered
343 * @see #paint
345 private static final Object paintLock = new Object();
349 * The default locale of the component.
351 * @see #getDefaultLocale
352 * @see #setDefaultLocale
354 private static Locale defaultLocale;
356 public static final String TOOL_TIP_TEXT_KEY = "ToolTipText";
359 * Constant used to indicate that no condition has been assigned to a
360 * particular action.
362 * @see #registerKeyboardAction
364 public static final int UNDEFINED_CONDITION = -1;
367 * Constant used to indicate that an action should be performed only when
368 * the component has focus.
370 * @see #registerKeyboardAction
372 public static final int WHEN_FOCUSED = 0;
375 * Constant used to indicate that an action should be performed only when
376 * the component is an ancestor of the component which has focus.
378 * @see #registerKeyboardAction
380 public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1;
383 * Constant used to indicate that an action should be performed only when
384 * the component is in the window which has focus.
386 * @see #registerKeyboardAction
388 public static final int WHEN_IN_FOCUSED_WINDOW = 2;
392 * Creates a new <code>JComponent</code> instance.
394 public JComponent()
396 super();
397 super.setLayout(new FlowLayout());
398 defaultLocale = Locale.getDefault();
399 debugGraphicsOptions = DebugGraphics.NONE_OPTION;
403 * Helper to lazily construct and return the client properties table.
405 * @return The current client properties table
407 * @see #clientProperties
408 * @see #getClientProperty
409 * @see #putClientProperty
411 private Hashtable getClientProperties()
413 if (clientProperties == null)
414 clientProperties = new Hashtable();
415 return clientProperties;
419 * Get a client property associated with this component and a particular
420 * key.
422 * @param key The key with which to look up the client property
424 * @return A client property associated with this object and key
426 * @see #clientProperties
427 * @see #getClientProperties
428 * @see #putClientProperty
430 public Object getClientProperty(Object key)
432 return getClientProperties().get(key);
436 * Add a client property <code>value</code> to this component, associated
437 * with <code>key</code>. If there is an existing client property
438 * associated with <code>key</code>, it will be replaced.
440 * @param key The key of the client property association to add
441 * @param value The value of the client property association to add
443 * @see #clientProperties
444 * @see #getClientProperties
445 * @see #getClientProperty
447 public void putClientProperty(Object key, Object value)
449 getClientProperties().put(key, value);
453 * Unregister an <code>AncestorListener</code>.
455 * @param listener The listener to unregister
457 * @see addAncestorListener
459 public void removeAncestorListener(AncestorListener listener)
461 listenerList.remove(AncestorListener.class, listener);
465 * Unregister a <code>PropertyChangeListener</code>.
467 * @param listener The listener to register
469 * @see #addPropertyChangeListener
470 * @see #changeSupport
472 public void removePropertyChangeListener(PropertyChangeListener listener)
474 if (changeSupport != null)
475 changeSupport.removePropertyChangeListener(listener);
479 * Unregister a <code>PropertyChangeListener</code>.
481 * @param propertyName The property name to unregister the listener from
482 * @param listener The listener to unregister
484 * @see #addPropertyChangeListener
485 * @see #changeSupport
487 public void removePropertyChangeListener(String propertyName,
488 PropertyChangeListener listener)
490 if (changeSupport != null)
491 changeSupport.removePropertyChangeListener(propertyName, listener);
495 * Unregister a <code>VetoableChangeChangeListener</code>.
497 * @param listener The listener to unregister
499 * @see #addVetoableChangeListener
501 public void removeVetoableChangeListener(VetoableChangeListener listener)
503 listenerList.remove(VetoableChangeListener.class, listener);
507 * Register an <code>AncestorListener</code>.
509 * @param listener The listener to register
511 * @see #removeVetoableChangeListener
513 public void addAncestorListener(AncestorListener listener)
515 listenerList.add(AncestorListener.class, listener);
519 * Register a <code>PropertyChangeListener</code>. This listener will
520 * receive any PropertyChangeEvent, regardless of property name. To
521 * listen to a specific property name, use {@link
522 * #addPropertyChangeListener(String,PropertyChangeListener)} instead.
524 * @param listener The listener to register
526 * @see #removePropertyChangeListener
527 * @see #changeSupport
529 public void addPropertyChangeListener(PropertyChangeListener listener)
531 if (changeSupport == null)
532 changeSupport = new SwingPropertyChangeSupport(this);
533 changeSupport.addPropertyChangeListener(listener);
537 * Register a <code>PropertyChangeListener</code> for a specific, named
538 * property. To listen to all property changes, regardless of name, use
539 * {@link #addPropertyChangeListener(PropertyChangeListener)} instead.
541 * @param propertyName The property name to listen to
542 * @param listener The listener to register
544 * @see #removePropertyChangeListener
545 * @see #changeSupport
547 public void addPropertyChangeListener(String propertyName,
548 PropertyChangeListener listener)
550 listenerList.add(PropertyChangeListener.class, listener);
554 * Register a <code>VetoableChangeListener</code>.
556 * @param listener The listener to register
558 * @see #removeVetoableChangeListener
559 * @see #listenerList
561 public void addVetoableChangeListener(VetoableChangeListener listener)
563 listenerList.add(VetoableChangeListener.class, listener);
567 * Return all registered listeners of a particular type.
569 * @param listenerType The type of listener to return
571 * @return All listeners in the {@link #listenerList} which
572 * are of the specified type
574 * @see #listenerList
576 public EventListener[] getListeners(Class listenerType)
578 return listenerList.getListeners(listenerType);
582 * Return all registered <code>AncestorListener</code> objects.
584 * @return The set of <code>AncestorListener</code> objects in {@link
585 * #listenerList}
587 public AncestorListener[] getAncestorListeners()
589 return (AncestorListener[]) getListeners(AncestorListener.class);
593 * Return all registered <code>VetoableChangeListener</code> objects.
595 * @return The set of <code>VetoableChangeListener</code> objects in {@link
596 * #listenerList}
598 public VetoableChangeListener[] getVetoableChangeListeners()
600 return (VetoableChangeListener[]) getListeners(VetoableChangeListener.class);
604 * Return all <code>PropertyChangeListener</code> objects registered to listen
605 * for a particular property.
607 * @param property The property to return the listeners of
609 * @return The set of <code>PropertyChangeListener</code> objects in
610 * {@link #changeSupport} registered to listen on the specified propert
612 public PropertyChangeListener[] getPropertyChangeListeners(String property)
614 return changeSupport == null ? new PropertyChangeListener[0]
615 : changeSupport.getPropertyChangeListeners(property);
619 * A variant of {@link #firePropertyChange(String,Object,Object)}
620 * for properties with <code>boolean</code> values.
622 public void firePropertyChange(String propertyName, boolean oldValue,
623 boolean newValue)
625 if (changeSupport != null)
626 changeSupport.firePropertyChange(propertyName, Boolean.valueOf(oldValue),
627 Boolean.valueOf(newValue));
631 * A variant of {@link #firePropertyChange(String,Object,Object)}
632 * for properties with <code>byte</code> values.
634 public void firePropertyChange(String propertyName, byte oldValue,
635 byte newValue)
637 if (changeSupport != null)
638 changeSupport.firePropertyChange(propertyName, new Byte(oldValue),
639 new Byte(newValue));
643 * A variant of {@link #firePropertyChange(String,Object,Object)}
644 * for properties with <code>char</code> values.
646 public void firePropertyChange(String propertyName, char oldValue,
647 char newValue)
649 if (changeSupport != null)
650 changeSupport.firePropertyChange(propertyName, new Character(oldValue),
651 new Character(newValue));
655 * A variant of {@link #firePropertyChange(String,Object,Object)}
656 * for properties with <code>double</code> values.
658 public void firePropertyChange(String propertyName, double oldValue,
659 double newValue)
661 if (changeSupport != null)
662 changeSupport.firePropertyChange(propertyName, new Double(oldValue),
663 new Double(newValue));
667 * A variant of {@link #firePropertyChange(String,Object,Object)}
668 * for properties with <code>float</code> values.
670 public void firePropertyChange(String propertyName, float oldValue,
671 float newValue)
673 if (changeSupport != null)
674 changeSupport.firePropertyChange(propertyName, new Float(oldValue),
675 new Float(newValue));
679 * A variant of {@link #firePropertyChange(String,Object,Object)}
680 * for properties with <code>int</code> values.
682 public void firePropertyChange(String propertyName, int oldValue,
683 int newValue)
685 if (changeSupport != null)
686 changeSupport.firePropertyChange(propertyName, new Integer(oldValue),
687 new Integer(newValue));
691 * A variant of {@link #firePropertyChange(String,Object,Object)}
692 * for properties with <code>long</code> values.
694 public void firePropertyChange(String propertyName, long oldValue,
695 long newValue)
697 if (changeSupport != null)
698 changeSupport.firePropertyChange(propertyName, new Long(oldValue),
699 new Long(newValue));
703 * Call {@link PropertyChangeListener#propertyChange} on all listeners
704 * registered to listen to a given property. Any method which changes
705 * the specified property of this component should call this method.
707 * @param propertyName The property which changed
708 * @param oldValue The old value of the property
709 * @param newValue The new value of the property
711 * @see #changeSupport
712 * @see #addPropertyChangeListener
713 * @see #removePropertyChangeListener
715 protected void firePropertyChange(String propertyName, Object oldValue,
716 Object newValue)
718 if (changeSupport != null)
719 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
723 * A variant of {@link #firePropertyChange(String,Object,Object)}
724 * for properties with <code>short</code> values.
726 public void firePropertyChange(String propertyName, short oldValue,
727 short newValue)
729 if (changeSupport != null)
730 changeSupport.firePropertyChange(propertyName, new Short(oldValue),
731 new Short(newValue));
735 * Call {@link VetoableChangeListener#vetoableChange} on all listeners
736 * registered to listen to a given property. Any method which changes
737 * the specified property of this component should call this method.
739 * @param propertyName The property which changed
740 * @param oldValue The old value of the property
741 * @param newValue The new value of the property
743 * @throws PropertyVetoException if the change was vetoed by a listener
745 * @see addVetoableChangeListener
746 * @see removeVetoableChangeListener
748 protected void fireVetoableChange(String propertyName, Object oldValue,
749 Object newValue)
750 throws PropertyVetoException
752 VetoableChangeListener[] listeners = getVetoableChangeListeners();
754 PropertyChangeEvent evt = new PropertyChangeEvent(this, propertyName, oldValue, newValue);
756 for (int i = 0; i < listeners.length; i++)
757 listeners[i].vetoableChange(evt);
761 * Get the value of the accessibleContext property for this component.
763 * @return the current value of the property
765 public AccessibleContext getAccessibleContext()
767 return null;
772 * Get the value of the {@link #alignmentX} property.
774 * @return The current value of the property.
776 * @see #setAlignmentX
777 * @see #alignmentY
779 public float getAlignmentX()
781 return alignmentX;
785 * Get the value of the {@link #alignmentY} property.
787 * @return The current value of the property.
789 * @see #setAlignmentY
790 * @see #alignmentX
792 public float getAlignmentY()
794 return alignmentY;
798 * Get the current value of the {@link #autoscrolls} property.
800 * @return The current value of the property
802 public boolean getAutoscrolls()
804 return autoscrolls;
808 * Set the value of the {@link #border} property, revalidate
809 * and repaint this component.
811 * @param newBorder The new value of the property
813 * @see #getBorder
815 public void setBorder(Border newBorder)
817 Border oldBorder = border;
818 border = newBorder;
819 firePropertyChange("border", oldBorder, newBorder);
820 revalidate();
821 repaint();
825 * Get the value of the {@link #border} property.
827 * @return The property's current value
829 * @see #setBorder
831 public Border getBorder()
833 return border;
837 * Get the component's current bounding box. If a rectangle is provided,
838 * use this as the return value (adjusting its fields in place);
839 * otherwise (of <code>null</code> is provided) return a new {@link
840 * Rectangle}.
842 * @param rv Optional return value to use
844 * @return A rectangle bounding the component
846 public Rectangle getBounds(Rectangle rv)
848 if (rv == null)
849 return new Rectangle(getX(), getY(), getWidth(), getHeight());
850 else
852 rv.setBounds(getX(), getY(), getWidth(), getHeight());
853 return rv;
858 * Prepares a graphics context for painting this object. If {@link
859 * #debugGraphicsOptions} is not equal to {@link
860 * DebugGraphics#NONE_OPTION}, produce a new {@link DebugGraphics} object
861 * wrapping the parameter. Otherwise configure the parameter with this
862 * component's foreground color and font.
864 * @param g The graphics context to wrap or configure
866 * @return A graphics context to paint this object with
868 * @see #debugGraphicsOptions
869 * @see #paint
871 protected Graphics getComponentGraphics(Graphics g)
873 Graphics g2 = g.create();
874 g2.setFont(this.getFont());
875 g2.setColor(this.getForeground());
876 return g2;
881 * Get the value of the {@link #debugGraphicsOptions} property.
883 * @return The current value of the property.
885 * @see #setDebugGraphicsOptions
886 * @see #debugGraphicsOptions
888 public int getDebugGraphicsOptions()
890 return 0;
894 * Get the component's insets, which are calculated from
895 * the {@link #border} property. If the border is <code>null</code>,
896 * calls {@link Container#getInsets}.
898 * @return The component's current insets
900 public Insets getInsets()
902 if (border == null)
903 return super.getInsets();
904 return getBorder().getBorderInsets(this);
908 * Get the component's insets, which are calculated from the {@link
909 * #border} property. If the border is <code>null</code>, calls {@link
910 * Container#getInsets}. The passed-in {@link Insets} value will be
911 * used as the return value, if possible.
913 * @param insets Return value object to reuse, if possible
915 * @return The component's current insets
917 public Insets getInsets(Insets insets)
919 Insets t = getInsets();
921 if (insets == null)
922 return t;
924 insets.left = t.left;
925 insets.right = t.right;
926 insets.top = t.top;
927 insets.bottom = t.bottom;
928 return insets;
932 * Get the component's location. The passed-in {@link Point} value
933 * will be used as the return value, if possible.
935 * @param rv Return value object to reuse, if possible
937 * @return The component's current location
939 public Point getLocation(Point rv)
941 if (rv == null)
942 return new Point(getX(), getY());
944 rv.setLocation(getX(), getY());
945 return rv;
949 * Get the component's maximum size. If the {@link #maximumSize} property
950 * has been explicitly set, it is returned. If the {@link #maximumSize}
951 * property has not been set but the {@link ui} property has been, the
952 * result of {@link ComponentUI#getMaximumSize} is returned. If neither
953 * property has been set, the result of {@link Container#getMaximumSize}
954 * is returned.
956 * @return The maximum size of the component
958 * @see #maximumSize
959 * @see #setMaximumSize
961 public Dimension getMaximumSize()
963 if (maximumSize != null)
964 return maximumSize;
966 if (ui != null)
968 Dimension s = ui.getMaximumSize(this);
969 if (s != null)
970 return s;
973 Dimension p = super.getMaximumSize();
974 return p;
978 * Get the component's minimum size. If the {@link #minimumSize} property
979 * has been explicitly set, it is returned. If the {@link #minimumSize}
980 * property has not been set but the {@link ui} property has been, the
981 * result of {@link ComponentUI#getMinimumSize} is returned. If neither
982 * property has been set, the result of {@link Container#getMinimumSize}
983 * is returned.
985 * @return The minimum size of the component
987 * @see #minimumSize
988 * @see #setMinimumSize
990 public Dimension getMinimumSize()
992 if (minimumSize != null)
993 return minimumSize;
995 if (ui != null)
997 Dimension s = ui.getMinimumSize(this);
998 if (s != null)
999 return s;
1002 Dimension p = super.getMinimumSize();
1003 return p;
1007 * Get the component's preferred size. If the {@link #preferredSize}
1008 * property has been explicitly set, it is returned. If the {@link
1009 * #preferredSize} property has not been set but the {@link ui} property
1010 * has been, the result of {@link ComponentUI#getPreferredSize} is
1011 * returned. If neither property has been set, the result of {@link
1012 * Container#getPreferredSize} is returned.
1014 * @return The preferred size of the component
1016 * @see #preferredSize
1017 * @see #setPreferredSize
1019 public Dimension getPreferredSize()
1021 if (preferredSize != null)
1022 return preferredSize;
1024 if (ui != null)
1026 Dimension s = ui.getPreferredSize(this);
1027 if (s != null)
1028 return s;
1030 Dimension p = super.getPreferredSize();
1031 return p;
1035 * Checks if a maximum size was explicitely set on the component.
1037 * @return <code>true</code> if a maximum size was set,
1038 * <code>false</code> otherwise
1040 * @since 1.3
1042 public boolean isMaximumSizeSet()
1044 return maximumSize != null;
1048 * Checks if a minimum size was explicitely set on the component.
1050 * @return <code>true</code> if a minimum size was set,
1051 * <code>false</code> otherwise
1053 * @since 1.3
1055 public boolean isMinimumSizeSet()
1057 return minimumSize != null;
1061 * Checks if a preferred size was explicitely set on the component.
1063 * @return <code>true</code> if a preferred size was set,
1064 * <code>false</code> otherwise
1066 * @since 1.3
1068 public boolean isPreferredSizeSet()
1070 return preferredSize != null;
1074 * Return the value of the {@link #nextFocusableComponent} property.
1076 * @return The current value of the property, or <code>null</code>
1077 * if none has been set.
1079 * @deprecated See {@link java.awt.FocusTraversalPolicy}
1081 public Component getNextFocusableComponent()
1083 return null;
1087 * Return the set of {@link KeyStroke} objects which are registered
1088 * to initiate actions on this component.
1090 * @return An array of the registered keystrokes
1092 public KeyStroke[] getRegisteredKeyStrokes()
1094 return null;
1098 * Returns the first ancestor of this component which is a {@link JRootPane}.
1099 * Equivalent to calling <code>SwingUtilities.getRootPane(this);</code>.
1101 * @return An ancestral JRootPane, or <code>null</code> if none exists.
1103 public JRootPane getRootPane()
1105 JRootPane p = SwingUtilities.getRootPane(this);
1106 return p;
1110 * Get the component's size. The passed-in {@link Dimension} value
1111 * will be used as the return value, if possible.
1113 * @param rv Return value object to reuse, if possible
1115 * @return The component's current size
1117 public Dimension getSize(Dimension rv)
1119 if (rv == null)
1120 return new Dimension(getWidth(), getHeight());
1121 else
1123 rv.setSize(getWidth(), getHeight());
1124 return rv;
1129 * Return the {@link #toolTip} property of this component, creating it and
1130 * setting it if it is currently <code>null</code>. This method can be
1131 * overridden in subclasses which wish to control the exact form of
1132 * tooltip created.
1134 * @return The current toolTip
1136 public JToolTip createToolTip()
1138 JToolTip toolTip = new JToolTip();
1139 toolTip.setComponent(this);
1140 toolTip.setTipText(toolTipText);
1142 return toolTip;
1146 * Return the location at which the {@link #toolTip} property should be
1147 * displayed, when triggered by a particular mouse event.
1149 * @param event The event the tooltip is being presented in response to
1151 * @return The point at which to display a tooltip, or <code>null</code>
1152 * if swing is to choose a default location.
1154 public Point getToolTipLocation(MouseEvent event)
1156 return null;
1160 * Set the value of the {@link #toolTipText} property.
1162 * @param text The new property value
1164 * @see #getToolTipText
1166 public void setToolTipText(String text)
1168 if (text == null)
1170 ToolTipManager.sharedInstance().unregisterComponent(this);
1171 toolTipText = null;
1172 return;
1175 // XXX: The tip text doesn't get updated unless you set it to null
1176 // and then to something not-null. This is consistent with the behaviour
1177 // of Sun's ToolTipManager.
1179 String oldText = toolTipText;
1180 toolTipText = text;
1182 if (oldText == null)
1183 ToolTipManager.sharedInstance().registerComponent(this);
1187 * Get the value of the {@link #toolTipText} property.
1189 * @return The current property value
1191 * @see #setToolTipText
1193 public String getToolTipText()
1195 return toolTipText;
1199 * Get the value of the {@link #toolTipText} property, in response to a
1200 * particular mouse event.
1202 * @param event The mouse event which triggered the tooltip
1204 * @return The current property value
1206 * @see #setToolTipText
1208 public String getToolTipText(MouseEvent event)
1210 return getToolTipText();
1214 * Return the top level ancestral container (usually a {@link
1215 * java.awt.Window} or {@link java.awt.Applet}) which this component is
1216 * contained within, or <code>null</code> if no ancestors exist.
1218 * @return The top level container, if it exists
1220 public Container getTopLevelAncestor()
1222 Container c = getParent();
1223 for (Container peek = c; peek != null; peek = peek.getParent())
1224 c = peek;
1225 return c;
1229 * Compute the component's visible rectangle, which is defined
1230 * recursively as either the component's bounds, if it has no parent, or
1231 * the intersection of the component's bounds with the visible rectangle
1232 * of its parent.
1234 * @param rect The return value slot to place the visible rectangle in
1236 public void computeVisibleRect(Rectangle rect)
1238 Component c = getParent();
1239 if (c != null && c instanceof JComponent)
1241 ((JComponent) c).computeVisibleRect(rect);
1242 rect.translate(-getX(), -getY());
1243 Rectangle2D.intersect(rect,
1244 new Rectangle(0, 0, getWidth(), getHeight()),
1245 rect);
1247 else
1248 rect.setRect(0, 0, getWidth(), getHeight());
1252 * Return the component's visible rectangle in a new {@link Rectangle},
1253 * rather than via a return slot.
1255 * @return The component's visible rectangle
1257 * @see #computeVisibleRect(Rectangle)
1259 public Rectangle getVisibleRect()
1261 Rectangle r = new Rectangle();
1262 computeVisibleRect(r);
1263 return r;
1267 * <p>Requests that this component receive input focus, giving window
1268 * focus to the top level ancestor of this component. Only works on
1269 * displayable, focusable, visible components.</p>
1271 * <p>This method should not be called by clients; it is intended for
1272 * focus implementations. Use {@link Component#requestFocus} instead.</p>
1274 * @see {@link Component#requestFocus}
1276 public void grabFocus()
1281 * Get the value of the {@link #doubleBuffered} property.
1283 * @return The property's current value
1285 public boolean isDoubleBuffered()
1287 return doubleBuffered;
1291 * Return <code>true</code> if the provided component has no native peer;
1292 * in other words, if it is a "lightweight component".
1294 * @param c The component to test for lightweight-ness
1296 * @return Whether or not the component is lightweight
1298 public static boolean isLightweightComponent(Component c)
1300 return c.getPeer() instanceof LightweightPeer;
1304 * Return <code>true<code> if you wish this component to manage its own
1305 * focus. In particular: if you want this component to be sent
1306 * <code>TAB</code> and <code>SHIFT+TAB</code> key events, and to not
1307 * have its children considered as focus transfer targets. If
1308 * <code>true</code>, focus traversal around this component changes to
1309 * <code>CTRL+TAB</code> and <code>CTRL+SHIFT+TAB</code>.
1311 * @return <code>true</code> if you want this component to manage its own
1312 * focus, otherwise (by default) <code>false</code>
1314 * @deprecated 1.4 Use {@link Component.setFocusTraversalKeys(int,Set)} and
1315 * {@link Container.setFocusCycleRoot(boolean)} instead
1317 public boolean isManagingFocus()
1319 return false;
1323 * Return the current value of the {@link opaque} property.
1325 * @return The current property value
1327 public boolean isOpaque()
1329 return opaque;
1333 * Return <code>true</code> if the component can guarantee that none of its
1334 * children will overlap in Z-order. This is a hint to the painting system.
1335 * The default is to return <code>true</code>, but some components such as
1336 * {@link JLayeredPane} should override this to return <code>false</code>.
1338 * @return Whether the component tiles its children
1340 public boolean isOptimizedDrawingEnabled()
1342 return true;
1346 * Return <code>true</code> if this component is currently painting a tile.
1348 * @return Whether the component is painting a tile
1350 public boolean isPaintingTile()
1352 return false;
1356 * Get the value of the {@link #requestFocusEnabled} property.
1358 * @return The current value of the property
1360 public boolean isRequestFocusEnabled()
1362 return requestFocusEnabled;
1366 * Return <code>true</code> if this component is a validation root; this
1367 * will cause calls to {@link #invalidate} in this component's children
1368 * to be "captured" at this component, and not propagate to its parents.
1369 * For most components this should return <code>false</code>, but some
1370 * components such as {@link JViewPort} will want to return
1371 * <code>true</code>.
1373 * @return Whether this component is a validation root
1375 public boolean isValidateRoot()
1377 return false;
1381 * <p>Paint the component. This is a delicate process, and should only be
1382 * called from the repaint thread, under control of the {@link
1383 * RepaintManager}. Client code should usually call {@link #repaint} to
1384 * trigger painting.</p>
1386 * <p>This method will acquire a double buffer from the {@link
1387 * RepaintManager} if the component's {@link #doubleBuffered} property is
1388 * <code>true</code> and the <code>paint</code> call is the
1389 * <em>first</em> recursive <code>paint</code> call inside swing.</p>
1391 * <p>The method will also modify the provided {@link Graphics} context
1392 * via the {@link #getComponentGraphics} method. If you want to customize
1393 * the graphics object used for painting, you should override that method
1394 * rather than <code>paint</code>.</p>
1396 * <p>The body of the <code>paint</code> call involves calling {@link
1397 * #paintComponent}, {@link #paintBorder}, and {@link #paintChildren} in
1398 * order. If you want to customize painting behavior, you should override
1399 * one of these methods rather than <code>paint</code>.</p>
1401 * <p>For more details on the painting sequence, see <a
1402 * href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">this
1403 * article</a>.</p>
1405 * @param g The graphics context to paint with
1407 * @see #paintImmediately
1409 public void paint(Graphics g)
1411 Graphics g2 = g;
1412 Image doubleBuffer = null;
1413 RepaintManager rm = RepaintManager.currentManager(this);
1415 if (isDoubleBuffered()
1416 && (rm.isDoubleBufferingEnabled())
1417 && (! Thread.holdsLock(paintLock)))
1419 doubleBuffer = rm.getOffscreenBuffer(this, getWidth(), getHeight());
1422 synchronized (paintLock)
1424 if (doubleBuffer != null)
1426 g2 = doubleBuffer.getGraphics();
1427 g2.setClip(g.getClipBounds());
1430 g2 = getComponentGraphics(g2);
1431 paintComponent(g2);
1432 paintBorder(g2);
1433 paintChildren(g2);
1435 if (doubleBuffer != null)
1436 g.drawImage(doubleBuffer, 0, 0, (ImageObserver) null);
1441 * Paint the component's border. This usually means calling {@link
1442 * Border#paintBorder} on the {@link #border} property, if it is
1443 * non-<code>null</code>. You may override this if you wish to customize
1444 * border painting behavior. The border is painted after the component's
1445 * body, but before the component's children.
1447 * @param g The graphics context with which to paint the border
1449 * @see #paint
1450 * @see #paintChildren
1451 * @see #paintComponent
1453 protected void paintBorder(Graphics g)
1455 if (getBorder() != null)
1456 getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
1460 * Paint the component's children. This usually means calling {@link
1461 * Container#paint}, which recursively calls {@link #paint} on any of the
1462 * component's children, with appropriate changes to coordinate space and
1463 * clipping region. You may override this if you wish to customize
1464 * children painting behavior. The children are painted after the
1465 * component's body and border.
1467 * @param g The graphics context with which to paint the children
1469 * @see #paint
1470 * @see #paintBorder
1471 * @see #paintComponent
1473 protected void paintChildren(Graphics g)
1475 super.paint(g);
1479 * Paint the component's body. This usually means calling {@link
1480 * ComponentUI#update} on the {@link #ui} property of the component, if
1481 * it is non-<code>null</code>. You may override this if you wish to
1482 * customize the component's body-painting behavior. The component's body
1483 * is painted first, before the border and children.
1485 * @param g The graphics context with which to paint the body
1487 * @see #paint
1488 * @see #paintBorder
1489 * @see #paintChildren
1491 protected void paintComponent(Graphics g)
1493 if (ui != null)
1494 ui.update(g, this);
1498 * A variant of {@link #paintImmediately(Rectangle)} which takes
1499 * integer parameters.
1501 * @param x The left x coordinate of the dirty region
1502 * @param y The top y coordinate of the dirty region
1503 * @param w The width of the dirty region
1504 * @param h The height of the dirty region
1506 public void paintImmediately(int x, int y, int w, int h)
1508 paintImmediately(new Rectangle(x, y, w, h));
1512 * Transform the provided dirty rectangle for this component into the
1513 * appropriate ancestral {@link JRootPane} and call {@link #paint} on
1514 * that root pane. This method is called from the {@link RepaintManager}
1515 * and should always be called within the painting thread.
1517 * @param r The dirty rectangle to paint
1519 public void paintImmediately(Rectangle r)
1521 Component root = SwingUtilities.getRoot(this);
1522 if (root == null || ! root.isShowing())
1523 return;
1524 Graphics g = root.getGraphics();
1525 if (g == null)
1526 return;
1528 Rectangle clip = SwingUtilities.convertRectangle(this, r, root);
1529 g.setClip(clip);
1530 root.paint(g);
1531 g.dispose();
1535 * Return a string representation for this component, for use in
1536 * debugging.
1538 * @return A string describing this component.
1540 protected String paramString()
1542 return "JComponent";
1546 * A variant of {@link
1547 * #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which
1548 * provides <code>null</code> for the command name.
1550 public void registerKeyboardAction(ActionListener act,
1551 KeyStroke stroke,
1552 int cond)
1554 registerKeyboardAction(act, null, stroke, cond);
1558 * There is some charmingly undocumented behavior sun seems to be using
1559 * to simulate the old register/unregister keyboard binding API. It's not
1560 * clear to me why this matters, but we shall endeavour to follow suit.
1562 * Two main thing seem to be happening when you do registerKeyboardAction():
1564 * - no actionMap() entry gets created, just an entry in inputMap()
1566 * - the inputMap() entry is a proxy class which invokes the the
1567 * binding's actionListener as a target, and which clobbers the command
1568 * name sent in the ActionEvent, providing the binding command name
1569 * instead.
1571 * This much you can work out just by asking the input and action maps
1572 * what they contain after making bindings, and watching the event which
1573 * gets delivered to the recipient. Beyond that, it seems to be a
1574 * sun-private solution so I will only immitate it as much as it matters
1575 * to external observers.
1578 private static class ActionListenerProxy
1579 extends AbstractAction
1581 ActionListener target;
1582 String bindingCommandName;
1584 public ActionListenerProxy(ActionListener li,
1585 String cmd)
1587 target = li;
1588 bindingCommandName = cmd;
1591 public void actionPerformed(ActionEvent e)
1593 ActionEvent derivedEvent = new ActionEvent(e.getSource(),
1594 e.getID(),
1595 bindingCommandName,
1596 e.getModifiers());
1597 target.actionPerformed(derivedEvent);
1603 * An obsolete method to register a keyboard action on this component.
1604 * You should use <code>getInputMap</code> and <code>getActionMap</code>
1605 * to fetch mapping tables from keystrokes to commands, and commands to
1606 * actions, respectively, and modify those mappings directly.
1608 * @param anAction The action to be registered
1609 * @param aCommand The command to deliver in the delivered {@link
1610 * java.awt.ActionEvent}
1611 * @param aKeyStroke The keystroke to register on
1612 * @param aCondition One of the values {@link #UNDEFINED_CONDITION},
1613 * {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or
1614 * {@link #WHEN_IN_FOCUSED_WINDOW}, indicating the condition which must
1615 * be met for the action to be fired
1617 * @see #unregisterKeyboardAction
1618 * @see #getConditionForKeystroke
1619 * @see #resetKeyboardActiond
1621 public void registerKeyboardAction(ActionListener act,
1622 String cmd,
1623 KeyStroke stroke,
1624 int cond)
1626 getInputMap(cond).put(stroke, new ActionListenerProxy(act, cmd));
1631 public final void setInputMap(int condition, InputMap map)
1633 enableEvents(AWTEvent.KEY_EVENT_MASK);
1634 switch (condition)
1636 case WHEN_FOCUSED:
1637 inputMap_whenFocused = map;
1638 break;
1640 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
1641 inputMap_whenAncestorOfFocused = map;
1642 break;
1644 case WHEN_IN_FOCUSED_WINDOW:
1645 inputMap_whenInFocusedWindow = map;
1646 break;
1648 case UNDEFINED_CONDITION:
1649 default:
1650 throw new IllegalArgumentException();
1654 public final InputMap getInputMap(int condition)
1656 enableEvents(AWTEvent.KEY_EVENT_MASK);
1657 switch (condition)
1659 case WHEN_FOCUSED:
1660 if (inputMap_whenFocused == null)
1661 inputMap_whenFocused = new InputMap();
1662 return inputMap_whenFocused;
1664 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
1665 if (inputMap_whenAncestorOfFocused == null)
1666 inputMap_whenAncestorOfFocused = new InputMap();
1667 return inputMap_whenAncestorOfFocused;
1669 case WHEN_IN_FOCUSED_WINDOW:
1670 if (inputMap_whenInFocusedWindow == null)
1671 inputMap_whenInFocusedWindow = new InputMap();
1672 return inputMap_whenInFocusedWindow;
1674 case UNDEFINED_CONDITION:
1675 default:
1676 return null;
1680 public final InputMap getInputMap()
1682 return getInputMap(WHEN_FOCUSED);
1685 public final ActionMap getActionMap()
1687 if (actionMap == null)
1688 actionMap = new ActionMap();
1689 return actionMap;
1692 public final void setActionMap(ActionMap map)
1694 actionMap = map;
1698 * Return the condition that determines whether a registered action
1699 * occurs in response to the specified keystroke.
1701 * @param aKeyStroke The keystroke to return the condition of
1703 * @return One of the values {@link #UNDEFINED_CONDITION}, {@link
1704 * #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or {@link
1705 * #WHEN_IN_FOCUSED_WINDOW}
1707 * @deprecated As of 1.3 KeyStrokes can be registered with multiple
1708 * simultaneous conditions.
1710 * @see #registerKeyboardAction
1711 * @see #unregisterKeyboardAction
1712 * @see #resetKeyboardActiond
1714 public int getConditionForKeyStroke(KeyStroke ks)
1716 if (inputMap_whenFocused != null
1717 && inputMap_whenFocused.get(ks) != null)
1718 return WHEN_FOCUSED;
1719 else if (inputMap_whenAncestorOfFocused != null
1720 && inputMap_whenAncestorOfFocused.get(ks) != null)
1721 return WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
1722 else if (inputMap_whenInFocusedWindow != null
1723 && inputMap_whenInFocusedWindow.get(ks) != null)
1724 return WHEN_IN_FOCUSED_WINDOW;
1725 else
1726 return UNDEFINED_CONDITION;
1730 * Get the ActionListener (typically an {@link Action} object) which is
1731 * associated with a particular keystroke.
1733 * @param aKeyStroke The keystroke to retrieve the action of
1735 * @return The action associated with the specified keystroke
1737 * @deprecated Use {@link #getActionMap()}
1739 public ActionListener getActionForKeyStroke(KeyStroke ks)
1741 Object cmd = getInputMap().get(ks);
1742 if (cmd != null)
1744 if (cmd instanceof ActionListenerProxy)
1745 return (ActionListenerProxy) cmd;
1746 else if (cmd instanceof String)
1747 return getActionMap().get(cmd);
1749 return null;
1753 * A hook for subclasses which want to customize event processing.
1755 protected void processComponentKeyEvent(KeyEvent e)
1760 * Override the default key dispatch system from Component to hook into
1761 * the swing {@link InputMap} / {@link ActionMap} system.
1763 * See <a
1764 * href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">this
1765 * report</a> for more details, it's somewhat complex.
1767 protected void processKeyEvent(KeyEvent e)
1769 processComponentKeyEvent(e);
1771 // FIXME: this needs to be elaborated significantly, to do all the
1772 // focus / ancestor / window searching for the various binding modes.
1773 if (! e.isConsumed() &&
1774 processKeyBinding(KeyStroke.getKeyStrokeForEvent(e),
1775 e, WHEN_FOCUSED, e.getID() == KeyEvent.KEY_PRESSED))
1776 e.consume();
1779 protected boolean processKeyBinding(KeyStroke ks,
1780 KeyEvent e,
1781 int condition,
1782 boolean pressed)
1784 if (isEnabled())
1786 Action act = null;
1787 InputMap map = getInputMap(condition);
1788 if (map != null)
1790 Object cmd = map.get(ks);
1791 if (cmd != null)
1793 if (cmd instanceof ActionListenerProxy)
1794 act = (Action) cmd;
1795 else
1796 act = (Action) getActionMap().get(cmd);
1799 if (act != null && act.isEnabled())
1800 return SwingUtilities.notifyAction(act, ks, e, this, e.getModifiers());
1802 return false;
1806 * Remove a keyboard action registry.
1808 * @param stroke The keystroke to unregister
1810 * @see #registerKeyboardAction
1811 * @see #getConditionForKeystroke
1812 * @see #resetKeyboardActiond
1814 public void unregisterKeyboardAction(KeyStroke aKeyStroke)
1820 * Reset all keyboard action registries.
1822 * @see #registerKeyboardAction
1823 * @see #unregisterKeyboardAction
1824 * @see #getConditionForKeystroke
1826 public void resetKeyboardActions()
1828 if (inputMap_whenFocused != null)
1829 inputMap_whenFocused.clear();
1830 if (inputMap_whenAncestorOfFocused != null)
1831 inputMap_whenAncestorOfFocused.clear();
1832 if (inputMap_whenInFocusedWindow != null)
1833 inputMap_whenInFocusedWindow.clear();
1834 if (actionMap != null)
1835 actionMap.clear();
1840 * Mark the described region of this component as dirty in the current
1841 * {@link RepaintManager}. This will queue an asynchronous repaint using
1842 * the system painting thread in the near future.
1844 * @param tm ignored
1845 * @param x coordinate of the region to mark as dirty
1846 * @param y coordinate of the region to mark as dirty
1847 * @param width dimension of the region to mark as dirty
1848 * @param height dimension of the region to mark as dirty
1850 public void repaint(long tm, int x, int y, int width, int height)
1852 Rectangle dirty = new Rectangle(x, y, width, height);
1853 Rectangle vis = getVisibleRect();
1854 dirty = dirty.intersection(vis);
1855 RepaintManager.currentManager(this).addDirtyRegion(this, dirty.x, dirty.y,
1856 dirty.width,
1857 dirty.height);
1861 * Mark the described region of this component as dirty in the current
1862 * {@link RepaintManager}. This will queue an asynchronous repaint using
1863 * the system painting thread in the near future.
1865 * @param r The rectangle to mark as dirty
1867 public void repaint(Rectangle r)
1869 repaint((long) 0, (int) r.getX(), (int) r.getY(), (int) r.getWidth(),
1870 (int) r.getHeight());
1874 * Request focus on the default component of this component's {@link
1875 * FocusTraversalPolicy}.
1877 * @return The result of {@link #requestFocus}
1879 * @deprecated Use {@link #requestFocus()} on the default component provided from
1880 * the {@link FocusTraversalPolicy} instead.
1882 public boolean requestDefaultFocus()
1884 return false;
1888 * Queue a an invalidation and revalidation of this component, using
1889 * {@link RepaintManager#addInvalidComponent}.
1891 public void revalidate()
1893 invalidate();
1894 RepaintManager.currentManager(this).addInvalidComponent(this);
1898 * Calls <code>scrollRectToVisible</code> on the component's parent.
1899 * Components which can service this call should override.
1901 * @param r The rectangle to make visible
1903 public void scrollRectToVisible(Rectangle r)
1905 Component p = getParent();
1906 if (p instanceof JComponent)
1907 ((JComponent) p).scrollRectToVisible(r);
1911 * Set the value of the {@link #alignmentX} property.
1913 * @param a The new value of the property
1915 public void setAlignmentX(float a)
1917 alignmentX = a;
1921 * Set the value of the {@link #alignmentY} property.
1923 * @param a The new value of the property
1925 public void setAlignmentY(float a)
1927 alignmentY = a;
1931 * Set the value of the {@link #autoscrolls} property.
1933 * @param a The new value of the property
1935 public void setAutoscrolls(boolean a)
1937 autoscrolls = a;
1941 * Set the value of the {@link #debugGraphicsOptions} property.
1943 * @param debugOptions The new value of the property
1945 public void setDebugGraphicsOptions(int debugOptions)
1947 debugGraphicsOptions = debugOptions;
1951 * Set the value of the {@link #doubleBuffered} property.
1953 * @param db The new value of the property
1955 public void setDoubleBuffered(boolean db)
1957 doubleBuffered = db;
1961 * Set the value of the {@link #enabled} property, revalidate
1962 * and repaint this component.
1964 * @param enable The new value of the property
1966 public void setEnabled(boolean enable)
1968 boolean oldEnabled = isEnabled();
1969 super.setEnabled(enable);
1970 firePropertyChange("enabeld", oldEnabled, enable);
1971 revalidate();
1972 repaint();
1976 * Set the value of the {@link #font} property, revalidate
1977 * and repaint this component.
1979 * @param f The new value of the property
1981 public void setFont(Font f)
1983 super.setFont(f);
1984 revalidate();
1985 repaint();
1989 * Set the value of the {@link #background} property, revalidate
1990 * and repaint this component.
1992 * @param bg The new value of the property
1994 public void setBackground(Color bg)
1996 super.setBackground(bg);
1997 revalidate();
1998 repaint();
2002 * Set the value of the {@link #foreground} property, revalidate
2003 * and repaint this component.
2005 * @param fg The new value of the property
2007 public void setForeground(Color fg)
2009 super.setForeground(fg);
2010 revalidate();
2011 repaint();
2015 * Set the value of the {@link #maximumSize} property, revalidate
2016 * and repaint this component.
2018 * @param max The new value of the property
2020 public void setMaximumSize(Dimension max)
2022 Dimension oldMaximumSize = maximumSize;
2023 maximumSize = max;
2024 firePropertyChange("maximumSize", oldMaximumSize, maximumSize);
2025 revalidate();
2026 repaint();
2030 * Set the value of the {@link #minimumSize} property, revalidate
2031 * and repaint this component.
2033 * @param min The new value of the property
2035 public void setMinimumSize(Dimension min)
2037 Dimension oldMinimumSize = minimumSize;
2038 minimumSize = min;
2039 firePropertyChange("minimumSize", oldMinimumSize, minimumSize);
2040 revalidate();
2041 repaint();
2045 * Set the value of the {@link #preferredSize} property, revalidate
2046 * and repaint this component.
2048 * @param pref The new value of the property
2050 public void setPreferredSize(Dimension pref)
2052 Dimension oldPreferredSize = preferredSize;
2053 preferredSize = pref;
2054 firePropertyChange("preferredSize", oldPreferredSize, preferredSize);
2058 * Set the specified component to be the next component in the
2059 * focus cycle, overriding the {@link FocusTraversalPolicy} for
2060 * this component.
2062 * @param aComponent The component to set as the next focusable
2064 * @deprecated Use FocusTraversalPolicy instead
2066 public void setNextFocusableComponent(Component aComponent)
2071 * Set the value of the {@link #requestFocusEnabled} property.
2073 * @param e The new value of the property
2075 public void setRequestFocusEnabled(boolean e)
2077 requestFocusEnabled = e;
2081 * Get the value of the {@link #transferHandler} property.
2083 * @return The current value of the property
2085 * @see ComponentUI#setTransferHandler
2088 public TransferHandler getTransferHandler()
2090 return transferHandler;
2094 * Set the value of the {@link #transferHandler} property.
2096 * @param newHandler The new value of the property
2098 * @see ComponentUI#getTransferHandler
2101 public void setTransferHandler(TransferHandler newHandler)
2103 if (transferHandler == newHandler)
2104 return;
2106 TransferHandler oldHandler = transferHandler;
2107 transferHandler = newHandler;
2108 firePropertyChange("transferHandler", oldHandler, newHandler);
2112 * Set the value of the {@link #opaque} property, revalidate and repaint
2113 * this component.
2115 * @param isOpaque The new value of the property
2117 * @see ComponentUI#update
2119 public void setOpaque(boolean isOpaque)
2121 boolean oldOpaque = opaque;
2122 opaque = isOpaque;
2123 firePropertyChange("opaque", oldOpaque, opaque);
2124 revalidate();
2125 repaint();
2129 * Set the value of the visible property, and revalidate / repaint the
2130 * component.
2132 * @param v The new value of the property
2134 public void setVisible(boolean v)
2136 super.setVisible(v);
2137 revalidate();
2138 repaint();
2142 * Call {@link paint}.
2144 * @param g The graphics context to paint into
2146 public void update(Graphics g)
2148 paint(g);
2152 * Get the value of the UIClassID property. This property should be a key
2153 * in the {@link UIDefaults} table managed by {@link UIManager}, the
2154 * value of which is the name of a class to load for the component's
2155 * {@link ui} property.
2157 * @return A "symbolic" name which will map to a class to use for the
2158 * component's UI, such as <code>"ComponentUI"</code>
2160 * @see #setUI
2161 * @see #updateUI
2163 public String getUIClassID()
2165 return "ComponentUI";
2169 * Install a new UI delegate as the component's {@link ui} property. In
2170 * the process, this will call {@link ComponentUI.uninstallUI} on any
2171 * existing value for the {@link ui} property, and {@link
2172 * ComponentUI.installUI} on the new UI delegate.
2174 * @param newUI The new UI delegate to install
2176 * @see #updateUI
2177 * @see #getUIClassID
2179 protected void setUI(ComponentUI newUI)
2181 if (ui != null)
2182 ui.uninstallUI(this);
2184 ComponentUI oldUI = ui;
2185 ui = newUI;
2187 if (ui != null)
2188 ui.installUI(this);
2190 firePropertyChange("UI", oldUI, newUI);
2192 revalidate();
2193 repaint();
2197 * This method should be overridden in subclasses. In JComponent, the
2198 * method does nothing. In subclasses, it should a UI delegate
2199 * (corresponding to the symbolic name returned from {@link
2200 * getUIClassID}) from the {@link UIManager}, and calls {@link setUI}
2201 * with the new delegate.
2203 public void updateUI()
2205 System.out.println("update UI not overwritten in class: " + this);
2208 public static Locale getDefaultLocale()
2210 return defaultLocale;
2213 public static void setDefaultLocale(Locale l)
2215 defaultLocale = l;
2219 * Returns the currently set input verifier for this component.
2221 * @return the input verifier, or <code>null</code> if none
2223 public InputVerifier getInputVerifier()
2225 return inputVerifier;
2229 * Sets the input verifier to use by this component.
2231 * @param verifier the input verifier, or <code>null</code>
2233 public void setInputVerifier(InputVerifier verifier)
2235 InputVerifier oldVerifier = inputVerifier;
2236 inputVerifier = verifier;
2237 firePropertyChange("inputVerifier", oldVerifier, verifier);
2241 * @since 1.3
2243 public boolean getVerifyInputWhenFocusTarget()
2245 return verifyInputWhenFocusTarget;
2249 * @since 1.3
2251 public void setVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)
2253 if (this.verifyInputWhenFocusTarget == verifyInputWhenFocusTarget)
2254 return;
2256 this.verifyInputWhenFocusTarget = verifyInputWhenFocusTarget;
2257 firePropertyChange("verifyInputWhenFocusTarget",
2258 ! verifyInputWhenFocusTarget,
2259 verifyInputWhenFocusTarget);