1 /* Component.java -- a graphics component
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
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.dnd.DropTarget;
42 import java.awt.event.ComponentEvent;
43 import java.awt.event.ComponentListener;
44 import java.awt.event.FocusEvent;
45 import java.awt.event.FocusListener;
46 import java.awt.event.HierarchyBoundsListener;
47 import java.awt.event.HierarchyEvent;
48 import java.awt.event.HierarchyListener;
49 import java.awt.event.KeyEvent;
50 import java.awt.event.KeyListener;
51 import java.awt.event.InputMethodEvent;
52 import java.awt.event.InputMethodListener;
53 import java.awt.event.MouseEvent;
54 import java.awt.event.MouseListener;
55 import java.awt.event.MouseMotionListener;
56 import java.awt.event.MouseWheelListener;
57 import java.awt.event.MouseWheelEvent;
58 import java.awt.event.PaintEvent;
59 import java.awt.im.InputContext;
60 import java.awt.im.InputMethodRequests;
61 import java.awt.image.BufferStrategy;
62 import java.awt.image.ColorModel;
63 import java.awt.image.ImageObserver;
64 import java.awt.image.ImageProducer;
65 import java.awt.image.VolatileImage;
66 import java.awt.peer.ComponentPeer;
67 import java.awt.peer.LightweightPeer;
68 import java.beans.PropertyChangeListener;
69 import java.beans.PropertyChangeSupport;
70 import java.io.ObjectInputStream;
71 import java.io.IOException;
72 import java.io.ObjectOutputStream;
73 import java.io.PrintStream;
74 import java.io.PrintWriter;
75 import java.io.Serializable;
76 import java.lang.reflect.Array;
77 import java.util.Collections;
78 import java.util.EventListener;
79 import java.util.HashSet;
80 import java.util.Iterator;
81 import java.util.Locale;
83 import java.util.Vector;
84 import javax.accessibility.Accessible;
85 import javax.accessibility.AccessibleComponent;
86 import javax.accessibility.AccessibleContext;
87 import javax.accessibility.AccessibleRole;
88 import javax.accessibility.AccessibleState;
89 import javax.accessibility.AccessibleStateSet;
92 * The root of all evil. All graphical representations are subclasses of this
93 * giant class, which is designed for screen display and user interaction.
94 * This class can be extended directly to build a lightweight component (one
95 * not associated with a native window); lightweight components must reside
96 * inside a heavyweight window.
98 * <p>This class is Serializable, which has some big implications. A user can
99 * save the state of all graphical components in one VM, and reload them in
100 * another. Note that this class will only save Serializable listeners, and
101 * ignore the rest, without causing any serialization exceptions. However, by
102 * making a listener serializable, and adding it to another element, you link
103 * in that entire element to the state of this component. To get around this,
104 * use the idiom shown in the example below - make listeners non-serializable
105 * in inner classes, rather than using this object itself as the listener, if
106 * external objects do not need to save the state of this object.
110 * import java.awt.event.*;
111 * import java.io.Serializable;
112 * class MyApp implements Serializable
114 * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
115 * // Serializing aButton will not suck in an instance of MyApp, with its
116 * // accompanying field bigOne.
117 * Button aButton = new Button();
118 * class MyActionListener implements ActionListener
120 * public void actionPerformed(ActionEvent e)
122 * System.out.println("Hello There");
127 * aButton.addActionListener(new MyActionListener());
131 * <p>Status: Incomplete. The event dispatch mechanism is implemented. All
132 * other methods defined in the J2SE 1.3 API javadoc exist, but are mostly
133 * incomplete or only stubs; except for methods relating to the Drag and
134 * Drop, Input Method, and Accessibility frameworks: These methods are
135 * present but commented out.
137 * @author original author unknown
138 * @author Eric Blake <ebb9@email.byu.edu>
140 * @status still missing 1.4 support
142 public abstract class Component
143 implements ImageObserver, MenuContainer, Serializable
145 // Word to the wise - this file is huge. Search for '\f' (^L) for logical
146 // sectioning by fields, public API, private API, and nested classes.
150 * Compatible with JDK 1.0+.
152 private static final long serialVersionUID = -7644114512714619750L;
155 * Constant returned by the <code>getAlignmentY</code> method to indicate
156 * that the component wishes to be aligned to the top relative to
159 * @see #getAlignmentY()
161 public static final float TOP_ALIGNMENT = 0;
164 * Constant returned by the <code>getAlignmentY</code> and
165 * <code>getAlignmentX</code> methods to indicate
166 * that the component wishes to be aligned to the center relative to
169 * @see #getAlignmentX()
170 * @see #getAlignmentY()
172 public static final float CENTER_ALIGNMENT = 0.5f;
175 * Constant returned by the <code>getAlignmentY</code> method to indicate
176 * that the component wishes to be aligned to the bottom relative to
179 * @see #getAlignmentY()
181 public static final float BOTTOM_ALIGNMENT = 1;
184 * Constant returned by the <code>getAlignmentX</code> method to indicate
185 * that the component wishes to be aligned to the right relative to
188 * @see #getAlignmentX()
190 public static final float RIGHT_ALIGNMENT = 1;
193 * Constant returned by the <code>getAlignmentX</code> method to indicate
194 * that the component wishes to be aligned to the left relative to
197 * @see #getAlignmentX()
199 public static final float LEFT_ALIGNMENT = 0;
202 * Make the treelock a String so that it can easily be identified
203 * in debug dumps. We clone the String in order to avoid a conflict in
204 * the unlikely event that some other package uses exactly the same string
207 static final Object treeLock = new String("AWT_TREE_LOCK");
209 // Serialized fields from the serialization spec.
212 * The x position of the component in the parent's coordinate system.
214 * @see #getLocation()
215 * @serial the x position
220 * The y position of the component in the parent's coordinate system.
222 * @see #getLocation()
223 * @serial the y position
228 * The component width.
236 * The component height.
244 * The foreground color for the component. This may be null.
246 * @see #getForeground()
247 * @see #setForeground(Color)
248 * @serial the foreground color
253 * The background color for the component. This may be null.
255 * @see #getBackground()
256 * @see #setBackground(Color)
257 * @serial the background color
262 * The default font used in the component. This may be null.
265 * @see #setFont(Font)
271 * The font in use by the peer, or null if there is no peer.
273 * @serial the peer's font
278 * The cursor displayed when the pointer is over this component. This may
282 * @see #setCursor(Cursor)
287 * The locale for the component.
290 * @see #setLocale(Locale)
295 * True if the object should ignore repaint events (usually because it is
298 * @see #getIgnoreRepaint()
299 * @see #setIgnoreRepaint(boolean)
300 * @serial true to ignore repaints
303 boolean ignoreRepaint;
306 * True when the object is visible (although it is only showing if all
307 * ancestors are likewise visible). For component, this defaults to true.
310 * @see #setVisible(boolean)
311 * @serial true if visible
313 boolean visible = true;
316 * True if the object is enabled, meaning it can interact with the user.
317 * For component, this defaults to true.
320 * @see #setEnabled(boolean)
321 * @serial true if enabled
323 boolean enabled = true;
326 * True if the object is valid. This is set to false any time a size
327 * adjustment means the component need to be layed out again.
332 * @serial true if layout is valid
337 * The DropTarget for drag-and-drop operations.
339 * @see #getDropTarget()
340 * @see #setDropTarget(DropTarget)
341 * @serial the drop target, or null
344 DropTarget dropTarget;
347 * The list of popup menus for this component.
349 * @see #add(PopupMenu)
350 * @serial the list of popups
355 * The component's name. May be null, in which case a default name is
356 * generated on the first use.
359 * @see #setName(String)
365 * True once the user has set the name. Note that the user may set the name
370 * @see #setName(String)
371 * @serial true if the name has been explicitly set
373 boolean nameExplicitlySet;
376 * Indicates if the object can be focused. Defaults to true for components.
378 * @see #isFocusable()
379 * @see #setFocusable(boolean)
382 boolean focusable = true;
385 * Tracks whether this component uses default focus traversal, or has a
388 * @see #isFocusTraversableOverridden()
391 int isFocusTraversableOverridden;
394 * The focus traversal keys, if not inherited from the parent or default
395 * keyboard manager. These sets will contain only AWTKeyStrokes that
396 * represent press and release events to use as focus control.
398 * @see #getFocusTraversalKeys(int)
399 * @see #setFocusTraversalKeys(int, Set)
402 Set[] focusTraversalKeys;
405 * True if focus traversal keys are enabled. This defaults to true for
406 * Component. If this is true, keystrokes in focusTraversalKeys are trapped
407 * and processed automatically rather than being passed on to the component.
409 * @see #getFocusTraversalKeysEnabled()
410 * @see #setFocusTraversalKeysEnabled(boolean)
413 boolean focusTraversalKeysEnabled = true;
416 * Cached information on the minimum size. Should have been transient.
423 * Cached information on the preferred size. Should have been transient.
430 * Set to true if an event is to be handled by this component, false if
431 * it is to be passed up the hierarcy.
433 * @see #dispatchEvent(AWTEvent)
434 * @serial true to process event locally
436 boolean newEventsOnly;
439 * Set by subclasses to enable event handling of particular events, and
440 * left alone when modifying listeners. For component, this defaults to
441 * enabling only input methods.
443 * @see #enableInputMethods(boolean)
445 * @serial the mask of events to process
447 long eventMask = AWTEvent.INPUT_ENABLED_EVENT_MASK;
450 * Describes all registered PropertyChangeListeners.
452 * @see #addPropertyChangeListener(PropertyChangeListener)
453 * @see #removePropertyChangeListener(PropertyChangeListener)
454 * @see #firePropertyChange(String, Object, Object)
455 * @serial the property change listeners
458 PropertyChangeSupport changeSupport;
461 * True if the component has been packed (layed out).
463 * @serial true if this is packed
468 * The serialization version for this class. Currently at version 4.
470 * XXX How do we handle prior versions?
472 * @serial the serialization version
474 int componentSerializedDataVersion = 4;
477 * The accessible context associated with this component. This is only set
480 * @see #getAccessibleContext()
481 * @serial the accessibility context
484 AccessibleContext accessibleContext;
487 // Guess what - listeners are special cased in serialization. See
488 // readObject and writeObject.
490 /** Component listener chain. */
491 transient ComponentListener componentListener;
493 /** Focus listener chain. */
494 transient FocusListener focusListener;
496 /** Key listener chain. */
497 transient KeyListener keyListener;
499 /** Mouse listener chain. */
500 transient MouseListener mouseListener;
502 /** Mouse motion listener chain. */
503 transient MouseMotionListener mouseMotionListener;
506 * Mouse wheel listener chain.
510 transient MouseWheelListener mouseWheelListener;
513 * Input method listener chain.
517 transient InputMethodListener inputMethodListener;
520 * Hierarcy listener chain.
524 transient HierarchyListener hierarchyListener;
527 * Hierarcy bounds listener chain.
531 transient HierarchyBoundsListener hierarchyBoundsListener;
533 // Anything else is non-serializable, and should be declared "transient".
536 transient Container parent;
538 /** The associated native peer. */
539 transient ComponentPeer peer;
541 /** The preferred component orientation. */
542 transient ComponentOrientation orientation = ComponentOrientation.UNKNOWN;
545 * The associated graphics configuration.
549 transient GraphicsConfiguration graphicsConfig;
552 * The buffer strategy for repainting.
556 transient BufferStrategy bufferStrategy;
559 * The system properties that affect image updating.
561 private static transient boolean incrementalDraw;
562 private static transient Long redrawRate;
566 incrementalDraw = Boolean.getBoolean ("awt.image.incrementalDraw");
567 redrawRate = Long.getLong ("awt.image.redrawrate");
570 // Public and protected API.
573 * Default constructor for subclasses. When Component is extended directly,
574 * it forms a lightweight component that must be hosted in an opaque native
575 * container higher in the tree.
577 protected Component()
582 * Returns the name of this component.
584 * @return the name of this component
585 * @see #setName(String)
588 public String getName()
590 if (name == null && ! nameExplicitlySet)
591 name = generateName();
596 * Sets the name of this component to the specified name.
598 * @param name the new name of this component
602 public void setName(String name)
604 nameExplicitlySet = true;
609 * Returns the parent of this component.
611 * @return the parent of this component
613 public Container getParent()
619 * Returns the native windowing system peer for this component. Only the
620 * platform specific implementation code should call this method.
622 * @return the peer for this component
623 * @deprecated user programs should not directly manipulate peers; use
624 * {@link #isDisplayable()} instead
626 // Classpath's Gtk peers rely on this.
627 public ComponentPeer getPeer()
633 * Set the associated drag-and-drop target, which receives events when this
636 * @param dt the new drop target
639 public void setDropTarget(DropTarget dt)
641 this.dropTarget = dt;
645 * Gets the associated drag-and-drop target, if there is one.
647 * @return the drop target
649 public DropTarget getDropTarget()
655 * Returns the graphics configuration of this component, if there is one.
656 * If it has not been set, it is inherited from the parent.
658 * @return the graphics configuration, or null
661 public GraphicsConfiguration getGraphicsConfiguration()
663 return getGraphicsConfigurationImpl();
667 * Returns the object used for synchronization locks on this component
668 * when performing tree and layout functions.
670 * @return the synchronization lock for this component
672 public final Object getTreeLock()
678 * Returns the toolkit in use for this component. The toolkit is associated
679 * with the frame this component belongs to.
681 * @return the toolkit for this component
683 public Toolkit getToolkit()
687 Toolkit tk = peer.getToolkit();
691 // Get toolkit for lightweight component.
693 return parent.getToolkit();
694 return Toolkit.getDefaultToolkit();
698 * Tests whether or not this component is valid. A invalid component needs
699 * to have its layout redone.
701 * @return true if this component is valid
705 public boolean isValid()
711 * Tests if the component is displayable. It must be connected to a native
712 * screen resource, and all its ancestors must be displayable. A containment
713 * hierarchy is made displayable when a window is packed or made visible.
715 * @return true if the component is displayable
716 * @see Container#add(Component)
717 * @see Container#remove(Component)
720 * @see Window#dispose()
723 public boolean isDisplayable()
726 return parent.isDisplayable();
731 * Tests whether or not this component is visible. Except for top-level
732 * frames, components are initially visible.
734 * @return true if the component is visible
735 * @see #setVisible(boolean)
737 public boolean isVisible()
743 * Tests whether or not this component is actually being shown on
744 * the screen. This will be true if and only if it this component is
745 * visible and its parent components are all visible.
747 * @return true if the component is showing on the screen
748 * @see #setVisible(boolean)
750 public boolean isShowing()
752 if (! visible || peer == null)
755 return parent == null ? true : parent.isShowing();
759 * Tests whether or not this component is enabled. Components are enabled
760 * by default, and must be enabled to receive user input or generate events.
762 * @return true if the component is enabled
763 * @see #setEnabled(boolean)
765 public boolean isEnabled()
771 * Enables or disables this component. The component must be enabled to
772 * receive events (except that lightweight components always receive mouse
775 * @param enabled true to enable this component
777 * @see #isLightweight()
780 public void setEnabled(boolean b)
786 * Enables this component.
788 * @deprecated use {@link #setEnabled(boolean)} instead
794 peer.setEnabled (true);
798 * Enables or disables this component.
800 * @param enabled true to enable this component
801 * @deprecated use {@link #setEnabled(boolean)} instead
803 public void enable(boolean b)
812 * Disables this component.
814 * @deprecated use {@link #setEnabled(boolean)} instead
816 public void disable()
818 this.enabled = false;
820 peer.setEnabled (false);
824 * Checks if this image is painted to an offscreen image buffer that is
825 * later copied to screen (double buffering reduces flicker). This version
826 * returns false, so subclasses must override it if they provide double
829 * @return true if this is double buffered; defaults to false
831 public boolean isDoubleBuffered()
837 * Enables or disables input method support for this component. By default,
838 * components have this enabled. Input methods are given the opportunity
839 * to process key events before this component and its listeners.
841 * @param enable true to enable input method processing
842 * @see #processKeyEvent(KeyEvent)
845 public void enableInputMethods(boolean enable)
848 throw new Error("not implemented");
852 * Makes this component visible or invisible. Note that it wtill might
853 * not show the component, if a parent is invisible.
855 * @param visible true to make this component visible
859 public void setVisible(boolean b)
861 // Inspection by subclassing shows that Sun's implementation calls
862 // show(boolean) which then calls show() or hide(). It is the show()
863 // method that is overriden in subclasses like Window.
868 * Makes this component visible on the screen.
870 * @deprecated use {@link #setVisible(boolean)} instead
874 // We must set visible before showing the peer. Otherwise the
875 // peer could post paint events before visible is true, in which
876 // case lightweight components are not initially painted --
877 // Container.paint first calls isShowing () before painting itself
881 peer.setVisible(true);
886 * Makes this component visible or invisible.
888 * @param visible true to make this component visible
889 * @deprecated use {@link #setVisible(boolean)} instead
891 public void show(boolean b)
900 * Hides this component so that it is no longer shown on the screen.
902 * @deprecated use {@link #setVisible(boolean)} instead
907 peer.setVisible(false);
908 this.visible = false;
913 * Returns this component's foreground color. If not set, this is inherited
916 * @return this component's foreground color, or null
917 * @see #setForeground(Color)
919 public Color getForeground()
921 if (foreground != null)
923 return parent == null ? null : parent.getForeground();
927 * Sets this component's foreground color to the specified color. This is a
930 * @param c the new foreground color
931 * @see #getForeground()
933 public void setForeground(Color c)
935 firePropertyChange("foreground", foreground, c);
937 peer.setForeground(c);
942 * Tests if the foreground was explicitly set, or just inherited from the
945 * @return true if the foreground has been set
948 public boolean isForegroundSet()
950 return foreground != null;
954 * Returns this component's background color. If not set, this is inherited
957 * @return the background color of the component, or null
958 * @see #setBackground(Color)
960 public Color getBackground()
962 if (background != null)
964 return parent == null ? null : parent.getBackground();
968 * Sets this component's background color to the specified color. The parts
969 * of the component affected by the background color may by system dependent.
970 * This is a bound property.
972 * @param c the new background color
973 * @see #getBackground()
975 public void setBackground(Color c)
977 firePropertyChange("background", background, c);
979 peer.setBackground(c);
984 * Tests if the background was explicitly set, or just inherited from the
987 * @return true if the background has been set
990 public boolean isBackgroundSet()
992 return background != null;
996 * Returns the font in use for this component. If not set, this is inherited
999 * @return the font for this component
1000 * @see #setFont(Font)
1002 public Font getFont()
1008 return parent.getFont ();
1010 return new Font ("Fixed", Font.PLAIN, 12);
1014 * Sets the font for this component to the specified font. This is a bound
1017 * @param font the new font for this component
1020 public void setFont(Font f)
1022 firePropertyChange("font", font, f);
1029 * Tests if the font was explicitly set, or just inherited from the parent.
1031 * @return true if the font has been set
1034 public boolean isFontSet()
1036 return font != null;
1040 * Returns the locale for this component. If this component does not
1041 * have a locale, the locale of the parent component is returned.
1043 * @return the locale for this component
1044 * @throws IllegalComponentStateException if it has no locale or parent
1045 * @see setLocale(Locale)
1048 public Locale getLocale()
1053 throw new IllegalComponentStateException
1054 ("Component has no parent: can't determine Locale");
1055 return parent.getLocale();
1059 * Sets the locale for this component to the specified locale. This is a
1062 * @param locale the new locale for this component
1064 public void setLocale(Locale l)
1066 firePropertyChange("locale", locale, l);
1068 // New writing/layout direction or more/less room for localized labels.
1073 * Returns the color model of the device this componet is displayed on.
1075 * @return this object's color model
1076 * @see Toolkit#getColorModel()
1078 public ColorModel getColorModel()
1080 GraphicsConfiguration config = getGraphicsConfiguration();
1081 return config != null ? config.getColorModel()
1082 : getToolkit().getColorModel();
1086 * Returns the location of this component's top left corner relative to
1087 * its parent component. This may be outdated, so for synchronous behavior,
1088 * you should use a component listner.
1090 * @return the location of this component
1091 * @see #setLocation(int, int)
1092 * @see #getLocationOnScreen()
1095 public Point getLocation()
1101 * Returns the location of this component's top left corner in screen
1104 * @return the location of this component in screen coordinates
1105 * @throws IllegalComponentStateException if the component is not showing
1107 public Point getLocationOnScreen()
1110 throw new IllegalComponentStateException("component not showing");
1111 // We know peer != null here.
1112 return peer.getLocationOnScreen();
1116 * Returns the location of this component's top left corner relative to
1117 * its parent component.
1119 * @return the location of this component
1120 * @deprecated use {@link #getLocation()} instead
1122 public Point location()
1124 return new Point (x, y);
1128 * Moves this component to the specified location, relative to the parent's
1129 * coordinates. The coordinates are the new upper left corner of this
1132 * @param x the new X coordinate of this component
1133 * @param y the new Y coordinate of this component
1134 * @see #getLocation()
1135 * @see #setBounds(int, int, int, int)
1137 public void setLocation(int x, int y)
1143 * Moves this component to the specified location, relative to the parent's
1144 * coordinates. The coordinates are the new upper left corner of this
1147 * @param x the new X coordinate of this component
1148 * @param y the new Y coordinate of this component
1149 * @deprecated use {@link #setLocation(int, int)} instead
1151 public void move(int x, int y)
1153 if (this.x == x && this.y == y)
1159 peer.setBounds (x, y, width, height);
1163 * Moves this component to the specified location, relative to the parent's
1164 * coordinates. The coordinates are the new upper left corner of this
1167 * @param p new coordinates for this component
1168 * @throws NullPointerException if p is null
1169 * @see #getLocation()
1170 * @see #setBounds(int, int, int, int)
1173 public void setLocation(Point p)
1175 setLocation(p.x, p.y);
1179 * Returns the size of this object.
1181 * @return the size of this object
1182 * @see #setSize(int, int)
1185 public Dimension getSize()
1191 * Returns the size of this object.
1193 * @return the size of this object
1194 * @deprecated use {@link #getSize()} instead
1196 public Dimension size()
1198 return new Dimension (width, height);
1202 * Sets the size of this component to the specified width and height.
1204 * @param width the new width of this component
1205 * @param height the new height of this component
1207 * @see #setBounds(int, int, int, int)
1209 public void setSize(int width, int height)
1211 resize (width, height);
1215 * Sets the size of this component to the specified value.
1217 * @param width the new width of the component
1218 * @param height the new height of the component
1219 * @deprecated use {@link #setSize(int, int)} instead
1221 public void resize(int width, int height)
1223 if (this.width == width && this.height == height)
1227 this.height = height;
1229 peer.setBounds (x, y, width, height);
1233 * Sets the size of this component to the specified value.
1235 * @param d the new size of this component
1236 * @throws NullPointerException if d is null
1237 * @see #setSize(int, int)
1238 * @see #setBounds(int, int, int, int)
1241 public void setSize(Dimension d)
1247 * Sets the size of this component to the specified value.
1249 * @param d the new size of this component
1250 * @throws NullPointerException if d is null
1251 * @deprecated use {@link #setSize(Dimension)} instead
1253 public void resize(Dimension d)
1255 resize (d.width, d.height);
1259 * Returns a bounding rectangle for this component. Note that the
1260 * returned rectange is relative to this component's parent, not to
1263 * @return the bounding rectangle for this component
1264 * @see #setBounds(int, int, int, int)
1265 * @see #getLocation()
1268 public Rectangle getBounds()
1274 * Returns a bounding rectangle for this component. Note that the
1275 * returned rectange is relative to this component's parent, not to
1278 * @return the bounding rectangle for this component
1279 * @deprecated use {@link #getBounds()} instead
1281 public Rectangle bounds()
1283 return new Rectangle (x, y, width, height);
1287 * Sets the bounding rectangle for this component to the specified values.
1288 * Note that these coordinates are relative to the parent, not to the screen.
1290 * @param x the X coordinate of the upper left corner of the rectangle
1291 * @param y the Y coordinate of the upper left corner of the rectangle
1292 * @param w the width of the rectangle
1293 * @param h the height of the rectangle
1295 * @see #setLocation(int, int)
1296 * @see #setLocation(Point)
1297 * @see #setSize(int, int)
1298 * @see #setSize(Dimension)
1301 public void setBounds(int x, int y, int w, int h)
1303 reshape (x, y, w, h);
1307 * Sets the bounding rectangle for this component to the specified values.
1308 * Note that these coordinates are relative to the parent, not to the screen.
1310 * @param x the X coordinate of the upper left corner of the rectangle
1311 * @param y the Y coordinate of the upper left corner of the rectangle
1312 * @param width the width of the rectangle
1313 * @param height the height of the rectangle
1314 * @deprecated use {@link #setBounds(int, int, int, int)} instead
1316 public void reshape(int x, int y, int width, int height)
1318 if (this.x == x && this.y == y
1319 && this.width == width && this.height == height)
1325 this.height = height;
1327 peer.setBounds (x, y, width, height);
1331 * Sets the bounding rectangle for this component to the specified
1332 * rectangle. Note that these coordinates are relative to the parent, not
1335 * @param r the new bounding rectangle
1336 * @throws NullPointerException if r is null
1338 * @see #setLocation(Point)
1339 * @see #setSize(Dimension)
1342 public void setBounds(Rectangle r)
1344 setBounds (r.x, r.y, r.width, r.height);
1348 * Gets the x coordinate of the upper left corner. This is more efficient
1349 * than getBounds().x or getLocation().x.
1351 * @return the current x coordinate
1360 * Gets the y coordinate of the upper left corner. This is more efficient
1361 * than getBounds().y or getLocation().y.
1363 * @return the current y coordinate
1372 * Gets the width of the component. This is more efficient than
1373 * getBounds().width or getSize().width.
1375 * @return the current width
1378 public int getWidth()
1384 * Gets the height of the component. This is more efficient than
1385 * getBounds().height or getSize().height.
1387 * @return the current width
1390 public int getHeight()
1396 * Returns the bounds of this component. This allows reuse of an existing
1397 * rectangle, if r is non-null.
1399 * @param r the rectangle to use, or null
1400 * @return the bounds
1402 public Rectangle getBounds(Rectangle r)
1405 r = new Rectangle();
1414 * Returns the size of this component. This allows reuse of an existing
1415 * dimension, if d is non-null.
1417 * @param d the dimension to use, or null
1420 public Dimension getSize(Dimension d)
1423 d = new Dimension();
1430 * Returns the location of this component. This allows reuse of an existing
1431 * point, if p is non-null.
1433 * @param p the point to use, or null
1434 * @return the location
1436 public Point getLocation(Point p)
1446 * Tests if this component is opaque. All "heavyweight" (natively-drawn)
1447 * components are opaque. A component is opaque if it draws all pixels in
1448 * the bounds; a lightweight component is partially transparent if it lets
1449 * pixels underneath show through. Subclasses that guarantee that all pixels
1450 * will be drawn should override this.
1452 * @return true if this is opaque
1453 * @see #isLightweight()
1456 public boolean isOpaque()
1458 return ! isLightweight();
1462 * Return whether the component is lightweight. That means the component has
1463 * no native peer, but is displayable. This applies to subclasses of
1464 * Component not in this package, such as javax.swing.
1466 * @return true if the component has a lightweight peer
1467 * @see #isDisplayable()
1470 public boolean isLightweight()
1472 return peer instanceof LightweightPeer;
1476 * Returns the component's preferred size.
1478 * @return the component's preferred size
1479 * @see #getMinimumSize()
1480 * @see LayoutManager
1482 public Dimension getPreferredSize()
1484 return preferredSize();
1488 * Returns the component's preferred size.
1490 * @return the component's preferred size
1491 * @deprecated use {@link #getPreferredSize()} instead
1493 public Dimension preferredSize()
1495 if (prefSize == null)
1497 return new Dimension(width, height);
1499 prefSize = peer.getPreferredSize();
1504 * Returns the component's minimum size.
1506 * @return the component's minimum size
1507 * @see #getPreferredSize()
1508 * @see LayoutManager
1510 public Dimension getMinimumSize()
1512 return minimumSize();
1516 * Returns the component's minimum size.
1518 * @return the component's minimum size
1519 * @deprecated use {@link #getMinimumSize()} instead
1521 public Dimension minimumSize()
1523 if (minSize == null)
1524 minSize = (peer != null ? peer.getMinimumSize()
1525 : new Dimension(width, height));
1530 * Returns the component's maximum size.
1532 * @return the component's maximum size
1533 * @see #getMinimumSize()
1534 * @see #getPreferredSize()
1535 * @see LayoutManager
1537 public Dimension getMaximumSize()
1539 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1543 * Returns the preferred horizontal alignment of this component. The value
1544 * returned will be between {@link #LEFT_ALIGNMENT} and
1545 * {@link #RIGHT_ALIGNMENT}, inclusive.
1547 * @return the preferred horizontal alignment of this component
1549 public float getAlignmentX()
1551 return CENTER_ALIGNMENT;
1555 * Returns the preferred vertical alignment of this component. The value
1556 * returned will be between {@link #TOP_ALIGNMENT} and
1557 * {@link #BOTTOM_ALIGNMENT}, inclusive.
1559 * @return the preferred vertical alignment of this component
1561 public float getAlignmentY()
1563 return CENTER_ALIGNMENT;
1567 * Calls the layout manager to re-layout the component. This is called
1568 * during validation of a container in most cases.
1571 * @see LayoutManager
1573 public void doLayout()
1579 * Calls the layout manager to re-layout the component. This is called
1580 * during validation of a container in most cases.
1582 * @deprecated use {@link #doLayout()} instead
1584 public void layout()
1586 // Nothing to do unless we're a container.
1590 * Called to ensure that the layout for this component is valid. This is
1591 * usually called on containers.
1593 * @see #invalidate()
1595 * @see LayoutManager
1596 * @see Container#validate()
1598 public void validate()
1604 * Invalidates this component and all of its parent components. This will
1605 * cause them to have their layout redone. This is called frequently, so
1608 public void invalidate()
1613 if (parent != null && parent.valid)
1614 parent.invalidate();
1618 * Returns a graphics object for this component. Returns <code>null</code>
1619 * if this component is not currently displayed on the screen.
1621 * @return a graphics object for this component
1622 * @see #paint(Graphics)
1624 public Graphics getGraphics()
1628 Graphics gfx = peer.getGraphics();
1631 // create graphics for lightweight:
1632 Container parent = getParent();
1635 gfx = parent.getGraphics();
1636 Rectangle bounds = getBounds();
1637 gfx.setClip(bounds);
1638 gfx.translate(bounds.x, bounds.y);
1646 * Returns the font metrics for the specified font in this component.
1648 * @param font the font to retrieve metrics for
1649 * @return the font metrics for the specified font
1650 * @throws NullPointerException if font is null
1652 * @see Toolkit#getFontMetrics(Font)
1654 public FontMetrics getFontMetrics(Font font)
1656 return peer == null ? getToolkit().getFontMetrics(font)
1657 : peer.getFontMetrics(font);
1661 * Sets the cursor for this component to the specified cursor. The cursor
1662 * is displayed when the point is contained by the component, and the
1663 * component is visible, displayable, and enabled. This is inherited by
1664 * subcomponents unless they set their own cursor.
1666 * @param cursor the new cursor for this component
1670 * @see #contains(int, int)
1671 * @see Toolkit#createCustomCursor(Image, Point, String)
1673 public void setCursor(Cursor cursor)
1675 this.cursor = cursor;
1677 peer.setCursor(cursor);
1681 * Returns the cursor for this component. If not set, this is inherited
1682 * from the parent, or from Cursor.getDefaultCursor().
1684 * @return the cursor for this component
1686 public Cursor getCursor()
1690 return parent != null ? parent.getCursor() : Cursor.getDefaultCursor();
1694 * Tests if the cursor was explicitly set, or just inherited from the parent.
1696 * @return true if the cursor has been set
1699 public boolean isCursorSet()
1701 return cursor != null;
1705 * Paints this component on the screen. The clipping region in the graphics
1706 * context will indicate the region that requires painting. This is called
1707 * whenever the component first shows, or needs to be repaired because
1708 * something was temporarily drawn on top. It is not necessary for
1709 * subclasses to call <code>super.paint(g)</code>. Components with no area
1712 * @param g the graphics context for this paint job
1713 * @see #update(Graphics)
1715 public void paint(Graphics g)
1717 // Paint the heavyweight peer
1718 if (!isLightweight() && peer != null)
1723 * Updates this component. This is called in response to
1724 * <code>repaint</code>. This method fills the component with the
1725 * background color, then sets the foreground color of the specified
1726 * graphics context to the foreground color of this component and calls
1727 * the <code>paint()</code> method. The coordinates of the graphics are
1728 * relative to this component. Subclasses should call either
1729 * <code>super.update(g)</code> or <code>paint(g)</code>.
1731 * @param graphics the graphics context for this update
1732 * @see #paint(Graphics)
1735 public void update(Graphics g)
1737 if (!isLightweight())
1739 Rectangle clip = g.getClipBounds();
1741 g.clearRect(0, 0, width, height);
1743 g.clearRect(clip.x, clip.y, clip.width, clip.height);
1750 * Paints this entire component, including any sub-components.
1752 * @param graphics the graphics context for this paint job
1753 * @see #paint(Graphics)
1755 public void paintAll(Graphics g)
1763 * Repaint this entire component. The <code>update()</code> method
1764 * on this component will be called as soon as possible.
1766 * @see #update(Graphics)
1767 * @see #repaint(long, int, int, int, int)
1769 public void repaint()
1771 repaint(0, 0, 0, width, height);
1775 * Repaint this entire component. The <code>update()</code> method on this
1776 * component will be called in approximate the specified number of
1779 * @param tm milliseconds before this component should be repainted
1780 * @see #paint(Graphics)
1781 * @see #repaint(long, int, int, int, int)
1783 public void repaint(long tm)
1785 repaint(tm, 0, 0, width, height);
1789 * Repaints the specified rectangular region within this component. The
1790 * <code>update</code> method on this component will be called as soon as
1791 * possible. The coordinates are relative to this component.
1793 * @param x the X coordinate of the upper left of the region to repaint
1794 * @param y the Y coordinate of the upper left of the region to repaint
1795 * @param w the width of the region to repaint
1796 * @param h the height of the region to repaint
1797 * @see #update(Graphics)
1798 * @see #repaint(long, int, int, int, int)
1800 public void repaint(int x, int y, int w, int h)
1802 repaint(0, x, y, w, h);
1806 * Repaints the specified rectangular region within this component. The
1807 * <code>update</code> method on this component will be called in
1808 * approximately the specified number of milliseconds. The coordinates
1809 * are relative to this component.
1811 * @param tm milliseconds before this component should be repainted
1812 * @param x the X coordinate of the upper left of the region to repaint
1813 * @param y the Y coordinate of the upper left of the region to repaint
1814 * @param w the width of the region to repaint
1815 * @param h the height of the region to repaint
1816 * @see #update(Graphics)
1818 public void repaint(long tm, int x, int y, int width, int height)
1820 // Handle lightweight repainting by forwarding to native parent
1821 if (isLightweight() && parent != null)
1824 parent.repaint(tm, x + getX(), y + getY(), width, height);
1826 else if (peer != null)
1827 peer.repaint(tm, x, y, width, height);
1831 * Prints this component. This method is provided so that printing can be
1832 * done in a different manner from painting. However, the implementation
1833 * in this class simply calls the <code>paint()</code> method.
1835 * @param graphics the graphics context of the print device
1836 * @see #paint(Graphics)
1838 public void print(Graphics g)
1844 * Prints this component, including all sub-components. This method is
1845 * provided so that printing can be done in a different manner from
1846 * painting. However, the implementation in this class simply calls the
1847 * <code>paintAll()</code> method.
1849 * @param graphics the graphics context of the print device
1850 * @see #paintAll(Graphics)
1852 public void printAll(Graphics g)
1858 * Called when an image has changed so that this component is repainted.
1859 * This incrementally draws an image as more bits are available, when
1860 * possible. Incremental drawing is enabled if the system property
1861 * <code>awt.image.incrementalDraw</code> is not present or is true, in which
1862 * case the redraw rate is set to 100ms or the value of the system property
1863 * <code>awt.image.redrawrate</code>.
1865 * <p>The coordinate system used depends on the particular flags.
1867 * @param image the image that has been updated
1868 * @param flags tlags as specified in <code>ImageObserver</code>
1869 * @param x the X coordinate
1870 * @param y the Y coordinate
1871 * @param w the width
1872 * @param h the height
1873 * @return false if the image is completely loaded, loading has been
1874 * aborted, or an error has occurred. true if more updates are
1876 * @see ImageObserver
1877 * @see Graphics#drawImage(Image, int, int, Color, ImageObserver)
1878 * @see Graphics#drawImage(Image, int, int, ImageObserver)
1879 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
1880 * @see Graphics#drawImage(Image, int, int, int, int, ImageObserver)
1881 * @see ImageObserver#update(Image, int, int, int, int, int)
1883 public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
1885 if ((flags & (FRAMEBITS | ALLBITS)) != 0)
1887 else if ((flags & SOMEBITS) != 0)
1889 if (incrementalDraw)
1891 if (redrawRate != null)
1893 long tm = redrawRate.longValue();
1902 return (flags & (ALLBITS | ABORT | ERROR)) == 0;
1906 * Creates an image from the specified producer.
1908 * @param producer the image procedure to create the image from
1909 * @return the resulting image
1911 public Image createImage(ImageProducer producer)
1913 // Sun allows producer to be null.
1915 return peer.createImage(producer);
1917 return getToolkit().createImage(producer);
1921 * Creates an image with the specified width and height for use in
1922 * double buffering. Headless environments do not support images.
1924 * @param width the width of the image
1925 * @param height the height of the image
1926 * @return the requested image, or null if it is not supported
1928 public Image createImage (int width, int height)
1930 Image returnValue = null;
1931 if (!GraphicsEnvironment.isHeadless ())
1933 if (isLightweight () && parent != null)
1934 returnValue = parent.createImage (width, height);
1935 else if (peer != null)
1936 returnValue = peer.createImage (width, height);
1942 * Creates an image with the specified width and height for use in
1943 * double buffering. Headless environments do not support images.
1945 * @param width the width of the image
1946 * @param height the height of the image
1947 * @return the requested image, or null if it is not supported
1950 public VolatileImage createVolatileImage(int width, int height)
1952 if (GraphicsEnvironment.isHeadless())
1954 GraphicsConfiguration config = getGraphicsConfiguration();
1955 return config == null ? null
1956 : config.createCompatibleVolatileImage(width, height);
1960 * Creates an image with the specified width and height for use in
1961 * double buffering. Headless environments do not support images. The image
1962 * will support the specified capabilities.
1964 * @param width the width of the image
1965 * @param height the height of the image
1966 * @param caps the requested capabilities
1967 * @return the requested image, or null if it is not supported
1968 * @throws AWTException if a buffer with the capabilities cannot be created
1971 public VolatileImage createVolatileImage(int width, int height,
1972 ImageCapabilities caps)
1975 if (GraphicsEnvironment.isHeadless())
1977 GraphicsConfiguration config = getGraphicsConfiguration();
1978 return config == null ? null
1979 : config.createCompatibleVolatileImage(width, height, caps);
1983 * Prepares the specified image for rendering on this component.
1985 * @param image the image to prepare for rendering
1986 * @param observer the observer to notify of image preparation status
1987 * @return true if the image is already fully prepared
1988 * @throws NullPointerException if image is null
1990 public boolean prepareImage(Image image, ImageObserver observer)
1992 return prepareImage(image, image.getWidth(observer),
1993 image.getHeight(observer), observer);
1997 * Prepares the specified image for rendering on this component at the
1998 * specified scaled width and height
2000 * @param image the image to prepare for rendering
2001 * @param width the scaled width of the image
2002 * @param height the scaled height of the image
2003 * @param observer the observer to notify of image preparation status
2004 * @return true if the image is already fully prepared
2006 public boolean prepareImage(Image image, int width, int height,
2007 ImageObserver observer)
2010 return peer.prepareImage(image, width, height, observer);
2012 return getToolkit().prepareImage(image, width, height, observer);
2016 * Returns the status of the loading of the specified image. The value
2017 * returned will be those flags defined in <code>ImageObserver</code>.
2019 * @param image the image to check on
2020 * @param observer the observer to notify of image loading progress
2021 * @return the image observer flags indicating the status of the load
2022 * @see #prepareImage(Image, int, int, ImageObserver)
2023 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
2024 * @throws NullPointerException if image is null
2026 public int checkImage(Image image, ImageObserver observer)
2028 return checkImage(image, -1, -1, observer);
2032 * Returns the status of the loading of the specified image. The value
2033 * returned will be those flags defined in <code>ImageObserver</code>.
2035 * @param image the image to check on
2036 * @param width the scaled image width
2037 * @param height the scaled image height
2038 * @param observer the observer to notify of image loading progress
2039 * @return the image observer flags indicating the status of the load
2040 * @see #prepareImage(Image, int, int, ImageObserver)
2041 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
2043 public int checkImage(Image image, int width, int height,
2044 ImageObserver observer)
2047 return peer.checkImage(image, width, height, observer);
2048 return getToolkit().checkImage(image, width, height, observer);
2052 * Sets whether paint messages delivered by the operating system should be
2053 * ignored. This does not affect messages from AWT, except for those
2054 * triggered by OS messages. Setting this to true can allow faster
2055 * performance in full-screen mode or page-flipping.
2057 * @param ignoreRepaint the new setting for ignoring repaint events
2058 * @see #getIgnoreRepaint()
2059 * @see BufferStrategy
2060 * @see GraphicsDevice.setFullScreenWindow(Window)
2063 public void setIgnoreRepaint(boolean ignoreRepaint)
2065 this.ignoreRepaint = ignoreRepaint;
2069 * Test whether paint events from the operating system are ignored.
2071 * @return the status of ignoring paint events
2072 * @see #setIgnoreRepaint(boolean)
2075 public boolean getIgnoreRepaint()
2077 return ignoreRepaint;
2081 * Tests whether or not the specified point is contained within this
2082 * component. Coordinates are relative to this component.
2084 * @param x the X coordinate of the point to test
2085 * @param y the Y coordinate of the point to test
2086 * @return true if the point is within this component
2087 * @see #getComponentAt(int, int)
2089 public boolean contains(int x, int y)
2091 return inside (x, y);
2095 * Tests whether or not the specified point is contained within this
2096 * component. Coordinates are relative to this component.
2098 * @param x the X coordinate of the point to test
2099 * @param y the Y coordinate of the point to test
2100 * @return true if the point is within this component
2101 * @deprecated use {@link #contains(int, int)} instead
2103 public boolean inside(int x, int y)
2105 return x >= 0 && y >= 0 && x < width && y < height;
2109 * Tests whether or not the specified point is contained within this
2110 * component. Coordinates are relative to this component.
2112 * @param p the point to test
2113 * @return true if the point is within this component
2114 * @throws NullPointerException if p is null
2115 * @see #getComponentAt(Point)
2118 public boolean contains(Point p)
2120 return contains (p.x, p.y);
2124 * Returns the component occupying the position (x,y). This will either
2125 * be this component, an immediate child component, or <code>null</code>
2126 * if neither of the first two occupies the specified location.
2128 * @param x the X coordinate to search for components at
2129 * @param y the Y coordinate to search for components at
2130 * @return the component at the specified location, or null
2131 * @see #contains(int, int)
2133 public Component getComponentAt(int x, int y)
2135 return locate (x, y);
2139 * Returns the component occupying the position (x,y). This will either
2140 * be this component, an immediate child component, or <code>null</code>
2141 * if neither of the first two occupies the specified location.
2143 * @param x the X coordinate to search for components at
2144 * @param y the Y coordinate to search for components at
2145 * @return the component at the specified location, or null
2146 * @deprecated use {@link #getComponentAt(int, int)} instead
2148 public Component locate(int x, int y)
2150 return contains (x, y) ? this : null;
2154 * Returns the component occupying the position (x,y). This will either
2155 * be this component, an immediate child component, or <code>null</code>
2156 * if neither of the first two occupies the specified location.
2158 * @param p the point to search for components at
2159 * @return the component at the specified location, or null
2160 * @throws NullPointerException if p is null
2161 * @see #contains(Point)
2164 public Component getComponentAt(Point p)
2166 return getComponentAt (p.x, p.y);
2170 * AWT 1.0 event dispatcher.
2172 * @param e the event to dispatch
2173 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2175 public void deliverEvent(Event e)
2177 // XXX Add backward compatibility handling.
2181 * Forwards AWT events to processEvent() if:<ul>
2182 * <li>Events have been enabled for this type of event via
2183 * <code>enableEvents()</code></li>,
2184 * <li>There is at least one registered listener for this type of event</li>
2187 * @param e the event to dispatch
2189 public final void dispatchEvent(AWTEvent e)
2191 // Some subclasses in the AWT package need to override this behavior,
2192 // hence the use of dispatchEventImpl().
2193 dispatchEventImpl(e);
2194 if (peer != null && ! e.consumed)
2195 peer.handleEvent(e);
2199 * AWT 1.0 event dispatcher.
2201 * @param e the event to dispatch
2202 * @return false: since the method was deprecated, the return has no meaning
2203 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2205 public boolean postEvent(Event e)
2207 // XXX Add backward compatibility handling.
2212 * Adds the specified listener to this component. This is harmless if the
2213 * listener is null, but if the listener has already been registered, it
2214 * will now be registered twice.
2216 * @param listener the new listener to add
2217 * @see ComponentEvent
2218 * @see #removeComponentListener(ComponentListener)
2219 * @see #getComponentListeners()
2222 public synchronized void addComponentListener(ComponentListener l)
2224 componentListener = AWTEventMulticaster.add(componentListener, l);
2225 if (componentListener != null)
2226 enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
2230 * Removes the specified listener from the component. This is harmless if
2231 * the listener was not previously registered.
2233 * @param listener the listener to remove
2234 * @see ComponentEvent
2235 * @see #addComponentListener(ComponentListener)
2236 * @see #getComponentListeners()
2239 public synchronized void removeComponentListener(ComponentListener l)
2241 componentListener = AWTEventMulticaster.remove(componentListener, l);
2245 * Returns an array of all specified listeners registered on this component.
2247 * @return an array of listeners
2248 * @see #addComponentListener(ComponentListener)
2249 * @see #removeComponentListener(ComponentListener)
2252 public synchronized ComponentListener[] getComponentListeners()
2254 return (ComponentListener[])
2255 AWTEventMulticaster.getListeners(componentListener,
2256 ComponentListener.class);
2260 * Adds the specified listener to this component. This is harmless if the
2261 * listener is null, but if the listener has already been registered, it
2262 * will now be registered twice.
2264 * @param listener the new listener to add
2266 * @see #removeFocusListener(FocusListener)
2267 * @see #getFocusListeners()
2270 public synchronized void addFocusListener(FocusListener l)
2272 focusListener = AWTEventMulticaster.add(focusListener, l);
2273 if (focusListener != null)
2274 enableEvents(AWTEvent.FOCUS_EVENT_MASK);
2278 * Removes the specified listener from the component. This is harmless if
2279 * the listener was not previously registered.
2281 * @param listener the listener to remove
2283 * @see #addFocusListener(FocusListener)
2284 * @see #getFocusListeners()
2287 public synchronized void removeFocusListener(FocusListener l)
2289 focusListener = AWTEventMulticaster.remove(focusListener, l);
2293 * Returns an array of all specified listeners registered on this component.
2295 * @return an array of listeners
2296 * @see #addFocusListener(FocusListener)
2297 * @see #removeFocusListener(FocusListener)
2300 public synchronized FocusListener[] getFocusListeners()
2302 return (FocusListener[])
2303 AWTEventMulticaster.getListeners(focusListener, FocusListener.class);
2307 * Adds the specified listener to this component. This is harmless if the
2308 * listener is null, but if the listener has already been registered, it
2309 * will now be registered twice.
2311 * @param listener the new listener to add
2312 * @see HierarchyEvent
2313 * @see #removeHierarchyListener(HierarchyListener)
2314 * @see #getHierarchyListeners()
2317 public synchronized void addHierarchyListener(HierarchyListener l)
2319 hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
2320 if (hierarchyListener != null)
2321 enableEvents(AWTEvent.HIERARCHY_EVENT_MASK);
2325 * Removes the specified listener from the component. This is harmless if
2326 * the listener was not previously registered.
2328 * @param listener the listener to remove
2329 * @see HierarchyEvent
2330 * @see #addHierarchyListener(HierarchyListener)
2331 * @see #getHierarchyListeners()
2334 public synchronized void removeHierarchyListener(HierarchyListener l)
2336 hierarchyListener = AWTEventMulticaster.remove(hierarchyListener, l);
2340 * Returns an array of all specified listeners registered on this component.
2342 * @return an array of listeners
2343 * @see #addHierarchyListener(HierarchyListener)
2344 * @see #removeHierarchyListener(HierarchyListener)
2347 public synchronized HierarchyListener[] getHierarchyListeners()
2349 return (HierarchyListener[])
2350 AWTEventMulticaster.getListeners(hierarchyListener,
2351 HierarchyListener.class);
2355 * Adds the specified listener to this component. This is harmless if the
2356 * listener is null, but if the listener has already been registered, it
2357 * will now be registered twice.
2359 * @param listener the new listener to add
2360 * @see HierarchyEvent
2361 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2362 * @see #getHierarchyBoundsListeners()
2365 public synchronized void
2366 addHierarchyBoundsListener(HierarchyBoundsListener l)
2368 hierarchyBoundsListener =
2369 AWTEventMulticaster.add(hierarchyBoundsListener, l);
2370 if (hierarchyBoundsListener != null)
2371 enableEvents(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
2375 * Removes the specified listener from the component. This is harmless if
2376 * the listener was not previously registered.
2378 * @param listener the listener to remove
2379 * @see HierarchyEvent
2380 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2381 * @see #getHierarchyBoundsListeners()
2384 public synchronized void
2385 removeHierarchyBoundsListener(HierarchyBoundsListener l)
2387 hierarchyBoundsListener =
2388 AWTEventMulticaster.remove(hierarchyBoundsListener, l);
2392 * Returns an array of all specified listeners registered on this component.
2394 * @return an array of listeners
2395 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2396 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2399 public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners()
2401 return (HierarchyBoundsListener[])
2402 AWTEventMulticaster.getListeners(hierarchyBoundsListener,
2403 HierarchyBoundsListener.class);
2407 * Adds the specified listener to this component. This is harmless if the
2408 * listener is null, but if the listener has already been registered, it
2409 * will now be registered twice.
2411 * @param listener the new listener to add
2413 * @see #removeKeyListener(KeyListener)
2414 * @see #getKeyListeners()
2417 public synchronized void addKeyListener(KeyListener l)
2419 keyListener = AWTEventMulticaster.add(keyListener, l);
2420 if (keyListener != null)
2421 enableEvents(AWTEvent.KEY_EVENT_MASK);
2425 * Removes the specified listener from the component. This is harmless if
2426 * the listener was not previously registered.
2428 * @param listener the listener to remove
2430 * @see #addKeyListener(KeyListener)
2431 * @see #getKeyListeners()
2434 public synchronized void removeKeyListener(KeyListener l)
2436 keyListener = AWTEventMulticaster.remove(keyListener, l);
2440 * Returns an array of all specified listeners registered on this component.
2442 * @return an array of listeners
2443 * @see #addKeyListener(KeyListener)
2444 * @see #removeKeyListener(KeyListener)
2447 public synchronized KeyListener[] getKeyListeners()
2449 return (KeyListener[])
2450 AWTEventMulticaster.getListeners(keyListener, KeyListener.class);
2454 * Adds the specified listener to this component. This is harmless if the
2455 * listener is null, but if the listener has already been registered, it
2456 * will now be registered twice.
2458 * @param listener the new listener to add
2460 * @see #removeMouseListener(MouseListener)
2461 * @see #getMouseListeners()
2464 public synchronized void addMouseListener(MouseListener l)
2466 mouseListener = AWTEventMulticaster.add(mouseListener, l);
2467 if (mouseListener != null)
2468 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2472 * Removes the specified listener from the component. This is harmless if
2473 * the listener was not previously registered.
2475 * @param listener the listener to remove
2477 * @see #addMouseListener(MouseListener)
2478 * @see #getMouseListeners()
2481 public synchronized void removeMouseListener(MouseListener l)
2483 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
2487 * Returns an array of all specified listeners registered on this component.
2489 * @return an array of listeners
2490 * @see #addMouseListener(MouseListener)
2491 * @see #removeMouseListener(MouseListener)
2494 public synchronized MouseListener[] getMouseListeners()
2496 return (MouseListener[])
2497 AWTEventMulticaster.getListeners(mouseListener, MouseListener.class);
2501 * Adds the specified listener to this component. This is harmless if the
2502 * listener is null, but if the listener has already been registered, it
2503 * will now be registered twice.
2505 * @param listener the new listener to add
2507 * @see #removeMouseMotionListener(MouseMotionListener)
2508 * @see #getMouseMotionListeners()
2511 public synchronized void addMouseMotionListener(MouseMotionListener l)
2513 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, l);
2514 if (mouseMotionListener != null)
2515 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2519 * Removes the specified listener from the component. This is harmless if
2520 * the listener was not previously registered.
2522 * @param listener the listener to remove
2524 * @see #addMouseMotionListener(MouseMotionListener)
2525 * @see #getMouseMotionListeners()
2528 public synchronized void removeMouseMotionListener(MouseMotionListener l)
2530 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
2534 * Returns an array of all specified listeners registered on this component.
2536 * @return an array of listeners
2537 * @see #addMouseMotionListener(MouseMotionListener)
2538 * @see #removeMouseMotionListener(MouseMotionListener)
2541 public synchronized MouseMotionListener[] getMouseMotionListeners()
2543 return (MouseMotionListener[])
2544 AWTEventMulticaster.getListeners(mouseMotionListener,
2545 MouseMotionListener.class);
2549 * Adds the specified listener to this component. This is harmless if the
2550 * listener is null, but if the listener has already been registered, it
2551 * will now be registered twice.
2553 * @param listener the new listener to add
2555 * @see MouseWheelEvent
2556 * @see #removeMouseWheelListener(MouseWheelListener)
2557 * @see #getMouseWheelListeners()
2560 public synchronized void addMouseWheelListener(MouseWheelListener l)
2562 mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, l);
2563 if (mouseWheelListener != null)
2564 enableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK);
2568 * Removes the specified listener from the component. This is harmless if
2569 * the listener was not previously registered.
2571 * @param listener the listener to remove
2573 * @see MouseWheelEvent
2574 * @see #addMouseWheelListener(MouseWheelListener)
2575 * @see #getMouseWheelListeners()
2578 public synchronized void removeMouseWheelListener(MouseWheelListener l)
2580 mouseWheelListener = AWTEventMulticaster.remove(mouseWheelListener, l);
2584 * Returns an array of all specified listeners registered on this component.
2586 * @return an array of listeners
2587 * @see #addMouseWheelListener(MouseWheelListener)
2588 * @see #removeMouseWheelListener(MouseWheelListener)
2591 public synchronized MouseWheelListener[] getMouseWheelListeners()
2593 return (MouseWheelListener[])
2594 AWTEventMulticaster.getListeners(mouseWheelListener,
2595 MouseWheelListener.class);
2599 * Adds the specified listener to this component. This is harmless if the
2600 * listener is null, but if the listener has already been registered, it
2601 * will now be registered twice.
2603 * @param listener the new listener to add
2604 * @see InputMethodEvent
2605 * @see #removeInputMethodListener(InputMethodListener)
2606 * @see #getInputMethodListeners()
2607 * @see #getInputMethodRequests()
2610 public synchronized void addInputMethodListener(InputMethodListener l)
2612 inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
2613 if (inputMethodListener != null)
2614 enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK);
2618 * Removes the specified listener from the component. This is harmless if
2619 * the listener was not previously registered.
2621 * @param listener the listener to remove
2622 * @see InputMethodEvent
2623 * @see #addInputMethodListener(InputMethodListener)
2624 * @see #getInputMethodRequests()
2627 public synchronized void removeInputMethodListener(InputMethodListener l)
2629 inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
2633 * Returns an array of all specified listeners registered on this component.
2635 * @return an array of listeners
2636 * @see #addInputMethodListener(InputMethodListener)
2637 * @see #removeInputMethodListener(InputMethodListener)
2640 public synchronized InputMethodListener[] getInputMethodListeners()
2642 return (InputMethodListener[])
2643 AWTEventMulticaster.getListeners(inputMethodListener,
2644 InputMethodListener.class);
2648 * Returns all registered EventListers of the given listenerType.
2650 * @param listenerType the class of listeners to filter
2651 * @return an array of registered listeners
2652 * @see #getComponentListeners()
2653 * @see #getFocusListeners()
2654 * @see #getHierarchyListeners()
2655 * @see #getHierarchyBoundsListeners()
2656 * @see #getKeyListeners()
2657 * @see #getMouseListeners()
2658 * @see #getMouseMotionListeners()
2659 * @see #getMouseWheelListeners()
2660 * @see #getInputMethodListeners()
2661 * @see #getPropertyChangeListeners()
2664 public EventListener[] getListeners(Class listenerType)
2666 if (listenerType == ComponentListener.class)
2667 return getComponentListeners();
2668 if (listenerType == FocusListener.class)
2669 return getFocusListeners();
2670 if (listenerType == HierarchyListener.class)
2671 return getHierarchyListeners();
2672 if (listenerType == HierarchyBoundsListener.class)
2673 return getHierarchyBoundsListeners();
2674 if (listenerType == KeyListener.class)
2675 return getKeyListeners();
2676 if (listenerType == MouseListener.class)
2677 return getMouseListeners();
2678 if (listenerType == MouseMotionListener.class)
2679 return getMouseMotionListeners();
2680 if (listenerType == MouseWheelListener.class)
2681 return getMouseWheelListeners();
2682 if (listenerType == InputMethodListener.class)
2683 return getInputMethodListeners();
2684 if (listenerType == PropertyChangeListener.class)
2685 return getPropertyChangeListeners();
2686 return (EventListener[]) Array.newInstance(listenerType, 0);
2690 * Returns the input method request handler, for subclasses which support
2691 * on-the-spot text input. By default, input methods are handled by AWT,
2692 * and this returns null.
2694 * @return the input method handler, null by default
2697 public InputMethodRequests getInputMethodRequests()
2703 * Gets the input context of this component, which is inherited from the
2704 * parent unless this is overridden.
2706 * @return the text input context
2709 public InputContext getInputContext()
2711 return parent == null ? null : parent.getInputContext();
2715 * Enables the specified events. The events to enable are specified
2716 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2718 * <p>Events are enabled by default when a listener is attached to the
2719 * component for that event type. This method can be used by subclasses
2720 * to ensure the delivery of a specified event regardless of whether
2721 * or not a listener is attached.
2723 * @param eventsToEnable the desired events to enable
2724 * @see #processEvent(AWTEvent)
2725 * @see #disableEvents(long)
2729 protected final void enableEvents(long eventsToEnable)
2731 eventMask |= eventsToEnable;
2732 // TODO: Unlike Sun's implementation, I think we should try and
2733 // enable/disable events at the peer (gtk/X) level. This will avoid
2734 // clogging the event pipeline with useless mousemove events that
2735 // we arn't interested in, etc. This will involve extending the peer
2736 // interface, but thats okay because the peer interfaces have been
2737 // deprecated for a long time, and no longer feature in the
2738 // API specification at all.
2739 if (isLightweight() && parent != null)
2740 parent.enableEvents(eventsToEnable);
2741 else if (peer != null)
2742 peer.setEventMask(eventMask);
2746 * Disables the specified events. The events to disable are specified
2747 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2749 * @param eventsToDisable the desired events to disable
2750 * @see #enableEvents(long)
2753 protected final void disableEvents(long eventsToDisable)
2755 eventMask &= ~eventsToDisable;
2756 // forward new event mask to peer?
2760 * This is called by the EventQueue if two events with the same event id
2761 * and owner component are queued. Returns a new combined event, or null if
2762 * no combining is done. The coelesced events are currently mouse moves
2763 * (intermediate ones are discarded) and paint events (a merged paint is
2764 * created in place of the two events).
2766 * @param existingEvent the event on the queue
2767 * @param newEvent the new event that might be entered on the queue
2768 * @return null if both events are kept, or the replacement coelesced event
2770 protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent)
2772 switch (existingEvent.id)
2774 case MouseEvent.MOUSE_MOVED:
2775 case MouseEvent.MOUSE_DRAGGED:
2776 // Just drop the old (intermediate) event and return the new one.
2778 case PaintEvent.PAINT:
2779 case PaintEvent.UPDATE:
2780 return coalescePaintEvents((PaintEvent) existingEvent,
2781 (PaintEvent) newEvent);
2788 * Processes the specified event. In this class, this method simply
2789 * calls one of the more specific event handlers.
2791 * @param event the event to process
2792 * @throws NullPointerException if e is null
2793 * @see #processComponentEvent(ComponentEvent)
2794 * @see #processFocusEvent(FocusEvent)
2795 * @see #processKeyEvent(KeyEvent)
2796 * @see #processMouseEvent(MouseEvent)
2797 * @see #processMouseMotionEvent(MouseEvent)
2798 * @see #processInputMethodEvent(InputMethodEvent)
2799 * @see #processHierarchyEvent(HierarchyEvent)
2800 * @see #processMouseWheelEvent(MouseWheelEvent)
2803 protected void processEvent(AWTEvent e)
2805 /* Note: the order of these if statements are
2806 important. Subclasses must be checked first. Eg. MouseEvent
2807 must be checked before ComponentEvent, since a MouseEvent
2808 object is also an instance of a ComponentEvent. */
2810 if (e instanceof FocusEvent)
2811 processFocusEvent((FocusEvent) e);
2812 else if (e instanceof MouseWheelEvent)
2813 processMouseWheelEvent((MouseWheelEvent) e);
2814 else if (e instanceof MouseEvent)
2816 if (e.id == MouseEvent.MOUSE_MOVED
2817 || e.id == MouseEvent.MOUSE_DRAGGED)
2818 processMouseMotionEvent((MouseEvent) e);
2820 processMouseEvent((MouseEvent) e);
2822 else if (e instanceof KeyEvent)
2823 processKeyEvent((KeyEvent) e);
2824 else if (e instanceof InputMethodEvent)
2825 processInputMethodEvent((InputMethodEvent) e);
2826 else if (e instanceof ComponentEvent)
2827 processComponentEvent((ComponentEvent) e);
2828 else if (e instanceof HierarchyEvent)
2830 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2831 processHierarchyEvent((HierarchyEvent) e);
2833 processHierarchyBoundsEvent((HierarchyEvent) e);
2838 * Called when a component event is dispatched and component events are
2839 * enabled. This method passes the event along to any listeners
2840 * that are attached.
2842 * @param event the <code>ComponentEvent</code> to process
2843 * @throws NullPointerException if e is null
2844 * @see ComponentListener
2845 * @see #addComponentListener(ComponentListener)
2846 * @see #enableEvents(long)
2849 protected void processComponentEvent(ComponentEvent e)
2851 if (componentListener == null)
2855 case ComponentEvent.COMPONENT_HIDDEN:
2856 componentListener.componentHidden(e);
2858 case ComponentEvent.COMPONENT_MOVED:
2859 componentListener.componentMoved(e);
2861 case ComponentEvent.COMPONENT_RESIZED:
2862 componentListener.componentResized(e);
2864 case ComponentEvent.COMPONENT_SHOWN:
2865 componentListener.componentShown(e);
2871 * Called when a focus event is dispatched and component events are
2872 * enabled. This method passes the event along to any listeners
2873 * that are attached.
2875 * @param event the <code>FocusEvent</code> to process
2876 * @throws NullPointerException if e is null
2877 * @see FocusListener
2878 * @see #addFocusListener(FocusListener)
2879 * @see #enableEvents(long)
2882 protected void processFocusEvent(FocusEvent e)
2884 if (focusListener == null)
2888 case FocusEvent.FOCUS_GAINED:
2889 focusListener.focusGained(e);
2891 case FocusEvent.FOCUS_LOST:
2892 focusListener.focusLost(e);
2898 * Called when a key event is dispatched and component events are
2899 * enabled. This method passes the event along to any listeners
2900 * that are attached.
2902 * @param event the <code>KeyEvent</code> to process
2903 * @throws NullPointerException if e is null
2905 * @see #addKeyListener(KeyListener)
2906 * @see #enableEvents(long)
2909 protected void processKeyEvent(KeyEvent e)
2911 if (keyListener == null)
2915 case KeyEvent.KEY_PRESSED:
2916 keyListener.keyPressed(e);
2918 case KeyEvent.KEY_RELEASED:
2919 keyListener.keyReleased(e);
2921 case KeyEvent.KEY_TYPED:
2922 keyListener.keyTyped(e);
2928 * Called when a regular mouse event is dispatched and component events are
2929 * enabled. This method passes the event along to any listeners
2930 * that are attached.
2932 * @param event the <code>MouseEvent</code> to process
2933 * @throws NullPointerException if e is null
2934 * @see MouseListener
2935 * @see #addMouseListener(MouseListener)
2936 * @see #enableEvents(long)
2939 protected void processMouseEvent(MouseEvent e)
2941 if (mouseListener == null)
2945 case MouseEvent.MOUSE_CLICKED:
2946 mouseListener.mouseClicked(e);
2948 case MouseEvent.MOUSE_ENTERED:
2949 mouseListener.mouseEntered(e);
2951 case MouseEvent.MOUSE_EXITED:
2952 mouseListener.mouseExited(e);
2954 case MouseEvent.MOUSE_PRESSED:
2955 mouseListener.mousePressed(e);
2957 case MouseEvent.MOUSE_RELEASED:
2958 mouseListener.mouseReleased(e);
2964 * Called when a mouse motion event is dispatched and component events are
2965 * enabled. This method passes the event along to any listeners
2966 * that are attached.
2968 * @param event the <code>MouseMotionEvent</code> to process
2969 * @throws NullPointerException if e is null
2970 * @see MouseMotionListener
2971 * @see #addMouseMotionListener(MouseMotionListener)
2972 * @see #enableEvents(long)
2975 protected void processMouseMotionEvent(MouseEvent e)
2977 if (mouseMotionListener == null)
2981 case MouseEvent.MOUSE_DRAGGED:
2982 mouseMotionListener.mouseDragged(e);
2984 case MouseEvent.MOUSE_MOVED:
2985 mouseMotionListener.mouseMoved(e);
2991 * Called when a mouse wheel event is dispatched and component events are
2992 * enabled. This method passes the event along to any listeners that are
2995 * @param event the <code>MouseWheelEvent</code> to process
2996 * @throws NullPointerException if e is null
2997 * @see MouseWheelListener
2998 * @see #addMouseWheelListener(MouseWheelListener)
2999 * @see #enableEvents(long)
3002 protected void processMouseWheelEvent(MouseWheelEvent e)
3004 if (mouseWheelListener != null
3005 && e.id == MouseEvent.MOUSE_WHEEL)
3006 mouseWheelListener.mouseWheelMoved(e);
3010 * Called when an input method event is dispatched and component events are
3011 * enabled. This method passes the event along to any listeners that are
3014 * @param event the <code>InputMethodEvent</code> to process
3015 * @throws NullPointerException if e is null
3016 * @see InputMethodListener
3017 * @see #addInputMethodListener(InputMethodListener)
3018 * @see #enableEvents(long)
3021 protected void processInputMethodEvent(InputMethodEvent e)
3023 if (inputMethodListener == null)
3027 case InputMethodEvent.CARET_POSITION_CHANGED:
3028 inputMethodListener.caretPositionChanged(e);
3030 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
3031 inputMethodListener.inputMethodTextChanged(e);
3037 * Called when a hierarchy change event is dispatched and component events
3038 * are enabled. This method passes the event along to any listeners that are
3041 * @param event the <code>HierarchyEvent</code> to process
3042 * @throws NullPointerException if e is null
3043 * @see HierarchyListener
3044 * @see #addHierarchyListener(HierarchyListener)
3045 * @see #enableEvents(long)
3048 protected void processHierarchyEvent(HierarchyEvent e)
3050 if (hierarchyListener == null)
3052 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
3053 hierarchyListener.hierarchyChanged(e);
3057 * Called when a hierarchy bounds event is dispatched and component events
3058 * are enabled. This method passes the event along to any listeners that are
3061 * @param event the <code>HierarchyEvent</code> to process
3062 * @throws NullPointerException if e is null
3063 * @see HierarchyBoundsListener
3064 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
3065 * @see #enableEvents(long)
3068 protected void processHierarchyBoundsEvent(HierarchyEvent e)
3070 if (hierarchyBoundsListener == null)
3074 case HierarchyEvent.ANCESTOR_MOVED:
3075 hierarchyBoundsListener.ancestorMoved(e);
3077 case HierarchyEvent.ANCESTOR_RESIZED:
3078 hierarchyBoundsListener.ancestorResized(e);
3084 * AWT 1.0 event processor.
3086 * @param evt the event to handle
3087 * @return false: since the method was deprecated, the return has no meaning
3088 * @deprecated use {@link #processEvent(AWTEvent)} instead
3090 public boolean handleEvent(Event evt)
3092 // XXX Add backward compatibility handling.
3097 * AWT 1.0 mouse event.
3099 * @param evt the event to handle
3100 * @param x the x coordinate, ignored
3101 * @param y the y coordinate, ignored
3102 * @return false: since the method was deprecated, the return has no meaning
3103 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3105 public boolean mouseDown(Event evt, int x, int y)
3107 // XXX Add backward compatibility handling.
3112 * AWT 1.0 mouse event.
3114 * @param evt the event to handle
3115 * @param x the x coordinate, ignored
3116 * @param y the y coordinate, ignored
3117 * @return false: since the method was deprecated, the return has no meaning
3118 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3120 public boolean mouseDrag(Event evt, int x, int y)
3122 // XXX Add backward compatibility handling.
3127 * AWT 1.0 mouse event.
3129 * @param evt the event to handle
3130 * @param x the x coordinate, ignored
3131 * @param y the y coordinate, ignored
3132 * @return false: since the method was deprecated, the return has no meaning
3133 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3135 public boolean mouseUp(Event evt, int x, int y)
3137 // XXX Add backward compatibility handling.
3142 * AWT 1.0 mouse event.
3144 * @param evt the event to handle
3145 * @param x the x coordinate, ignored
3146 * @param y the y coordinate, ignored
3147 * @return false: since the method was deprecated, the return has no meaning
3148 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3150 public boolean mouseMove(Event evt, int x, int y)
3152 // XXX Add backward compatibility handling.
3157 * AWT 1.0 mouse event.
3159 * @param evt the event to handle
3160 * @param x the x coordinate, ignored
3161 * @param y the y coordinate, ignored
3162 * @return false: since the method was deprecated, the return has no meaning
3163 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3165 public boolean mouseEnter(Event evt, int x, int y)
3167 // XXX Add backward compatibility handling.
3172 * AWT 1.0 mouse event.
3174 * @param evt the event to handle
3175 * @param x the x coordinate, ignored
3176 * @param y the y coordinate, ignored
3177 * @return false: since the method was deprecated, the return has no meaning
3178 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3180 public boolean mouseExit(Event evt, int x, int y)
3182 // XXX Add backward compatibility handling.
3187 * AWT 1.0 key press event.
3189 * @param evt the event to handle
3190 * @param key the key pressed, ignored
3191 * @return false: since the method was deprecated, the return has no meaning
3192 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3194 public boolean keyDown(Event evt, int key)
3196 // XXX Add backward compatibility handling.
3201 * AWT 1.0 key press event.
3203 * @param evt the event to handle
3204 * @param key the key pressed, ignored
3205 * @return false: since the method was deprecated, the return has no meaning
3206 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3208 public boolean keyUp(Event evt, int key)
3210 // XXX Add backward compatibility handling.
3215 * AWT 1.0 action event processor.
3217 * @param evt the event to handle
3218 * @param what the object acted on, ignored
3219 * @return false: since the method was deprecated, the return has no meaning
3220 * @deprecated in classes which support actions, use
3221 * <code>processActionEvent(ActionEvent)</code> instead
3223 public boolean action(Event evt, Object what)
3225 // XXX Add backward compatibility handling.
3230 * Called to inform this component it has been added to a container.
3231 * A native peer - if any - is created at this time. This method is
3232 * called automatically by the AWT system and should not be called by
3235 * @see #isDisplayable()
3236 * @see #removeNotify()
3238 public void addNotify()
3241 peer = getToolkit().createComponent(this);
3242 /* Now that all the children has gotten their peers, we should
3243 have the event mask needed for this component and its
3244 lightweight subcomponents. */
3245 peer.setEventMask(eventMask);
3246 /* We do not invalidate here, but rather leave that job up to
3247 the peer. For efficiency, the peer can choose not to
3248 invalidate if it is happy with the current dimensions,
3253 * Called to inform this component is has been removed from its
3254 * container. Its native peer - if any - is destroyed at this time.
3255 * This method is called automatically by the AWT system and should
3256 * not be called by user level code.
3258 * @see #isDisplayable()
3261 public void removeNotify()
3269 * AWT 1.0 focus event.
3271 * @param evt the event to handle
3272 * @param what the Object focused, ignored
3273 * @return false: since the method was deprecated, the return has no meaning
3274 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3276 public boolean gotFocus(Event evt, Object what)
3278 // XXX Add backward compatibility handling.
3283 * AWT 1.0 focus event.
3285 * @param evt the event to handle
3286 * @param what the Object focused, ignored
3287 * @return false: since the method was deprecated, the return has no meaning
3288 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3290 public boolean lostFocus(Event evt, Object what)
3292 // XXX Add backward compatibility handling.
3297 * Tests whether or not this component is in the group that can be
3298 * traversed using the keyboard traversal mechanism (such as the TAB key).
3300 * @return true if the component is traversed via the TAB key
3301 * @see #setFocusable(boolean)
3303 * @deprecated use {@link #isFocusable()} instead
3305 public boolean isFocusTraversable()
3307 return enabled && visible && (peer == null || peer.isFocusTraversable());
3311 * Tests if this component can receive focus.
3313 * @return true if this component can receive focus
3316 public boolean isFocusable()
3322 * Specify whether this component can receive focus.
3324 * @param focusable the new focusable status
3327 public void setFocusable(boolean focusable)
3329 firePropertyChange("focusable", this.focusable, focusable);
3330 this.focusable = focusable;
3334 * Sets the focus traversal keys for a given type of focus events. Normally,
3335 * the default values should match the operating system's native choices. To
3336 * disable a given traversal, use <code>Collections.EMPTY_SET</code>. The
3337 * event dispatcher will consume PRESSED, RELEASED, and TYPED events for the
3338 * specified key, although focus can only transfer on PRESSED or RELEASED.
3340 * <p>The defauts are:
3342 * <th><td>Identifier</td><td>Meaning</td><td>Default</td></th>
3343 * <tr><td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
3344 * <td>Normal forward traversal</td>
3345 * <td>TAB on KEY_PRESSED, Ctrl-TAB on KEY_PRESSED</td></tr>
3346 * <tr><td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
3347 * <td>Normal backward traversal</td>
3348 * <td>Shift-TAB on KEY_PRESSED, Ctrl-Shift-TAB on KEY_PRESSED</td></tr>
3349 * <tr><td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
3350 * <td>Go up a traversal cycle</td><td>None</td></tr>
3353 * <p>Specifying null allows inheritance from the parent, or from the current
3354 * KeyboardFocusManager default set. If not null, the set must contain only
3355 * AWTKeyStrokes that are not already focus keys and are not KEY_TYPED
3358 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3359 * UP_CYCLE_TRAVERSAL_KEYS
3360 * @param keystrokes a set of keys, or null
3361 * @throws IllegalArgumentException if id or keystrokes is invalid
3362 * @see #getFocusTraversalKeys(int)
3363 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3364 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3365 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3368 public void setFocusTraversalKeys(int id, Set keystrokes)
3370 if (keystrokes == null)
3371 throw new IllegalArgumentException();
3377 case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
3378 sa = getFocusTraversalKeys
3379 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3380 sb = getFocusTraversalKeys
3381 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3382 name = "forwardFocusTraversalKeys";
3384 case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
3385 sa = getFocusTraversalKeys
3386 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3387 sb = getFocusTraversalKeys
3388 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3389 name = "backwardFocusTraversalKeys";
3391 case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
3392 sa = getFocusTraversalKeys
3393 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3394 sb = getFocusTraversalKeys
3395 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3396 name = "upCycleFocusTraversalKeys";
3399 throw new IllegalArgumentException();
3401 int i = keystrokes.size();
3402 Iterator iter = keystrokes.iterator();
3405 Object o = iter.next();
3406 if (! (o instanceof AWTKeyStroke)
3407 || sa.contains(o) || sb.contains(o)
3408 || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
3409 throw new IllegalArgumentException();
3411 if (focusTraversalKeys == null)
3412 focusTraversalKeys = new Set[3];
3413 keystrokes = Collections.unmodifiableSet(new HashSet(keystrokes));
3414 firePropertyChange(name, focusTraversalKeys[id], keystrokes);
3415 focusTraversalKeys[id] = keystrokes;
3419 * Returns the set of keys for a given focus traversal action, as defined
3420 * in <code>setFocusTraversalKeys</code>. If not set, this is inherited from
3421 * the parent component, which may have gotten it from the
3422 * KeyboardFocusManager.
3424 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3425 * UP_CYCLE_TRAVERSAL_KEYS
3426 * @throws IllegalArgumentException if id is invalid
3427 * @see #setFocusTraversalKeys(int, Set)
3428 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3429 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3430 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3433 public Set getFocusTraversalKeys(int id)
3435 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3436 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3437 throw new IllegalArgumentException();
3439 if (focusTraversalKeys != null)
3440 s = focusTraversalKeys[id];
3441 if (s == null && parent != null)
3442 s = parent.getFocusTraversalKeys(id);
3443 return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
3444 .getDefaultFocusTraversalKeys(id)) : s;
3448 * Tests whether the focus traversal keys for a given action are explicitly
3451 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3452 * UP_CYCLE_TRAVERSAL_KEYS
3453 * @return true if that set is explicitly specified
3454 * @throws IllegalArgumentException if id is invalid
3455 * @see #getFocusTraversalKeys(int)
3456 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3457 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3458 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3461 public boolean areFocusTraversalKeysSet(int id)
3463 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3464 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3465 throw new IllegalArgumentException();
3466 return focusTraversalKeys != null && focusTraversalKeys[id] != null;
3470 * Sets whether focus traversal keys are enabled, which consumes traversal
3471 * keys and performs the focus event automatically.
3473 * @param focusTraversalKeysEnabled the new value of the flag
3474 * @see #getFocusTraversalKeysEnabled()
3475 * @see #setFocusTraversalKeys(int, Set)
3476 * @see #getFocusTraversalKeys(int)
3479 public void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)
3481 firePropertyChange("focusTraversalKeysEnabled",
3482 this.focusTraversalKeysEnabled,
3483 focusTraversalKeysEnabled);
3484 this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
3488 * Tests whether focus traversal keys are enabled. If they are, then focus
3489 * traversal keys are consumed and focus events performed automatically,
3490 * without the component seeing the keystrokes.
3492 * @return true if focus traversal is enabled
3493 * @see #setFocusTraversalKeysEnabled(boolean)
3494 * @see #setFocusTraversalKeys(int, Set)
3495 * @see #getFocusTraversalKeys(int)
3498 public boolean getFocusTraversalKeysEnabled()
3500 return focusTraversalKeysEnabled;
3504 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3505 * event will be fired if and only if this request is successful. To be
3506 * successful, the component must be displayable, visible, and focusable,
3507 * and the top-level Window must be able to receive focus. Thus, this
3508 * request may fail, or be delayed until the window receives focus. It is
3509 * recommended that <code>requestFocusInWindow</code> be used where
3510 * possible to be more platform-independent.
3512 * @see #requestFocusInWindow()
3514 * @see #addFocusListener(FocusListener)
3515 * @see #isFocusable()
3516 * @see #isDisplayable()
3517 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3519 public void requestFocus()
3521 // If there's no peer then this component can't get the focus. We
3522 // treat it as a silent rejection of the request.
3524 peer.requestFocus();
3528 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3529 * event will be fired if and only if this request is successful. To be
3530 * successful, the component must be displayable, visible, and focusable,
3531 * and the top-level Window must be able to receive focus. Thus, this
3532 * request may fail, or be delayed until the window receives focus. It is
3533 * recommended that <code>requestFocusInWindow</code> be used where
3534 * possible to be more platform-independent.
3536 * <p>If the return value is false, the request is guaranteed to fail. If
3537 * it is true, it will likely succeed unless the action is vetoed or
3538 * something in the native windowing system intervenes. The temporary flag,
3539 * and thus this method in general, is not designed for public use; rather
3540 * it is a hook for lightweight components to notify their container in
3541 * an attempt to reduce the amount of repainting necessary.
3543 * @param temporary true if the focus request is temporary
3544 * @return true if the request has a chance of success
3545 * @see #requestFocusInWindow()
3547 * @see #addFocusListener(FocusListener)
3548 * @see #isFocusable()
3549 * @see #isDisplayable()
3550 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3553 protected boolean requestFocus(boolean temporary)
3555 // XXX Implement correctly.
3561 * Requests that this component be given focus, if it resides in the
3562 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3563 * event will be fired if and only if this request is successful. To be
3564 * successful, the component must be displayable, visible, and focusable,
3565 * and the top-level Window must be focused.
3567 * <p>If the return value is false, the request is guaranteed to fail. If
3568 * it is true, it will likely succeed unless the action is vetoed or
3569 * something in the native windowing system intervenes. The temporary flag,
3570 * and thus this method in general, is not designed for public use; rather
3571 * it is a hook for lightweight components to notify their container in
3572 * an attempt to reduce the amount of repainting necessary.
3574 * @return true if the request has a chance of success
3575 * @see #requestFocus()
3577 * @see #addFocusListener(FocusListener)
3578 * @see #isFocusable()
3579 * @see #isDisplayable()
3580 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3583 public boolean requestFocusInWindow()
3585 // XXX Implement correctly.
3591 * Requests that this component be given focus, if it resides in the
3592 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3593 * event will be fired if and only if this request is successful. To be
3594 * successful, the component must be displayable, visible, and focusable,
3595 * and the top-level Window must be focused.
3597 * <p>If the return value is false, the request is guaranteed to fail. If
3598 * it is true, it will likely succeed unless the action is vetoed or
3599 * something in the native windowing system intervenes. The temporary flag,
3600 * and thus this method in general, is not designed for public use; rather
3601 * it is a hook for lightweight components to notify their container in
3602 * an attempt to reduce the amount of repainting necessary.
3604 * @param temporary true if the focus request is temporary
3605 * @return true if the request has a chance of success
3606 * @see #requestFocus()
3608 * @see #addFocusListener(FocusListener)
3609 * @see #isFocusable()
3610 * @see #isDisplayable()
3611 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3614 protected boolean requestFocusInWindow(boolean temporary)
3616 // XXX Implement correctly.
3622 * Transfers focus to the next component in the focus traversal order, as
3623 * though this were the current focus owner.
3625 * @see #requestFocus()
3628 public void transferFocus()
3632 next = findNextFocusComponent(null);
3634 next = parent.findNextFocusComponent(this);
3635 if (next != null && next != this)
3636 next.requestFocus();
3640 * Returns the root container that owns the focus cycle where this component
3641 * resides. A focus cycle root is in two cycles, one as the ancestor, and
3642 * one as the focusable element; this call always returns the ancestor.
3644 * @return the ancestor container that owns the focus cycle
3647 public Container getFocusCycleRootAncestor()
3650 throw new Error("not implemented");
3654 * Tests if the container is the ancestor of the focus cycle that this
3655 * component belongs to.
3657 * @param c the container to test
3658 * @return true if c is the focus cycle root
3661 public boolean isFocusCycleRoot(Container c)
3663 return c == getFocusCycleRootAncestor();
3667 * AWT 1.0 focus event processor.
3669 * @deprecated use {@link #transferFocus()} instead
3671 public void nextFocus()
3677 * Transfers focus to the previous component in the focus traversal order, as
3678 * though this were the current focus owner.
3680 * @see #requestFocus()
3683 public void transferFocusBackward()
3686 throw new Error("not implemented");
3690 * Transfers focus to the focus cycle root of this component. However, if
3691 * this is a Window, the default focus owner in the window in the current
3692 * focus cycle is focused instead.
3694 * @see #requestFocus()
3695 * @see #isFocusCycleRoot()
3698 public void transferFocusUpCycle()
3701 throw new Error("not implemented");
3705 * Tests if this component is the focus owner. Use {@link #isFocusOwner()}
3708 * @return true if this component owns focus
3711 public boolean hasFocus()
3713 return isFocusOwner();
3717 * Tests if this component is the focus owner.
3719 * @return true if this component owns focus
3722 public boolean isFocusOwner()
3725 throw new Error("not implemented");
3729 * Adds the specified popup menu to this component.
3731 * @param menu the popup menu to be added
3732 * @see #remove(MenuComponent)
3735 public synchronized void add(PopupMenu popup)
3738 popups = new Vector();
3743 * Removes the specified popup menu from this component.
3745 * @param menu the popup menu to remove
3746 * @see #add(PopupMenu)
3749 public synchronized void remove(MenuComponent popup)
3752 popups.remove(popup);
3756 * Returns a debugging string representing this component. The string may
3757 * be empty but not null.
3759 * @return a string representing this component
3761 protected String paramString()
3763 StringBuffer param = new StringBuffer();
3764 String name = getName();
3766 param.append(name).append(",");
3767 param.append(width).append("x").append(height).append("+").append(x)
3768 .append("+").append(y);
3770 param.append(",invalid");
3772 param.append(",invisible");
3774 param.append(",disabled");
3776 param.append(",translucent");
3777 if (isDoubleBuffered())
3778 param.append(",doublebuffered");
3779 return param.toString();
3783 * Returns a string representation of this component. This is implemented
3784 * as <code>getClass().getName() + '[' + paramString() + ']'</code>.
3786 * @return a string representation of this component
3788 public String toString()
3790 return getClass().getName() + '[' + paramString() + ']';
3794 * Prints a listing of this component to <code>System.out</code>.
3796 * @see #list(PrintStream)
3800 list(System.out, 0);
3804 * Prints a listing of this component to the specified print stream.
3806 * @param stream the <code>PrintStream</code> to print to
3808 public void list(PrintStream out)
3814 * Prints a listing of this component to the specified print stream,
3815 * starting at the specified indentation point.
3817 * @param stream the <code>PrintStream</code> to print to
3818 * @param indent the indentation point
3820 public void list(PrintStream out, int indent)
3822 for (int i = 0; i < indent; ++i)
3824 out.println(toString());
3828 * Prints a listing of this component to the specified print writer.
3830 * @param writer the <code>PrintWrinter</code> to print to
3833 public void list(PrintWriter out)
3839 * Prints a listing of this component to the specified print writer,
3840 * starting at the specified indentation point.
3842 * @param writer the <code>PrintWriter</code> to print to
3843 * @param indent the indentation point
3846 public void list(PrintWriter out, int indent)
3848 for (int i = 0; i < indent; ++i)
3850 out.println(toString());
3854 * Adds the specified property listener to this component. This is harmless
3855 * if the listener is null, but if the listener has already been registered,
3856 * it will now be registered twice. The property listener ignores inherited
3857 * properties. Recognized properties include:<br>
3859 * <li>the font (<code>"font"</code>)</li>
3860 * <li>the background color (<code>"background"</code>)</li>
3861 * <li>the foreground color (<code>"foreground"</code>)</li>
3862 * <li>the focusability (<code>"focusable"</code>)</li>
3863 * <li>the focus key traversal enabled state
3864 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3865 * <li>the set of forward traversal keys
3866 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3867 * <li>the set of backward traversal keys
3868 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3869 * <li>the set of up-cycle traversal keys
3870 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3873 * @param listener the new listener to add
3874 * @see #removePropertyChangeListener(PropertyChangeListener)
3875 * @see #getPropertyChangeListeners()
3876 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3879 public void addPropertyChangeListener(PropertyChangeListener listener)
3881 if (changeSupport == null)
3882 changeSupport = new PropertyChangeSupport(this);
3883 changeSupport.addPropertyChangeListener(listener);
3887 * Removes the specified property listener from the component. This is
3888 * harmless if the listener was not previously registered.
3890 * @param listener the listener to remove
3891 * @see #addPropertyChangeListener(PropertyChangeListener)
3892 * @see #getPropertyChangeListeners()
3893 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3896 public void removePropertyChangeListener(PropertyChangeListener listener)
3898 if (changeSupport != null)
3899 changeSupport.removePropertyChangeListener(listener);
3903 * Returns an array of all specified listeners registered on this component.
3905 * @return an array of listeners
3906 * @see #addPropertyChangeListener(PropertyChangeListener)
3907 * @see #removePropertyChangeListener(PropertyChangeListener)
3908 * @see #getPropertyChangeListeners(String)
3911 public PropertyChangeListener[] getPropertyChangeListeners()
3913 return changeSupport == null ? new PropertyChangeListener[0]
3914 : changeSupport.getPropertyChangeListeners();
3918 * Adds the specified property listener to this component. This is harmless
3919 * if the listener is null, but if the listener has already been registered,
3920 * it will now be registered twice. The property listener ignores inherited
3921 * properties. The listener is keyed to a single property. Recognized
3922 * properties include:<br>
3924 * <li>the font (<code>"font"</code>)</li>
3925 * <li>the background color (<code>"background"</code>)</li>
3926 * <li>the foreground color (<code>"foreground"</code>)</li>
3927 * <li>the focusability (<code>"focusable"</code>)</li>
3928 * <li>the focus key traversal enabled state
3929 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3930 * <li>the set of forward traversal keys
3931 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3932 p * <li>the set of backward traversal keys
3933 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3934 * <li>the set of up-cycle traversal keys
3935 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3938 * @param propertyName the property name to filter on
3939 * @param listener the new listener to add
3940 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3941 * @see #getPropertyChangeListeners(String)
3942 * @see #addPropertyChangeListener(PropertyChangeListener)
3945 public void addPropertyChangeListener(String propertyName,
3946 PropertyChangeListener listener)
3948 if (changeSupport == null)
3949 changeSupport = new PropertyChangeSupport(this);
3950 changeSupport.addPropertyChangeListener(propertyName, listener);
3954 * Removes the specified property listener on a particular property from
3955 * the component. This is harmless if the listener was not previously
3958 * @param propertyName the property name to filter on
3959 * @param listener the listener to remove
3960 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3961 * @see #getPropertyChangeListeners(String)
3962 * @see #removePropertyChangeListener(PropertyChangeListener)
3965 public void removePropertyChangeListener(String propertyName,
3966 PropertyChangeListener listener)
3968 if (changeSupport != null)
3969 changeSupport.removePropertyChangeListener(propertyName, listener);
3973 * Returns an array of all specified listeners on the named property that
3974 * are registered on this component.
3976 * @return an array of listeners
3977 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3978 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3979 * @see #getPropertyChangeListeners()
3982 public PropertyChangeListener[] getPropertyChangeListeners(String property)
3984 return changeSupport == null ? new PropertyChangeListener[0]
3985 : changeSupport.getPropertyChangeListeners(property);
3989 * Report a change in a bound property to any registered property listeners.
3991 * @param propertyName the property that changed
3992 * @param oldValue the old property value
3993 * @param newValue the new property value
3995 protected void firePropertyChange(String propertyName, Object oldValue,
3998 if (changeSupport != null)
3999 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
4003 * Report a change in a bound property to any registered property listeners.
4005 * @param propertyName the property that changed
4006 * @param oldValue the old property value
4007 * @param newValue the new property value
4009 protected void firePropertyChange(String propertyName, boolean oldValue,
4012 if (changeSupport != null)
4013 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
4017 * Report a change in a bound property to any registered property listeners.
4019 * @param propertyName the property that changed
4020 * @param oldValue the old property value
4021 * @param newValue the new property value
4023 protected void firePropertyChange(String propertyName, int oldValue,
4026 if (changeSupport != null)
4027 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
4031 * Sets the text layout orientation of this component. New components default
4032 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects only
4033 * the current component, while
4034 * {@link #applyComponentOrientation(ComponentOrientation)} affects the
4037 * @param o the new orientation
4038 * @throws NullPointerException if o is null
4039 * @see #getComponentOrientation()
4041 public void setComponentOrientation(ComponentOrientation o)
4044 throw new NullPointerException();
4049 * Determines the text layout orientation used by this component.
4051 * @return the component orientation
4052 * @see #setComponentOrientation(ComponentOrientation)
4054 public ComponentOrientation getComponentOrientation()
4060 * Sets the text layout orientation of this component. New components default
4061 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects the
4062 * entire hierarchy, while
4063 * {@link #setComponentOrientation(ComponentOrientation)} affects only the
4064 * current component.
4066 * @param o the new orientation
4067 * @throws NullPointerException if o is null
4068 * @see #getComponentOrientation()
4071 public void applyComponentOrientation(ComponentOrientation o)
4073 setComponentOrientation(o);
4077 * Returns the accessibility framework context of this class. Component is
4078 * not accessible, so the default implementation returns null. Subclasses
4079 * must override this behavior, and return an appropriate subclass of
4080 * {@link AccessibleAWTComponent}.
4082 * @return the accessibility context
4084 public AccessibleContext getAccessibleContext()
4090 // Helper methods; some are package visible for use by subclasses.
4093 * Subclasses should override this to return unique component names like
4096 * @return the generated name for this component
4098 String generateName()
4100 // Component is abstract.
4105 * Sets the peer for this component.
4107 * @param peer the new peer
4109 final void setPeer(ComponentPeer peer)
4115 * Implementation method that allows classes such as Canvas and Window to
4116 * override the graphics configuration without violating the published API.
4118 * @return the graphics configuration
4120 GraphicsConfiguration getGraphicsConfigurationImpl()
4124 GraphicsConfiguration config = peer.getGraphicsConfiguration();
4130 return parent.getGraphicsConfiguration();
4136 * Implementation of dispatchEvent. Allows trusted package classes to
4137 * dispatch additional events first.
4139 * @param e the event to dispatch
4141 void dispatchEventImpl(AWTEvent e)
4143 if (eventTypeEnabled (e.id))
4148 * Tells whether or not an event type is enabled.
4150 boolean eventTypeEnabled (int type)
4152 if (type > AWTEvent.RESERVED_ID_MAX)
4157 case ComponentEvent.COMPONENT_HIDDEN:
4158 case ComponentEvent.COMPONENT_MOVED:
4159 case ComponentEvent.COMPONENT_RESIZED:
4160 case ComponentEvent.COMPONENT_SHOWN:
4161 return (componentListener != null
4162 || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0);
4164 case KeyEvent.KEY_PRESSED:
4165 case KeyEvent.KEY_RELEASED:
4166 case KeyEvent.KEY_TYPED:
4167 return (keyListener != null
4168 || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0);
4170 case MouseEvent.MOUSE_CLICKED:
4171 case MouseEvent.MOUSE_ENTERED:
4172 case MouseEvent.MOUSE_EXITED:
4173 case MouseEvent.MOUSE_PRESSED:
4174 case MouseEvent.MOUSE_RELEASED:
4175 return (mouseListener != null
4176 || mouseMotionListener != null
4177 || (eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0);
4179 case FocusEvent.FOCUS_GAINED:
4180 case FocusEvent.FOCUS_LOST:
4181 return (focusListener != null
4182 || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0);
4184 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
4185 case InputMethodEvent.CARET_POSITION_CHANGED:
4186 return (inputMethodListener != null
4187 || (eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0);
4189 case PaintEvent.PAINT:
4190 case PaintEvent.UPDATE:
4191 return (eventMask & AWTEvent.PAINT_EVENT_MASK) != 0;
4199 * Coalesce paint events. Current heuristic is: Merge if the union of
4200 * areas is less than twice that of the sum of the areas. The X server
4201 * tend to create a lot of paint events that are adjacent but not
4206 * | +-----+ ...will be merged
4212 * +---------------+--+
4213 * | | | ...will not be merged
4214 * +---------------+ |
4223 * @param queuedEvent the first paint event
4224 * @param newEvent the second paint event
4225 * @return the combined paint event, or null
4227 private PaintEvent coalescePaintEvents(PaintEvent queuedEvent,
4228 PaintEvent newEvent)
4230 Rectangle r1 = queuedEvent.getUpdateRect();
4231 Rectangle r2 = newEvent.getUpdateRect();
4232 Rectangle union = r1.union(r2);
4234 int r1a = r1.width * r1.height;
4235 int r2a = r2.width * r2.height;
4236 int ua = union.width * union.height;
4238 if (ua > (r1a+r2a)*2)
4240 /* The 2 factor should maybe be reconsidered. Perhaps 3/2
4243 newEvent.setUpdateRect(union);
4248 * This method is used to implement transferFocus(). CHILD is the child
4249 * making the request. This is overridden by Container; when called for an
4250 * ordinary component there is no child and so we always return null.
4252 * @param child the component making the request
4253 * @return the next component to focus on
4255 Component findNextFocusComponent(Component child)
4261 * Deserializes this component. This regenerates all serializable listeners
4262 * which were registered originally.
4264 * @param s the stream to read from
4265 * @throws ClassNotFoundException if deserialization fails
4266 * @throws IOException if the stream fails
4268 private void readObject(ObjectInputStream s)
4269 throws ClassNotFoundException, IOException
4271 s.defaultReadObject();
4272 String key = (String) s.readObject();
4275 Object listener = s.readObject();
4276 if ("componentL".equals(key))
4277 addComponentListener((ComponentListener) listener);
4278 else if ("focusL".equals(key))
4279 addFocusListener((FocusListener) listener);
4280 else if ("keyL".equals(key))
4281 addKeyListener((KeyListener) listener);
4282 else if ("mouseL".equals(key))
4283 addMouseListener((MouseListener) listener);
4284 else if ("mouseMotionL".equals(key))
4285 addMouseMotionListener((MouseMotionListener) listener);
4286 else if ("inputMethodL".equals(key))
4287 addInputMethodListener((InputMethodListener) listener);
4288 else if ("hierarchyL".equals(key))
4289 addHierarchyListener((HierarchyListener) listener);
4290 else if ("hierarchyBoundsL".equals(key))
4291 addHierarchyBoundsListener((HierarchyBoundsListener) listener);
4292 else if ("mouseWheelL".equals(key))
4293 addMouseWheelListener((MouseWheelListener) listener);
4294 key = (String) s.readObject();
4299 * Serializes this component. This ignores all listeners which do not
4300 * implement Serializable, but includes those that do.
4302 * @param s the stream to write to
4303 * @throws IOException if the stream fails
4305 private void writeObject(ObjectOutputStream s) throws IOException
4307 s.defaultWriteObject();
4308 AWTEventMulticaster.save(s, "componentL", componentListener);
4309 AWTEventMulticaster.save(s, "focusL", focusListener);
4310 AWTEventMulticaster.save(s, "keyL", keyListener);
4311 AWTEventMulticaster.save(s, "mouseL", mouseListener);
4312 AWTEventMulticaster.save(s, "mouseMotionL", mouseMotionListener);
4313 AWTEventMulticaster.save(s, "inputMethodL", inputMethodListener);
4314 AWTEventMulticaster.save(s, "hierarchyL", hierarchyListener);
4315 AWTEventMulticaster.save(s, "hierarchyBoundsL", hierarchyBoundsListener);
4316 AWTEventMulticaster.save(s, "mouseWheelL", mouseWheelListener);
4317 s.writeObject(null);
4324 * This class provides accessibility support for subclasses of container.
4326 * @author Eric Blake <ebb9@email.byu.edu>
4328 * @status updated to 1.4
4330 protected abstract class AccessibleAWTComponent extends AccessibleContext
4331 implements Serializable, AccessibleComponent
4334 * Compatible with JDK 1.3+.
4336 private static final long serialVersionUID = 642321655757800191L;
4339 * Converts show/hide events to PropertyChange events, and is registered
4340 * as a component listener on this component.
4342 * @serial the component handler
4344 protected ComponentListener accessibleAWTComponentHandler
4345 = new AccessibleAWTComponentHandler();
4348 * Converts focus events to PropertyChange events, and is registered
4349 * as a focus listener on this component.
4351 * @serial the focus handler
4353 protected FocusListener accessibleAWTFocusHandler
4354 = new AccessibleAWTFocusHandler();
4357 * The default constructor.
4359 protected AccessibleAWTComponent()
4361 Component.this.addComponentListener(accessibleAWTComponentHandler);
4362 Component.this.addFocusListener(accessibleAWTFocusHandler);
4366 * Adds a global property change listener to the accessible component.
4368 * @param l the listener to add
4369 * @see #ACCESSIBLE_NAME_PROPERTY
4370 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
4371 * @see #ACCESSIBLE_STATE_PROPERTY
4372 * @see #ACCESSIBLE_VALUE_PROPERTY
4373 * @see #ACCESSIBLE_SELECTION_PROPERTY
4374 * @see #ACCESSIBLE_TEXT_PROPERTY
4375 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
4377 public void addPropertyChangeListener(PropertyChangeListener l)
4379 Component.this.addPropertyChangeListener(l);
4380 super.addPropertyChangeListener(l);
4384 * Removes a global property change listener from this accessible
4387 * @param l the listener to remove
4389 public void removePropertyChangeListener(PropertyChangeListener l)
4391 Component.this.removePropertyChangeListener(l);
4392 super.removePropertyChangeListener(l);
4396 * Returns the accessible name of this component. It is almost always
4397 * wrong to return getName(), since it is not localized. In fact, for
4398 * things like buttons, this should be the text of the button, not the
4399 * name of the object. The tooltip text might also be appropriate.
4402 * @see #setAccessibleName(String)
4404 public String getAccessibleName()
4406 return accessibleName == null ? getName() : accessibleName;
4410 * Returns a brief description of this accessible context. This should
4413 * @return a description of this component
4414 * @see #setAccessibleDescription(String)
4416 public String getAccessibleDescription()
4418 return accessibleDescription;
4422 * Returns the role of this component.
4424 * @return the accessible role
4426 public AccessibleRole getAccessibleRole()
4428 return AccessibleRole.AWT_COMPONENT;
4432 * Returns a state set describing this component's state.
4434 * @return a new state set
4435 * @see AccessibleState
4437 public AccessibleStateSet getAccessibleStateSet()
4439 AccessibleStateSet s = new AccessibleStateSet();
4440 if (Component.this.isEnabled())
4441 s.add(AccessibleState.ENABLED);
4443 s.add(AccessibleState.FOCUSABLE);
4445 s.add(AccessibleState.FOCUSED);
4447 s.add(AccessibleState.OPAQUE);
4448 if (Component.this.isShowing())
4449 s.add(AccessibleState.SHOWING);
4450 if (Component.this.isVisible())
4451 s.add(AccessibleState.VISIBLE);
4456 * Returns the parent of this component, if it is accessible.
4458 * @return the accessible parent
4460 public Accessible getAccessibleParent()
4462 if (accessibleParent == null)
4464 Container parent = getParent();
4465 accessibleParent = parent instanceof Accessible
4466 ? (Accessible) parent : null;
4468 return accessibleParent;
4472 * Returns the index of this component in its accessible parent.
4474 * @return the index, or -1 if the parent is not accessible
4475 * @see #getAccessibleParent()
4477 public int getAccessibleIndexInParent()
4479 if (getAccessibleParent() == null)
4481 AccessibleContext context
4482 = ((Component) accessibleParent).getAccessibleContext();
4483 if (context == null)
4485 for (int i = context.getAccessibleChildrenCount(); --i >= 0; )
4486 if (context.getAccessibleChild(i) == Component.this)
4492 * Returns the number of children of this component which implement
4493 * Accessible. Subclasses must override this if they can have children.
4495 * @return the number of accessible children, default 0
4497 public int getAccessibleChildrenCount()
4503 * Returns the ith accessible child. Subclasses must override this if
4504 * they can have children.
4506 * @return the ith accessible child, or null
4507 * @see #getAccessibleChildrenCount()
4509 public Accessible getAccessibleChild(int i)
4515 * Returns the locale of this component.
4517 * @return the locale
4518 * @throws IllegalComponentStateException if the locale is unknown
4520 public Locale getLocale()
4522 return Component.this.getLocale();
4526 * Returns this, since it is an accessible component.
4528 * @return the accessible component
4530 public AccessibleComponent getAccessibleComponent()
4536 * Gets the background color.
4538 * @return the background color
4539 * @see #setBackground(Color)
4541 public Color getBackground()
4543 return Component.this.getBackground();
4547 * Sets the background color.
4549 * @param c the background color
4550 * @see #getBackground()
4553 public void setBackground(Color c)
4555 Component.this.setBackground(c);
4559 * Gets the foreground color.
4561 * @return the foreground color
4562 * @see #setForeground(Color)
4564 public Color getForeground()
4566 return Component.this.getForeground();
4570 * Sets the foreground color.
4572 * @param c the foreground color
4573 * @see #getForeground()
4575 public void setForeground(Color c)
4577 Component.this.setForeground(c);
4583 * @return the cursor
4584 * @see #setCursor(Cursor)
4586 public Cursor getCursor()
4588 return Component.this.getCursor();
4594 * @param cursor the cursor
4597 public void setCursor(Cursor cursor)
4599 Component.this.setCursor(cursor);
4606 * @see #setFont(Font)
4608 public Font getFont()
4610 return Component.this.getFont();
4619 public void setFont(Font f)
4621 Component.this.setFont(f);
4625 * Gets the font metrics for a font.
4627 * @param f the font to look up
4628 * @return its metrics
4629 * @throws NullPointerException if f is null
4632 public FontMetrics getFontMetrics(Font f)
4634 return Component.this.getFontMetrics(f);
4638 * Tests if the component is enabled.
4640 * @return true if the component is enabled
4641 * @see #setEnabled(boolean)
4642 * @see #getAccessibleStateSet()
4643 * @see AccessibleState#ENABLED
4645 public boolean isEnabled()
4647 return Component.this.isEnabled();
4651 * Set whether the component is enabled.
4653 * @param b the new enabled status
4656 public void setEnabled(boolean b)
4658 Component.this.setEnabled(b);
4662 * Test whether the component is visible (not necesarily showing).
4664 * @return true if it is visible
4665 * @see #setVisible(boolean)
4666 * @see #getAccessibleStateSet()
4667 * @see AccessibleState#VISIBLE
4669 public boolean isVisible()
4671 return Component.this.isVisible();
4675 * Sets the visibility of this component.
4677 * @param b the desired visibility
4680 public void setVisible(boolean b)
4682 Component.this.setVisible(b);
4686 * Tests if the component is showing.
4688 * @return true if this is showing
4690 public boolean isShowing()
4692 return Component.this.isShowing();
4696 * Tests if the point is contained in this component.
4698 * @param p the point to check
4699 * @return true if it is contained
4700 * @throws NullPointerException if p is null
4702 public boolean contains(Point p)
4704 return Component.this.contains(p.x, p.y);
4708 * Returns the location of this object on the screen, or null if it is
4711 * @return the location relative to screen coordinates, if showing
4713 * @see #getLocation()
4715 public Point getLocationOnScreen()
4717 return Component.this.isShowing() ? Component.this.getLocationOnScreen()
4722 * Returns the location of this object relative to its parent's coordinate
4723 * system, or null if it is not showing.
4725 * @return the location
4727 * @see #getLocationOnScreen()
4729 public Point getLocation()
4731 return Component.this.isShowing() ? Component.this.getLocation() : null;
4735 * Sets the location of this relative to its parent's coordinate system.
4737 * @param p the location
4738 * @throws NullPointerException if p is null
4739 * @see #getLocation()
4741 public void setLocation(Point p)
4743 Component.this.setLocation(p.x, p.y);
4747 * Gets the bounds of this component, or null if it is not on screen.
4749 * @return the bounds
4750 * @see #contains(Point)
4751 * @see #setBounds(Rectangle)
4753 public Rectangle getBounds()
4755 return Component.this.isShowing() ? Component.this.getBounds() : null;
4759 * Sets the bounds of this component.
4761 * @param r the bounds
4762 * @throws NullPointerException if r is null
4765 public void setBounds(Rectangle r)
4767 Component.this.setBounds(r.x, r.y, r.width, r.height);
4771 * Gets the size of this component, or null if it is not showing.
4774 * @see #setSize(Dimension)
4776 public Dimension getSize()
4778 return Component.this.isShowing() ? Component.this.getSize() : null;
4782 * Sets the size of this component.
4785 * @throws NullPointerException if d is null
4788 public void setSize(Dimension d)
4790 Component.this.setSize(d.width, d.height);
4794 * Returns the Accessible child at a point relative to the coordinate
4795 * system of this component, if one exists, or null. Since components
4796 * have no children, subclasses must override this to get anything besides
4799 * @param p the point to check
4800 * @return the accessible child at that point
4801 * @throws NullPointerException if p is null
4803 public Accessible getAccessibleAt(Point p)
4809 * Tests whether this component can accept focus.
4811 * @return true if this is focus traversable
4812 * @see #getAccessibleStateSet()
4813 * @see AccessibleState#FOCUSABLE
4814 * @see AccessibleState#FOCUSED
4816 public boolean isFocusTraversable()
4818 return Component.this.isFocusTraversable();
4822 * Requests focus for this component.
4824 * @see #isFocusTraversable()
4826 public void requestFocus()
4828 Component.this.requestFocus();
4832 * Adds a focus listener.
4834 * @param l the listener to add
4836 public void addFocusListener(FocusListener l)
4838 Component.this.addFocusListener(l);
4842 * Removes a focus listener.
4844 * @param l the listener to remove
4846 public void removeFocusListener(FocusListener l)
4848 Component.this.removeFocusListener(l);
4852 * Converts component changes into property changes.
4854 * @author Eric Blake <ebb9@email.byu.edu>
4856 * @status updated to 1.4
4858 protected class AccessibleAWTComponentHandler implements ComponentListener
4861 * Default constructor.
4863 protected AccessibleAWTComponentHandler()
4868 * Convert a component hidden to a property change.
4870 * @param e the event to convert
4872 public void componentHidden(ComponentEvent e)
4874 AccessibleAWTComponent.this.firePropertyChange
4875 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.VISIBLE, null);
4879 * Convert a component shown to a property change.
4881 * @param e the event to convert
4883 public void componentShown(ComponentEvent e)
4885 AccessibleAWTComponent.this.firePropertyChange
4886 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.VISIBLE);
4890 * Moving a component does not affect properties.
4894 public void componentMoved(ComponentEvent e)
4899 * Resizing a component does not affect properties.
4903 public void componentResized(ComponentEvent e)
4906 } // class AccessibleAWTComponentHandler
4909 * Converts focus changes into property changes.
4911 * @author Eric Blake <ebb9@email.byu.edu>
4913 * @status updated to 1.4
4915 protected class AccessibleAWTFocusHandler implements FocusListener
4918 * Default constructor.
4920 protected AccessibleAWTFocusHandler()
4925 * Convert a focus gained to a property change.
4927 * @param e the event to convert
4929 public void focusGained(FocusEvent e)
4931 AccessibleAWTComponent.this.firePropertyChange
4932 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED);
4936 * Convert a focus lost to a property change.
4938 * @param e the event to convert
4940 public void focusLost(FocusEvent e)
4942 AccessibleAWTComponent.this.firePropertyChange
4943 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null);
4945 } // class AccessibleAWTComponentHandler
4946 } // class AccessibleAWTComponent
4949 * This class provides support for blitting offscreen surfaces.
4951 * @author Eric Blake <ebb9@email.byu.edu>
4953 * @XXX Shell class, to allow compilation. This needs documentation and
4954 * correct implementation.
4956 protected class BltBufferStrategy extends BufferStrategy
4958 protected BufferCapabilities caps;
4959 protected VolatileImage[] backBuffers;
4960 protected boolean validatedContents;
4961 protected int width;
4962 protected int height;
4963 protected BltBufferStrategy(int num, BufferCapabilities caps)
4966 createBackBuffers(num);
4968 protected void createBackBuffers(int num)
4970 backBuffers = new VolatileImage[num];
4972 public BufferCapabilities getCapabilities()
4976 public Graphics getDrawGraphics() { return null; }
4977 public void show() {}
4978 protected void revalidate() {}
4979 public boolean contentsLost() { return false; }
4980 public boolean contentsRestored() { return false; }
4981 } // class BltBufferStrategy
4984 * This class provides support for flipping component buffers. It is only
4985 * designed for use by Canvas and Window.
4987 * @author Eric Blake <ebb9@email.byu.edu>
4989 * @XXX Shell class, to allow compilation. This needs documentation and
4990 * correct implementation.
4992 protected class FlipBufferStrategy extends BufferStrategy
4994 protected int numBuffers;
4995 protected BufferCapabilities caps;
4996 protected Image drawBuffer;
4997 protected VolatileImage drawVBuffer;
4998 protected boolean validatedContents;
4999 protected FlipBufferStrategy(int num, BufferCapabilities caps)
5003 createBuffers(num, caps);
5005 protected void createBuffers(int num, BufferCapabilities caps)
5006 throws AWTException {}
5007 protected Image getBackBuffer()
5011 protected void flip(BufferCapabilities.FlipContents flipAction) {}
5012 protected void destroyBuffers() {}
5013 public BufferCapabilities getCapabilities()
5017 public Graphics getDrawGraphics() { return null; }
5018 protected void revalidate() {}
5019 public boolean contentsLost() { return false; }
5020 public boolean contentsRestored() { return false; }
5021 public void show() {}
5022 } // class FlipBufferStrategy
5023 } // class Component