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 // Public and protected API.
562 * Default constructor for subclasses. When Component is extended directly,
563 * it forms a lightweight component that must be hosted in an opaque native
564 * container higher in the tree.
566 protected Component()
571 * Returns the name of this component.
573 * @return the name of this component
574 * @see #setName(String)
577 public String getName()
579 if (name == null && ! nameExplicitlySet)
580 name = generateName();
585 * Sets the name of this component to the specified name.
587 * @param name the new name of this component
591 public void setName(String name)
593 nameExplicitlySet = true;
598 * Returns the parent of this component.
600 * @return the parent of this component
602 public Container getParent()
608 * Returns the native windowing system peer for this component. Only the
609 * platform specific implementation code should call this method.
611 * @return the peer for this component
612 * @deprecated user programs should not directly manipulate peers; use
613 * {@link #isDisplayable()} instead
615 // Classpath's Gtk peers rely on this.
616 public ComponentPeer getPeer()
622 * Set the associated drag-and-drop target, which receives events when this
625 * @param dt the new drop target
628 public void setDropTarget(DropTarget dt)
630 this.dropTarget = dt;
634 * Gets the associated drag-and-drop target, if there is one.
636 * @return the drop target
638 public DropTarget getDropTarget()
644 * Returns the graphics configuration of this component, if there is one.
645 * If it has not been set, it is inherited from the parent.
647 * @return the graphics configuration, or null
650 public GraphicsConfiguration getGraphicsConfiguration()
652 return getGraphicsConfigurationImpl();
656 * Returns the object used for synchronization locks on this component
657 * when performing tree and layout functions.
659 * @return the synchronization lock for this component
661 public final Object getTreeLock()
667 * Returns the toolkit in use for this component. The toolkit is associated
668 * with the frame this component belongs to.
670 * @return the toolkit for this component
672 public Toolkit getToolkit()
676 Toolkit tk = peer.getToolkit();
680 // Get toolkit for lightweight component.
682 return parent.getToolkit();
683 return Toolkit.getDefaultToolkit();
687 * Tests whether or not this component is valid. A invalid component needs
688 * to have its layout redone.
690 * @return true if this component is valid
694 public boolean isValid()
700 * Tests if the component is displayable. It must be connected to a native
701 * screen resource, and all its ancestors must be displayable. A containment
702 * hierarchy is made displayable when a window is packed or made visible.
704 * @return true if the component is displayable
705 * @see Container#add(Component)
706 * @see Container#remove(Component)
709 * @see Window#dispose()
712 public boolean isDisplayable()
715 return parent.isDisplayable();
720 * Tests whether or not this component is visible. Except for top-level
721 * frames, components are initially visible.
723 * @return true if the component is visible
724 * @see #setVisible(boolean)
726 public boolean isVisible()
732 * Tests whether or not this component is actually being shown on
733 * the screen. This will be true if and only if it this component is
734 * visible and its parent components are all visible.
736 * @return true if the component is showing on the screen
737 * @see #setVisible(boolean)
739 public boolean isShowing()
741 if (! visible || peer == null)
744 return parent == null ? true : parent.isShowing();
748 * Tests whether or not this component is enabled. Components are enabled
749 * by default, and must be enabled to receive user input or generate events.
751 * @return true if the component is enabled
752 * @see #setEnabled(boolean)
754 public boolean isEnabled()
760 * Enables or disables this component. The component must be enabled to
761 * receive events (except that lightweight components always receive mouse
764 * @param enabled true to enable this component
766 * @see #isLightweight()
769 public void setEnabled(boolean b)
777 * Enables this component.
779 * @deprecated use {@link #setEnabled(boolean)} instead
787 * Enables or disables this component.
789 * @param enabled true to enable this component
790 * @deprecated use {@link #setEnabled(boolean)} instead
792 public void enable(boolean b)
798 * Disables this component.
800 * @deprecated use {@link #setEnabled(boolean)} instead
802 public void disable()
808 * Checks if this image is painted to an offscreen image buffer that is
809 * later copied to screen (double buffering reduces flicker). This version
810 * returns false, so subclasses must override it if they provide double
813 * @return true if this is double buffered; defaults to false
815 public boolean isDoubleBuffered()
821 * Enables or disables input method support for this component. By default,
822 * components have this enabled. Input methods are given the opportunity
823 * to process key events before this component and its listeners.
825 * @param enable true to enable input method processing
826 * @see #processKeyEvent(KeyEvent)
829 public void enableInputMethods(boolean enable)
832 throw new Error("not implemented");
836 * Makes this component visible or invisible. Note that it wtill might
837 * not show the component, if a parent is invisible.
839 * @param visible true to make this component visible
843 public void setVisible(boolean b)
845 // Inspection by subclassing shows that Sun's implementation calls
846 // show(boolean) which then calls show() or hide(). It is the show()
847 // method that is overriden in subclasses like Window.
855 * Makes this component visible on the screen.
857 * @deprecated use {@link #setVisible(boolean)} instead
862 peer.setVisible(true);
867 * Makes this component visible or invisible.
869 * @param visible true to make this component visible
870 * @deprecated use {@link #setVisible(boolean)} instead
872 public void show(boolean b)
878 * Hides this component so that it is no longer shown on the screen.
880 * @deprecated use {@link #setVisible(boolean)} instead
885 peer.setVisible(false);
886 this.visible = false;
890 * Returns this component's foreground color. If not set, this is inherited
893 * @return this component's foreground color, or null
894 * @see #setForeground(Color)
896 public Color getForeground()
898 if (foreground != null)
900 return parent == null ? null : parent.getForeground();
904 * Sets this component's foreground color to the specified color. This is a
907 * @param c the new foreground color
908 * @see #getForeground()
910 public void setForeground(Color c)
912 firePropertyChange("foreground", foreground, c);
914 peer.setForeground(c);
919 * Tests if the foreground was explicitly set, or just inherited from the
922 * @return true if the foreground has been set
925 public boolean isForegroundSet()
927 return foreground != null;
931 * Returns this component's background color. If not set, this is inherited
934 * @return the background color of the component, or null
935 * @see #setBackground(Color)
937 public Color getBackground()
939 if (background != null)
941 return parent == null ? null : parent.getBackground();
945 * Sets this component's background color to the specified color. The parts
946 * of the component affected by the background color may by system dependent.
947 * This is a bound property.
949 * @param c the new background color
950 * @see #getBackground()
952 public void setBackground(Color c)
954 firePropertyChange("background", background, c);
956 peer.setBackground(c);
961 * Tests if the background was explicitly set, or just inherited from the
964 * @return true if the background has been set
967 public boolean isBackgroundSet()
969 return background != null;
973 * Returns the font in use for this component. If not set, this is inherited
976 * @return the font for this component
977 * @see #setFont(Font)
979 public Font getFont()
983 return parent == null ? null : parent.getFont();
987 * Sets the font for this component to the specified font. This is a bound
990 * @param font the new font for this component
993 public void setFont(Font f)
995 firePropertyChange("font", font, f);
1002 * Tests if the font was explicitly set, or just inherited from the parent.
1004 * @return true if the font has been set
1007 public boolean isFontSet()
1009 return font != null;
1013 * Returns the locale for this component. If this component does not
1014 * have a locale, the locale of the parent component is returned.
1016 * @return the locale for this component
1017 * @throws IllegalComponentStateException if it has no locale or parent
1018 * @see setLocale(Locale)
1021 public Locale getLocale()
1026 throw new IllegalComponentStateException
1027 ("Component has no parent: can't determine Locale");
1028 return parent.getLocale();
1032 * Sets the locale for this component to the specified locale. This is a
1035 * @param locale the new locale for this component
1037 public void setLocale(Locale l)
1039 firePropertyChange("locale", locale, l);
1041 // New writing/layout direction or more/less room for localized labels.
1046 * Returns the color model of the device this componet is displayed on.
1048 * @return this object's color model
1049 * @see Toolkit#getColorModel()
1051 public ColorModel getColorModel()
1053 GraphicsConfiguration config = getGraphicsConfiguration();
1054 return config != null ? config.getColorModel()
1055 : getToolkit().getColorModel();
1059 * Returns the location of this component's top left corner relative to
1060 * its parent component. This may be outdated, so for synchronous behavior,
1061 * you should use a component listner.
1063 * @return the location of this component
1064 * @see #setLocation(int, int)
1065 * @see #getLocationOnScreen()
1068 public Point getLocation()
1070 return new Point(x, y);
1074 * Returns the location of this component's top left corner in screen
1077 * @return the location of this component in screen coordinates
1078 * @throws IllegalComponentStateException if the component is not showing
1080 public Point getLocationOnScreen()
1083 throw new IllegalComponentStateException("component not showing");
1084 // We know peer != null here.
1085 return peer.getLocationOnScreen();
1089 * Returns the location of this component's top left corner relative to
1090 * its parent component.
1092 * @return the location of this component
1093 * @deprecated use {@link #getLocation()} instead
1095 public Point location()
1097 return getLocation();
1101 * Moves this component to the specified location, relative to the parent's
1102 * coordinates. The coordinates are the new upper left corner of this
1105 * @param x the new X coordinate of this component
1106 * @param y the new Y coordinate of this component
1107 * @see #getLocation()
1108 * @see #setBounds(int, int, int, int)
1110 public void setLocation(int x, int y)
1112 if (this.x == x && this.y == y)
1118 peer.setBounds(x, y, width, height);
1122 * Moves this component to the specified location, relative to the parent's
1123 * coordinates. The coordinates are the new upper left corner of this
1126 * @param x the new X coordinate of this component
1127 * @param y the new Y coordinate of this component
1128 * @deprecated use {@link #setLocation(int, int)} instead
1130 public void move(int x, int y)
1136 * Moves this component to the specified location, relative to the parent's
1137 * coordinates. The coordinates are the new upper left corner of this
1140 * @param p new coordinates for this component
1141 * @throws NullPointerException if p is null
1142 * @see #getLocation()
1143 * @see #setBounds(int, int, int, int)
1146 public void setLocation(Point p)
1148 setLocation(p.x, p.y);
1152 * Returns the size of this object.
1154 * @return the size of this object
1155 * @see #setSize(int, int)
1158 public Dimension getSize()
1160 return new Dimension(width, height);
1164 * Returns the size of this object.
1166 * @return the size of this object
1167 * @deprecated use {@link #getSize()} instead
1169 public Dimension size()
1175 * Sets the size of this component to the specified width and height.
1177 * @param width the new width of this component
1178 * @param height the new height of this component
1180 * @see #setBounds(int, int, int, int)
1182 public void setSize(int width, int height)
1184 if (this.width == width && this.height == height)
1188 this.height = height;
1190 peer.setBounds(x, y, width, height);
1194 * Sets the size of this component to the specified value.
1196 * @param width the new width of the component
1197 * @param height the new height of the component
1198 * @deprecated use {@link #setSize(int, int)} instead
1200 public void resize(int width, int height)
1202 setSize(width, height);
1206 * Sets the size of this component to the specified value.
1208 * @param d the new size of this component
1209 * @throws NullPointerException if d is null
1210 * @see #setSize(int, int)
1211 * @see #setBounds(int, int, int, int)
1214 public void setSize(Dimension d)
1216 setSize(d.width, d.height);
1220 * Sets the size of this component to the specified value.
1222 * @param d the new size of this component
1223 * @throws NullPointerException if d is null
1224 * @deprecated use {@link #setSize(Dimension)} instead
1226 public void resize(Dimension d)
1228 setSize(d.width, d.height);
1232 * Returns a bounding rectangle for this component. Note that the
1233 * returned rectange is relative to this component's parent, not to
1236 * @return the bounding rectangle for this component
1237 * @see #setBounds(int, int, int, int)
1238 * @see #getLocation()
1241 public Rectangle getBounds()
1243 return new Rectangle(x, y, width, height);
1247 * Returns a bounding rectangle for this component. Note that the
1248 * returned rectange is relative to this component's parent, not to
1251 * @return the bounding rectangle for this component
1252 * @deprecated use {@link #getBounds()} instead
1254 public Rectangle bounds()
1260 * Sets the bounding rectangle for this component to the specified values.
1261 * Note that these coordinates are relative to the parent, not to the screen.
1263 * @param x the X coordinate of the upper left corner of the rectangle
1264 * @param y the Y coordinate of the upper left corner of the rectangle
1265 * @param w the width of the rectangle
1266 * @param h the height of the rectangle
1268 * @see #setLocation(int, int)
1269 * @see #setLocation(Point)
1270 * @see #setSize(int, int)
1271 * @see #setSize(Dimension)
1274 public void setBounds(int x, int y, int w, int h)
1276 if (this.x == x && this.y == y && width == w && height == h)
1284 peer.setBounds(x, y, w, h);
1288 * Sets the bounding rectangle for this component to the specified values.
1289 * Note that these coordinates are relative to the parent, not to the screen.
1291 * @param x the X coordinate of the upper left corner of the rectangle
1292 * @param y the Y coordinate of the upper left corner of the rectangle
1293 * @param w the width of the rectangle
1294 * @param h the height of the rectangle
1295 * @deprecated use {@link #setBounds(int, int, int, int)} instead
1297 public void reshape(int x, int y, int width, int height)
1299 setBounds(x, y, width, height);
1303 * Sets the bounding rectangle for this component to the specified
1304 * rectangle. Note that these coordinates are relative to the parent, not
1307 * @param r the new bounding rectangle
1308 * @throws NullPointerException if r is null
1310 * @see #setLocation(Point)
1311 * @see #setSize(Dimension)
1314 public void setBounds(Rectangle r)
1316 setBounds(r.x, r.y, r.width, r.height);
1320 * Gets the x coordinate of the upper left corner. This is more efficient
1321 * than getBounds().x or getLocation().x.
1323 * @return the current x coordinate
1332 * Gets the y coordinate of the upper left corner. This is more efficient
1333 * than getBounds().y or getLocation().y.
1335 * @return the current y coordinate
1344 * Gets the width of the component. This is more efficient than
1345 * getBounds().width or getSize().width.
1347 * @return the current width
1350 public int getWidth()
1356 * Gets the height of the component. This is more efficient than
1357 * getBounds().height or getSize().height.
1359 * @return the current width
1362 public int getHeight()
1368 * Returns the bounds of this component. This allows reuse of an existing
1369 * rectangle, if r is non-null.
1371 * @param r the rectangle to use, or null
1372 * @return the bounds
1374 public Rectangle getBounds(Rectangle r)
1377 r = new Rectangle();
1386 * Returns the size of this component. This allows reuse of an existing
1387 * dimension, if d is non-null.
1389 * @param d the dimension to use, or null
1392 public Dimension getSize(Dimension d)
1395 d = new Dimension();
1402 * Returns the location of this component. This allows reuse of an existing
1403 * point, if p is non-null.
1405 * @param p the point to use, or null
1406 * @return the location
1408 public Point getLocation(Point p)
1418 * Tests if this component is opaque. All "heavyweight" (natively-drawn)
1419 * components are opaque. A component is opaque if it draws all pixels in
1420 * the bounds; a lightweight component is partially transparent if it lets
1421 * pixels underneath show through. Subclasses that guarantee that all pixels
1422 * will be drawn should override this.
1424 * @return true if this is opaque
1425 * @see #isLightweight()
1428 public boolean isOpaque()
1430 return ! isLightweight();
1434 * Return whether the component is lightweight. That means the component has
1435 * no native peer, but is displayable. This applies to subclasses of
1436 * Component not in this package, such as javax.swing.
1438 * @return true if the component has a lightweight peer
1439 * @see #isDisplayable()
1442 public boolean isLightweight()
1444 return peer instanceof LightweightPeer;
1448 * Returns the component's preferred size.
1450 * @return the component's preferred size
1451 * @see #getMinimumSize()
1452 * @see LayoutManager
1454 public Dimension getPreferredSize()
1456 return preferredSize();
1460 * Returns the component's preferred size.
1462 * @return the component's preferred size
1463 * @deprecated use {@link #getPreferredSize()} instead
1465 public Dimension preferredSize()
1467 if (prefSize == null)
1469 return new Dimension(width, height);
1471 prefSize = peer.getPreferredSize();
1476 * Returns the component's minimum size.
1478 * @return the component's minimum size
1479 * @see #getPreferredSize()
1480 * @see LayoutManager
1482 public Dimension getMinimumSize()
1484 return minimumSize();
1488 * Returns the component's minimum size.
1490 * @return the component's minimum size
1491 * @deprecated use {@link #getMinimumSize()} instead
1493 public Dimension minimumSize()
1495 if (minSize == null)
1496 minSize = (peer != null ? peer.getMinimumSize()
1497 : new Dimension(width, height));
1502 * Returns the component's maximum size.
1504 * @return the component's maximum size
1505 * @see #getMinimumSize()
1506 * @see #getPreferredSize()
1507 * @see LayoutManager
1509 public Dimension getMaximumSize()
1511 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1515 * Returns the preferred horizontal alignment of this component. The value
1516 * returned will be between {@link #LEFT_ALIGNMENT} and
1517 * {@link #RIGHT_ALIGNMENT}, inclusive.
1519 * @return the preferred horizontal alignment of this component
1521 public float getAlignmentX()
1523 return CENTER_ALIGNMENT;
1527 * Returns the preferred vertical alignment of this component. The value
1528 * returned will be between {@link #TOP_ALIGNMENT} and
1529 * {@link #BOTTOM_ALIGNMENT}, inclusive.
1531 * @return the preferred vertical alignment of this component
1533 public float getAlignmentY()
1535 return CENTER_ALIGNMENT;
1539 * Calls the layout manager to re-layout the component. This is called
1540 * during validation of a container in most cases.
1543 * @see LayoutManager
1545 public void doLayout()
1547 // nothing to do unless we're a container
1551 * Calls the layout manager to re-layout the component. This is called
1552 * during validation of a container in most cases.
1554 * @deprecated use {@link #doLayout()} instead
1556 public void layout()
1562 * Called to ensure that the layout for this component is valid. This is
1563 * usually called on containers.
1565 * @see #invalidate()
1567 * @see LayoutManager
1568 * @see Container#validate()
1570 public void validate()
1576 * Invalidates this component and all of its parent components. This will
1577 * cause them to have their layout redone. This is called frequently, so
1580 public void invalidate()
1585 if (parent != null && parent.valid)
1586 parent.invalidate();
1590 * Returns a graphics object for this component. Returns <code>null</code>
1591 * if this component is not currently displayed on the screen.
1593 * @return a graphics object for this component
1594 * @see #paint(Graphics)
1596 public Graphics getGraphics()
1600 Graphics gfx = peer.getGraphics();
1603 // create graphics for lightweight:
1604 Container parent = getParent();
1607 gfx = parent.getGraphics();
1608 Rectangle bounds = getBounds();
1609 gfx.setClip(bounds);
1610 gfx.translate(bounds.x, bounds.y);
1618 * Returns the font metrics for the specified font in this component.
1620 * @param font the font to retrieve metrics for
1621 * @return the font metrics for the specified font
1622 * @throws NullPointerException if font is null
1624 * @see Toolkit#getFontMetrics(Font)
1626 public FontMetrics getFontMetrics(Font font)
1628 return peer == null ? getToolkit().getFontMetrics(font)
1629 : peer.getFontMetrics(font);
1633 * Sets the cursor for this component to the specified cursor. The cursor
1634 * is displayed when the point is contained by the component, and the
1635 * component is visible, displayable, and enabled. This is inherited by
1636 * subcomponents unless they set their own cursor.
1638 * @param cursor the new cursor for this component
1642 * @see #contains(int, int)
1643 * @see Toolkit#createCustomCursor(Image, Point, String)
1645 public void setCursor(Cursor cursor)
1647 this.cursor = cursor;
1649 peer.setCursor(cursor);
1653 * Returns the cursor for this component. If not set, this is inherited
1654 * from the parent, or from Cursor.getDefaultCursor().
1656 * @return the cursor for this component
1658 public Cursor getCursor()
1662 return parent != null ? parent.getCursor() : Cursor.getDefaultCursor();
1666 * Tests if the cursor was explicitly set, or just inherited from the parent.
1668 * @return true if the cursor has been set
1671 public boolean isCursorSet()
1673 return cursor != null;
1677 * Paints this component on the screen. The clipping region in the graphics
1678 * context will indicate the region that requires painting. This is called
1679 * whenever the component first shows, or needs to be repaired because
1680 * something was temporarily drawn on top. It is not necessary for
1681 * subclasses to call <code>super.paint(g)</code>. Components with no area
1684 * @param g the graphics context for this paint job
1685 * @see #update(Graphics)
1687 public void paint(Graphics g)
1692 * Updates this component. This is called in response to
1693 * <code>repaint</code>. This method fills the component with the
1694 * background color, then sets the foreground color of the specified
1695 * graphics context to the foreground color of this component and calls
1696 * the <code>paint()</code> method. The coordinates of the graphics are
1697 * relative to this component. Subclasses should call either
1698 * <code>super.update(g)</code> or <code>paint(g)</code>.
1700 * @param graphics the graphics context for this update
1701 * @see #paint(Graphics)
1704 public void update(Graphics g)
1710 * Paints this entire component, including any sub-components.
1712 * @param graphics the graphics context for this paint job
1713 * @see #paint(Graphics)
1715 public void paintAll(Graphics g)
1725 * Repaint this entire component. The <code>update()</code> method
1726 * on this component will be called as soon as possible.
1728 * @see #update(Graphics)
1729 * @see #repaint(long, int, int, int, int)
1731 public void repaint()
1733 repaint(0, 0, 0, width, height);
1737 * Repaint this entire component. The <code>update()</code> method on this
1738 * component will be called in approximate the specified number of
1741 * @param tm milliseconds before this component should be repainted
1742 * @see #paint(Graphics)
1743 * @see #repaint(long, int, int, int, int)
1745 public void repaint(long tm)
1747 repaint(tm, 0, 0, width, height);
1751 * Repaints the specified rectangular region within this component. The
1752 * <code>update</code> method on this component will be called as soon as
1753 * possible. The coordinates are relative to this component.
1755 * @param x the X coordinate of the upper left of the region to repaint
1756 * @param y the Y coordinate of the upper left of the region to repaint
1757 * @param w the width of the region to repaint
1758 * @param h the height of the region to repaint
1759 * @see #update(Graphics)
1760 * @see #repaint(long, int, int, int, int)
1762 public void repaint(int x, int y, int w, int h)
1764 repaint(0, x, y, w, h);
1768 * Repaints the specified rectangular region within this component. The
1769 * <code>update</code> method on this component will be called in
1770 * approximately the specified number of milliseconds. The coordinates
1771 * are relative to this component.
1773 * @param tm milliseconds before this component should be repainted
1774 * @param x the X coordinate of the upper left of the region to repaint
1775 * @param y the Y coordinate of the upper left of the region to repaint
1776 * @param w the width of the region to repaint
1777 * @param h the height of the region to repaint
1778 * @see #update(Graphics)
1780 public void repaint(long tm, int x, int y, int width, int height)
1782 // Handle lightweight repainting by forwarding to native parent
1783 if (isLightweight() && parent != null)
1786 parent.repaint(tm, x + getX(), y + getY(), width, height);
1788 else if (peer != null)
1789 peer.repaint(tm, x, y, width, height);
1793 * Prints this component. This method is provided so that printing can be
1794 * done in a different manner from painting. However, the implementation
1795 * in this class simply calls the <code>paint()</code> method.
1797 * @param graphics the graphics context of the print device
1798 * @see #paint(Graphics)
1800 public void print(Graphics g)
1806 * Prints this component, including all sub-components. This method is
1807 * provided so that printing can be done in a different manner from
1808 * painting. However, the implementation in this class simply calls the
1809 * <code>paintAll()</code> method.
1811 * @param graphics the graphics context of the print device
1812 * @see #paintAll(Graphics)
1814 public void printAll(Graphics g)
1820 * Called when an image has changed so that this component is repainted.
1821 * This incrementally draws an image as more bits are available, when
1822 * possible. Incremental drawing is enabled if the system property
1823 * <code>awt.image.incrementalDraw</code> is not present or is true, in which
1824 * case the redraw rate is set to 100ms or the value of the system property
1825 * <code>awt.image.redrawrate</code>.
1827 * <p>The coordinate system used depends on the particular flags.
1829 * @param image the image that has been updated
1830 * @param flags tlags as specified in <code>ImageObserver</code>
1831 * @param x the X coordinate
1832 * @param y the Y coordinate
1833 * @param w the width
1834 * @param h the height
1835 * @return true if the image has been fully loaded
1836 * @see ImageObserver
1837 * @see Graphics#drawImage(Image, int, int, Color, ImageObserver)
1838 * @see Graphics#drawImage(Image, int, int, ImageObserver)
1839 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
1840 * @see Graphics#drawImage(Image, int, int, int, int, ImageObserver)
1841 * @see ImageObserver#update(Image, int, int, int, int, int)
1843 public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
1846 throw new Error("not implemented");
1850 * Creates an image from the specified producer.
1852 * @param producer the image procedure to create the image from
1853 * @return the resulting image
1855 public Image createImage(ImageProducer producer)
1857 // XXX What if peer or producer is null?
1858 return peer.createImage(producer);
1862 * Creates an image with the specified width and height for use in
1863 * double buffering. Headless environments do not support images.
1865 * @param width the width of the image
1866 * @param height the height of the image
1867 * @return the requested image, or null if it is not supported
1869 public Image createImage (int width, int height)
1871 Image returnValue = null;
1872 if (!GraphicsEnvironment.isHeadless ())
1874 if (isLightweight () && parent != null)
1875 returnValue = parent.createImage (width, height);
1876 else if (peer != null)
1877 returnValue = peer.createImage (width, height);
1883 * Creates an image with the specified width and height for use in
1884 * double buffering. Headless environments do not support images.
1886 * @param width the width of the image
1887 * @param height the height of the image
1888 * @return the requested image, or null if it is not supported
1891 public VolatileImage createVolatileImage(int width, int height)
1893 if (GraphicsEnvironment.isHeadless())
1895 GraphicsConfiguration config = getGraphicsConfiguration();
1896 return config == null ? null
1897 : config.createCompatibleVolatileImage(width, height);
1901 * Creates an image with the specified width and height for use in
1902 * double buffering. Headless environments do not support images. The image
1903 * will support the specified capabilities.
1905 * @param width the width of the image
1906 * @param height the height of the image
1907 * @param caps the requested capabilities
1908 * @return the requested image, or null if it is not supported
1909 * @throws AWTException if a buffer with the capabilities cannot be created
1912 public VolatileImage createVolatileImage(int width, int height,
1913 ImageCapabilities caps)
1916 if (GraphicsEnvironment.isHeadless())
1918 GraphicsConfiguration config = getGraphicsConfiguration();
1919 return config == null ? null
1920 : config.createCompatibleVolatileImage(width, height, caps);
1924 * Prepares the specified image for rendering on this component.
1926 * @param image the image to prepare for rendering
1927 * @param observer the observer to notify of image preparation status
1928 * @return true if the image is already fully prepared
1929 * @throws NullPointerException if image is null
1931 public boolean prepareImage(Image image, ImageObserver observer)
1933 return prepareImage(image, image.getWidth(observer),
1934 image.getHeight(observer), observer);
1938 * Prepares the specified image for rendering on this component at the
1939 * specified scaled width and height
1941 * @param image the image to prepare for rendering
1942 * @param width the scaled width of the image
1943 * @param height the scaled height of the image
1944 * @param observer the observer to notify of image preparation status
1945 * @return true if the image is already fully prepared
1947 public boolean prepareImage(Image image, int width, int height,
1948 ImageObserver observer)
1951 return peer.prepareImage(image, width, height, observer);
1953 return getToolkit().prepareImage(image, width, height, observer);
1957 * Returns the status of the loading of the specified image. The value
1958 * returned will be those flags defined in <code>ImageObserver</code>.
1960 * @param image the image to check on
1961 * @param observer the observer to notify of image loading progress
1962 * @return the image observer flags indicating the status of the load
1963 * @see #prepareImage(Image, int, int, ImageObserver)
1964 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
1965 * @throws NullPointerException if image is null
1967 public int checkImage(Image image, ImageObserver observer)
1969 return checkImage(image, -1, -1, observer);
1973 * Returns the status of the loading of the specified image. The value
1974 * returned will be those flags defined in <code>ImageObserver</code>.
1976 * @param image the image to check on
1977 * @param width the scaled image width
1978 * @param height the scaled image height
1979 * @param observer the observer to notify of image loading progress
1980 * @return the image observer flags indicating the status of the load
1981 * @see #prepareImage(Image, int, int, ImageObserver)
1982 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
1984 public int checkImage(Image image, int width, int height,
1985 ImageObserver observer)
1988 return peer.checkImage(image, width, height, observer);
1989 return getToolkit().checkImage(image, width, height, observer);
1993 * Sets whether paint messages delivered by the operating system should be
1994 * ignored. This does not affect messages from AWT, except for those
1995 * triggered by OS messages. Setting this to true can allow faster
1996 * performance in full-screen mode or page-flipping.
1998 * @param ignoreRepaint the new setting for ignoring repaint events
1999 * @see #getIgnoreRepaint()
2000 * @see BufferStrategy
2001 * @see GraphicsDevice.setFullScreenWindow(Window)
2004 public void setIgnoreRepaint(boolean ignoreRepaint)
2006 this.ignoreRepaint = ignoreRepaint;
2010 * Test whether paint events from the operating system are ignored.
2012 * @return the status of ignoring paint events
2013 * @see #setIgnoreRepaint(boolean)
2016 public boolean getIgnoreRepaint()
2018 return ignoreRepaint;
2022 * Tests whether or not the specified point is contained within this
2023 * component. Coordinates are relative to this component.
2025 * @param x the X coordinate of the point to test
2026 * @param y the Y coordinate of the point to test
2027 * @return true if the point is within this component
2028 * @see #getComponentAt(int, int)
2030 public boolean contains(int x, int y)
2032 return x >= 0 && y >= 0 && x < width && y < height;
2036 * Tests whether or not the specified point is contained within this
2037 * component. Coordinates are relative to this component.
2039 * @param x the X coordinate of the point to test
2040 * @param y the Y coordinate of the point to test
2041 * @return true if the point is within this component
2042 * @deprecated use {@link #contains(int, int)} instead
2044 public boolean inside(int x, int y)
2046 return contains(x, y);
2050 * Tests whether or not the specified point is contained within this
2051 * component. Coordinates are relative to this component.
2053 * @param p the point to test
2054 * @return true if the point is within this component
2055 * @throws NullPointerException if p is null
2056 * @see #getComponentAt(Point)
2059 public boolean contains(Point p)
2061 return contains(p.x, p.y);
2065 * Returns the component occupying the position (x,y). This will either
2066 * be this component, an immediate child component, or <code>null</code>
2067 * if neither of the first two occupies the specified location.
2069 * @param x the X coordinate to search for components at
2070 * @param y the Y coordinate to search for components at
2071 * @return the component at the specified location, or null
2072 * @see #contains(int, int)
2074 public Component getComponentAt(int x, int y)
2076 return contains(x, y) ? this : null;
2080 * Returns the component occupying the position (x,y). This will either
2081 * be this component, an immediate child component, or <code>null</code>
2082 * if neither of the first two occupies the specified location.
2084 * @param x the X coordinate to search for components at
2085 * @param y the Y coordinate to search for components at
2086 * @return the component at the specified location, or null
2087 * @deprecated use {@link #getComponentAt(int, int)} instead
2089 public Component locate(int x, int y)
2091 return getComponentAt(x, y);
2095 * Returns the component occupying the position (x,y). This will either
2096 * be this component, an immediate child component, or <code>null</code>
2097 * if neither of the first two occupies the specified location.
2099 * @param p the point to search for components at
2100 * @return the component at the specified location, or null
2101 * @throws NullPointerException if p is null
2102 * @see #contains(Point)
2105 public Component getComponentAt(Point p)
2107 return getComponentAt(p.x, p.y);
2111 * AWT 1.0 event dispatcher.
2113 * @param e the event to dispatch
2114 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2116 public void deliverEvent(Event e)
2118 // XXX Add backward compatibility handling.
2122 * Forwards AWT events to processEvent() if:<ul>
2123 * <li>Events have been enabled for this type of event via
2124 * <code>enableEvents()</code></li>,
2125 * <li>There is at least one registered listener for this type of event</li>
2128 * @param e the event to dispatch
2130 public final void dispatchEvent(AWTEvent e)
2132 // Some subclasses in the AWT package need to override this behavior,
2133 // hence the use of dispatchEventImpl().
2134 dispatchEventImpl(e);
2135 if (peer != null && ! e.consumed)
2136 peer.handleEvent(e);
2140 * AWT 1.0 event dispatcher.
2142 * @param e the event to dispatch
2143 * @return false: since the method was deprecated, the return has no meaning
2144 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2146 public boolean postEvent(Event e)
2148 // XXX Add backward compatibility handling.
2153 * Adds the specified listener to this component. This is harmless if the
2154 * listener is null, but if the listener has already been registered, it
2155 * will now be registered twice.
2157 * @param listener the new listener to add
2158 * @see ComponentEvent
2159 * @see #removeComponentListener(ComponentListener)
2160 * @see #getComponentListeners()
2163 public synchronized void addComponentListener(ComponentListener l)
2165 componentListener = AWTEventMulticaster.add(componentListener, l);
2166 if (componentListener != null)
2167 enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
2171 * Removes the specified listener from the component. This is harmless if
2172 * the listener was not previously registered.
2174 * @param listener the listener to remove
2175 * @see ComponentEvent
2176 * @see #addComponentListener(ComponentListener)
2177 * @see #getComponentListeners()
2180 public synchronized void removeComponentListener(ComponentListener l)
2182 componentListener = AWTEventMulticaster.remove(componentListener, l);
2186 * Returns an array of all specified listeners registered on this component.
2188 * @return an array of listeners
2189 * @see #addComponentListener(ComponentListener)
2190 * @see #removeComponentListener(ComponentListener)
2193 public synchronized ComponentListener[] getComponentListeners()
2195 return (ComponentListener[])
2196 AWTEventMulticaster.getListeners(componentListener,
2197 ComponentListener.class);
2201 * Adds the specified listener to this component. This is harmless if the
2202 * listener is null, but if the listener has already been registered, it
2203 * will now be registered twice.
2205 * @param listener the new listener to add
2207 * @see #removeFocusListener(FocusListener)
2208 * @see #getFocusListeners()
2211 public synchronized void addFocusListener(FocusListener l)
2213 focusListener = AWTEventMulticaster.add(focusListener, l);
2214 if (focusListener != null)
2215 enableEvents(AWTEvent.FOCUS_EVENT_MASK);
2219 * Removes the specified listener from the component. This is harmless if
2220 * the listener was not previously registered.
2222 * @param listener the listener to remove
2224 * @see #addFocusListener(FocusListener)
2225 * @see #getFocusListeners()
2228 public synchronized void removeFocusListener(FocusListener l)
2230 focusListener = AWTEventMulticaster.remove(focusListener, l);
2234 * Returns an array of all specified listeners registered on this component.
2236 * @return an array of listeners
2237 * @see #addFocusListener(FocusListener)
2238 * @see #removeFocusListener(FocusListener)
2241 public synchronized FocusListener[] getFocusListeners()
2243 return (FocusListener[])
2244 AWTEventMulticaster.getListeners(focusListener, FocusListener.class);
2248 * Adds the specified listener to this component. This is harmless if the
2249 * listener is null, but if the listener has already been registered, it
2250 * will now be registered twice.
2252 * @param listener the new listener to add
2253 * @see HierarchyEvent
2254 * @see #removeHierarchyListener(HierarchyListener)
2255 * @see #getHierarchyListeners()
2258 public synchronized void addHierarchyListener(HierarchyListener l)
2260 hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
2261 if (hierarchyListener != null)
2262 enableEvents(AWTEvent.HIERARCHY_EVENT_MASK);
2266 * Removes the specified listener from the component. This is harmless if
2267 * the listener was not previously registered.
2269 * @param listener the listener to remove
2270 * @see HierarchyEvent
2271 * @see #addHierarchyListener(HierarchyListener)
2272 * @see #getHierarchyListeners()
2275 public synchronized void removeHierarchyListener(HierarchyListener l)
2277 hierarchyListener = AWTEventMulticaster.remove(hierarchyListener, l);
2281 * Returns an array of all specified listeners registered on this component.
2283 * @return an array of listeners
2284 * @see #addHierarchyListener(HierarchyListener)
2285 * @see #removeHierarchyListener(HierarchyListener)
2288 public synchronized HierarchyListener[] getHierarchyListeners()
2290 return (HierarchyListener[])
2291 AWTEventMulticaster.getListeners(hierarchyListener,
2292 HierarchyListener.class);
2296 * Adds the specified listener to this component. This is harmless if the
2297 * listener is null, but if the listener has already been registered, it
2298 * will now be registered twice.
2300 * @param listener the new listener to add
2301 * @see HierarchyEvent
2302 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2303 * @see #getHierarchyBoundsListeners()
2306 public synchronized void
2307 addHierarchyBoundsListener(HierarchyBoundsListener l)
2309 hierarchyBoundsListener =
2310 AWTEventMulticaster.add(hierarchyBoundsListener, l);
2311 if (hierarchyBoundsListener != null)
2312 enableEvents(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
2316 * Removes the specified listener from the component. This is harmless if
2317 * the listener was not previously registered.
2319 * @param listener the listener to remove
2320 * @see HierarchyEvent
2321 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2322 * @see #getHierarchyBoundsListeners()
2325 public synchronized void
2326 removeHierarchyBoundsListener(HierarchyBoundsListener l)
2328 hierarchyBoundsListener =
2329 AWTEventMulticaster.remove(hierarchyBoundsListener, l);
2333 * Returns an array of all specified listeners registered on this component.
2335 * @return an array of listeners
2336 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2337 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2340 public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners()
2342 return (HierarchyBoundsListener[])
2343 AWTEventMulticaster.getListeners(hierarchyBoundsListener,
2344 HierarchyBoundsListener.class);
2348 * Adds the specified listener to this component. This is harmless if the
2349 * listener is null, but if the listener has already been registered, it
2350 * will now be registered twice.
2352 * @param listener the new listener to add
2354 * @see #removeKeyListener(KeyListener)
2355 * @see #getKeyListeners()
2358 public synchronized void addKeyListener(KeyListener l)
2360 keyListener = AWTEventMulticaster.add(keyListener, l);
2361 if (keyListener != null)
2362 enableEvents(AWTEvent.KEY_EVENT_MASK);
2366 * Removes the specified listener from the component. This is harmless if
2367 * the listener was not previously registered.
2369 * @param listener the listener to remove
2371 * @see #addKeyListener(KeyListener)
2372 * @see #getKeyListeners()
2375 public synchronized void removeKeyListener(KeyListener l)
2377 keyListener = AWTEventMulticaster.remove(keyListener, l);
2381 * Returns an array of all specified listeners registered on this component.
2383 * @return an array of listeners
2384 * @see #addKeyListener(KeyListener)
2385 * @see #removeKeyListener(KeyListener)
2388 public synchronized KeyListener[] getKeyListeners()
2390 return (KeyListener[])
2391 AWTEventMulticaster.getListeners(keyListener, KeyListener.class);
2395 * Adds the specified listener to this component. This is harmless if the
2396 * listener is null, but if the listener has already been registered, it
2397 * will now be registered twice.
2399 * @param listener the new listener to add
2401 * @see #removeMouseListener(MouseListener)
2402 * @see #getMouseListeners()
2405 public synchronized void addMouseListener(MouseListener l)
2407 mouseListener = AWTEventMulticaster.add(mouseListener, l);
2408 if (mouseListener != null)
2409 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2413 * Removes the specified listener from the component. This is harmless if
2414 * the listener was not previously registered.
2416 * @param listener the listener to remove
2418 * @see #addMouseListener(MouseListener)
2419 * @see #getMouseListeners()
2422 public synchronized void removeMouseListener(MouseListener l)
2424 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
2428 * Returns an array of all specified listeners registered on this component.
2430 * @return an array of listeners
2431 * @see #addMouseListener(MouseListener)
2432 * @see #removeMouseListener(MouseListener)
2435 public synchronized MouseListener[] getMouseListeners()
2437 return (MouseListener[])
2438 AWTEventMulticaster.getListeners(mouseListener, MouseListener.class);
2442 * Adds the specified listener to this component. This is harmless if the
2443 * listener is null, but if the listener has already been registered, it
2444 * will now be registered twice.
2446 * @param listener the new listener to add
2448 * @see #removeMouseMotionListener(MouseMotionListener)
2449 * @see #getMouseMotionListeners()
2452 public synchronized void addMouseMotionListener(MouseMotionListener l)
2454 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, l);
2455 if (mouseMotionListener != null)
2456 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2460 * Removes the specified listener from the component. This is harmless if
2461 * the listener was not previously registered.
2463 * @param listener the listener to remove
2465 * @see #addMouseMotionListener(MouseMotionListener)
2466 * @see #getMouseMotionListeners()
2469 public synchronized void removeMouseMotionListener(MouseMotionListener l)
2471 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
2475 * Returns an array of all specified listeners registered on this component.
2477 * @return an array of listeners
2478 * @see #addMouseMotionListener(MouseMotionListener)
2479 * @see #removeMouseMotionListener(MouseMotionListener)
2482 public synchronized MouseMotionListener[] getMouseMotionListeners()
2484 return (MouseMotionListener[])
2485 AWTEventMulticaster.getListeners(mouseMotionListener,
2486 MouseMotionListener.class);
2490 * Adds the specified listener to this component. This is harmless if the
2491 * listener is null, but if the listener has already been registered, it
2492 * will now be registered twice.
2494 * @param listener the new listener to add
2496 * @see MouseWheelEvent
2497 * @see #removeMouseWheelListener(MouseWheelListener)
2498 * @see #getMouseWheelListeners()
2501 public synchronized void addMouseWheelListener(MouseWheelListener l)
2503 mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, l);
2504 if (mouseWheelListener != null)
2505 enableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK);
2509 * Removes the specified listener from the component. This is harmless if
2510 * the listener was not previously registered.
2512 * @param listener the listener to remove
2514 * @see MouseWheelEvent
2515 * @see #addMouseWheelListener(MouseWheelListener)
2516 * @see #getMouseWheelListeners()
2519 public synchronized void removeMouseWheelListener(MouseWheelListener l)
2521 mouseWheelListener = AWTEventMulticaster.remove(mouseWheelListener, l);
2525 * Returns an array of all specified listeners registered on this component.
2527 * @return an array of listeners
2528 * @see #addMouseWheelListener(MouseWheelListener)
2529 * @see #removeMouseWheelListener(MouseWheelListener)
2532 public synchronized MouseWheelListener[] getMouseWheelListeners()
2534 return (MouseWheelListener[])
2535 AWTEventMulticaster.getListeners(mouseWheelListener,
2536 MouseWheelListener.class);
2540 * Adds the specified listener to this component. This is harmless if the
2541 * listener is null, but if the listener has already been registered, it
2542 * will now be registered twice.
2544 * @param listener the new listener to add
2545 * @see InputMethodEvent
2546 * @see #removeInputMethodListener(InputMethodListener)
2547 * @see #getInputMethodListeners()
2548 * @see #getInputMethodRequests()
2551 public synchronized void addInputMethodListener(InputMethodListener l)
2553 inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
2554 if (inputMethodListener != null)
2555 enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK);
2559 * Removes the specified listener from the component. This is harmless if
2560 * the listener was not previously registered.
2562 * @param listener the listener to remove
2563 * @see InputMethodEvent
2564 * @see #addInputMethodListener(InputMethodListener)
2565 * @see #getInputMethodRequests()
2568 public synchronized void removeInputMethodListener(InputMethodListener l)
2570 inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
2574 * Returns an array of all specified listeners registered on this component.
2576 * @return an array of listeners
2577 * @see #addInputMethodListener(InputMethodListener)
2578 * @see #removeInputMethodListener(InputMethodListener)
2581 public synchronized InputMethodListener[] getInputMethodListeners()
2583 return (InputMethodListener[])
2584 AWTEventMulticaster.getListeners(inputMethodListener,
2585 InputMethodListener.class);
2589 * Returns all registered EventListers of the given listenerType.
2591 * @param listenerType the class of listeners to filter
2592 * @return an array of registered listeners
2593 * @see #getComponentListeners()
2594 * @see #getFocusListeners()
2595 * @see #getHierarchyListeners()
2596 * @see #getHierarchyBoundsListeners()
2597 * @see #getKeyListeners()
2598 * @see #getMouseListeners()
2599 * @see #getMouseMotionListeners()
2600 * @see #getMouseWheelListeners()
2601 * @see #getInputMethodListeners()
2602 * @see #getPropertyChangeListeners()
2605 public EventListener[] getListeners(Class listenerType)
2607 if (listenerType == ComponentListener.class)
2608 return getComponentListeners();
2609 if (listenerType == FocusListener.class)
2610 return getFocusListeners();
2611 if (listenerType == HierarchyListener.class)
2612 return getHierarchyListeners();
2613 if (listenerType == HierarchyBoundsListener.class)
2614 return getHierarchyBoundsListeners();
2615 if (listenerType == KeyListener.class)
2616 return getKeyListeners();
2617 if (listenerType == MouseListener.class)
2618 return getMouseListeners();
2619 if (listenerType == MouseMotionListener.class)
2620 return getMouseMotionListeners();
2621 if (listenerType == MouseWheelListener.class)
2622 return getMouseWheelListeners();
2623 if (listenerType == InputMethodListener.class)
2624 return getInputMethodListeners();
2625 if (listenerType == PropertyChangeListener.class)
2626 return getPropertyChangeListeners();
2627 return (EventListener[]) Array.newInstance(listenerType, 0);
2631 * Returns the input method request handler, for subclasses which support
2632 * on-the-spot text input. By default, input methods are handled by AWT,
2633 * and this returns null.
2635 * @return the input method handler, null by default
2638 public InputMethodRequests getInputMethodRequests()
2644 * Gets the input context of this component, which is inherited from the
2645 * parent unless this is overridden.
2647 * @return the text input context
2650 public InputContext getInputContext()
2652 return parent == null ? null : parent.getInputContext();
2656 * Enables the specified events. The events to enable are specified
2657 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2659 * <p>Events are enabled by default when a listener is attached to the
2660 * component for that event type. This method can be used by subclasses
2661 * to ensure the delivery of a specified event regardless of whether
2662 * or not a listener is attached.
2664 * @param eventsToEnable the desired events to enable
2665 * @see #processEvent(AWTEvent)
2666 * @see #disableEvents(long)
2670 protected final void enableEvents(long eventsToEnable)
2672 eventMask |= eventsToEnable;
2673 // TODO: Unlike Sun's implementation, I think we should try and
2674 // enable/disable events at the peer (gtk/X) level. This will avoid
2675 // clogging the event pipeline with useless mousemove events that
2676 // we arn't interested in, etc. This will involve extending the peer
2677 // interface, but thats okay because the peer interfaces have been
2678 // deprecated for a long time, and no longer feature in the
2679 // API specification at all.
2680 if (isLightweight() && parent != null)
2681 parent.enableEvents(eventsToEnable);
2682 else if (peer != null)
2683 peer.setEventMask(eventMask);
2687 * Disables the specified events. The events to disable are specified
2688 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2690 * @param eventsToDisable the desired events to disable
2691 * @see #enableEvents(long)
2694 protected final void disableEvents(long eventsToDisable)
2696 eventMask &= ~eventsToDisable;
2697 // forward new event mask to peer?
2701 * This is called by the EventQueue if two events with the same event id
2702 * and owner component are queued. Returns a new combined event, or null if
2703 * no combining is done. The coelesced events are currently mouse moves
2704 * (intermediate ones are discarded) and paint events (a merged paint is
2705 * created in place of the two events).
2707 * @param existingEvent the event on the queue
2708 * @param newEvent the new event that might be entered on the queue
2709 * @return null if both events are kept, or the replacement coelesced event
2711 protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent)
2713 switch (existingEvent.id)
2715 case MouseEvent.MOUSE_MOVED:
2716 case MouseEvent.MOUSE_DRAGGED:
2717 // Just drop the old (intermediate) event and return the new one.
2719 case PaintEvent.PAINT:
2720 case PaintEvent.UPDATE:
2721 return coalescePaintEvents((PaintEvent) existingEvent,
2722 (PaintEvent) newEvent);
2729 * Processes the specified event. In this class, this method simply
2730 * calls one of the more specific event handlers.
2732 * @param event the event to process
2733 * @throws NullPointerException if e is null
2734 * @see #processComponentEvent(ComponentEvent)
2735 * @see #processFocusEvent(FocusEvent)
2736 * @see #processKeyEvent(KeyEvent)
2737 * @see #processMouseEvent(MouseEvent)
2738 * @see #processMouseMotionEvent(MouseEvent)
2739 * @see #processInputMethodEvent(InputMethodEvent)
2740 * @see #processHierarchyEvent(HierarchyEvent)
2741 * @see #processMouseWheelEvent(MouseWheelEvent)
2744 protected void processEvent(AWTEvent e)
2746 /* Note: the order of these if statements are
2747 important. Subclasses must be checked first. Eg. MouseEvent
2748 must be checked before ComponentEvent, since a MouseEvent
2749 object is also an instance of a ComponentEvent. */
2751 if (e instanceof FocusEvent)
2752 processFocusEvent((FocusEvent) e);
2753 else if (e instanceof PaintEvent)
2754 processPaintEvent((PaintEvent) e);
2755 else if (e instanceof MouseWheelEvent)
2756 processMouseWheelEvent((MouseWheelEvent) e);
2757 else if (e instanceof MouseEvent)
2759 if (e.id == MouseEvent.MOUSE_MOVED
2760 || e.id == MouseEvent.MOUSE_DRAGGED)
2761 processMouseMotionEvent((MouseEvent) e);
2763 processMouseEvent((MouseEvent) e);
2765 else if (e instanceof KeyEvent)
2766 processKeyEvent((KeyEvent) e);
2767 else if (e instanceof InputMethodEvent)
2768 processInputMethodEvent((InputMethodEvent) e);
2769 else if (e instanceof ComponentEvent)
2770 processComponentEvent((ComponentEvent) e);
2771 else if (e instanceof HierarchyEvent)
2773 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2774 processHierarchyEvent((HierarchyEvent) e);
2776 processHierarchyBoundsEvent((HierarchyEvent) e);
2781 * Called when a component event is dispatched and component events are
2782 * enabled. This method passes the event along to any listeners
2783 * that are attached.
2785 * @param event the <code>ComponentEvent</code> to process
2786 * @throws NullPointerException if e is null
2787 * @see ComponentListener
2788 * @see #addComponentListener(ComponentListener)
2789 * @see #enableEvents(long)
2792 protected void processComponentEvent(ComponentEvent e)
2794 if (componentListener == null)
2798 case ComponentEvent.COMPONENT_HIDDEN:
2799 componentListener.componentHidden(e);
2801 case ComponentEvent.COMPONENT_MOVED:
2802 componentListener.componentMoved(e);
2804 case ComponentEvent.COMPONENT_RESIZED:
2805 componentListener.componentResized(e);
2807 case ComponentEvent.COMPONENT_SHOWN:
2808 componentListener.componentShown(e);
2814 * Called when a focus event is dispatched and component events are
2815 * enabled. This method passes the event along to any listeners
2816 * that are attached.
2818 * @param event the <code>FocusEvent</code> to process
2819 * @throws NullPointerException if e is null
2820 * @see FocusListener
2821 * @see #addFocusListener(FocusListener)
2822 * @see #enableEvents(long)
2825 protected void processFocusEvent(FocusEvent e)
2827 if (focusListener == null)
2831 case FocusEvent.FOCUS_GAINED:
2832 focusListener.focusGained(e);
2834 case FocusEvent.FOCUS_LOST:
2835 focusListener.focusLost(e);
2841 * Called when a key event is dispatched and component events are
2842 * enabled. This method passes the event along to any listeners
2843 * that are attached.
2845 * @param event the <code>KeyEvent</code> to process
2846 * @throws NullPointerException if e is null
2848 * @see #addKeyListener(KeyListener)
2849 * @see #enableEvents(long)
2852 protected void processKeyEvent(KeyEvent e)
2854 if (keyListener == null)
2858 case KeyEvent.KEY_PRESSED:
2859 keyListener.keyPressed(e);
2861 case KeyEvent.KEY_RELEASED:
2862 keyListener.keyReleased(e);
2864 case KeyEvent.KEY_TYPED:
2865 keyListener.keyTyped(e);
2871 * Called when a regular mouse 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>MouseEvent</code> to process
2876 * @throws NullPointerException if e is null
2877 * @see MouseListener
2878 * @see #addMouseListener(MouseListener)
2879 * @see #enableEvents(long)
2882 protected void processMouseEvent(MouseEvent e)
2884 if (mouseListener == null)
2888 case MouseEvent.MOUSE_CLICKED:
2889 mouseListener.mouseClicked(e);
2891 case MouseEvent.MOUSE_ENTERED:
2892 mouseListener.mouseEntered(e);
2894 case MouseEvent.MOUSE_EXITED:
2895 mouseListener.mouseExited(e);
2897 case MouseEvent.MOUSE_PRESSED:
2898 mouseListener.mousePressed(e);
2900 case MouseEvent.MOUSE_RELEASED:
2901 mouseListener.mouseReleased(e);
2907 * Called when a mouse motion event is dispatched and component events are
2908 * enabled. This method passes the event along to any listeners
2909 * that are attached.
2911 * @param event the <code>MouseMotionEvent</code> to process
2912 * @throws NullPointerException if e is null
2913 * @see MouseMotionListener
2914 * @see #addMouseMotionListener(MouseMotionListener)
2915 * @see #enableEvents(long)
2918 protected void processMouseMotionEvent(MouseEvent e)
2920 if (mouseMotionListener == null)
2924 case MouseEvent.MOUSE_DRAGGED:
2925 mouseMotionListener.mouseDragged(e);
2927 case MouseEvent.MOUSE_MOVED:
2928 mouseMotionListener.mouseMoved(e);
2934 * Called when a mouse wheel event is dispatched and component events are
2935 * enabled. This method passes the event along to any listeners that are
2938 * @param event the <code>MouseWheelEvent</code> to process
2939 * @throws NullPointerException if e is null
2940 * @see MouseWheelListener
2941 * @see #addMouseWheelListener(MouseWheelListener)
2942 * @see #enableEvents(long)
2945 protected void processMouseWheelEvent(MouseWheelEvent e)
2947 if (mouseWheelListener != null
2948 && e.id == MouseEvent.MOUSE_WHEEL)
2949 mouseWheelListener.mouseWheelMoved(e);
2953 * Called when an input method event is dispatched and component events are
2954 * enabled. This method passes the event along to any listeners that are
2957 * @param event the <code>InputMethodEvent</code> to process
2958 * @throws NullPointerException if e is null
2959 * @see InputMethodListener
2960 * @see #addInputMethodListener(InputMethodListener)
2961 * @see #enableEvents(long)
2964 protected void processInputMethodEvent(InputMethodEvent e)
2966 if (inputMethodListener == null)
2970 case InputMethodEvent.CARET_POSITION_CHANGED:
2971 inputMethodListener.caretPositionChanged(e);
2973 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
2974 inputMethodListener.inputMethodTextChanged(e);
2980 * Called when a hierarchy change event is dispatched and component events
2981 * are enabled. This method passes the event along to any listeners that are
2984 * @param event the <code>HierarchyEvent</code> to process
2985 * @throws NullPointerException if e is null
2986 * @see HierarchyListener
2987 * @see #addHierarchyListener(HierarchyListener)
2988 * @see #enableEvents(long)
2991 protected void processHierarchyEvent(HierarchyEvent e)
2993 if (hierarchyListener == null)
2995 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2996 hierarchyListener.hierarchyChanged(e);
3000 * Called when a hierarchy bounds event is dispatched and component events
3001 * are enabled. This method passes the event along to any listeners that are
3004 * @param event the <code>HierarchyEvent</code> to process
3005 * @throws NullPointerException if e is null
3006 * @see HierarchyBoundsListener
3007 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
3008 * @see #enableEvents(long)
3011 protected void processHierarchyBoundsEvent(HierarchyEvent e)
3013 if (hierarchyBoundsListener == null)
3017 case HierarchyEvent.ANCESTOR_MOVED:
3018 hierarchyBoundsListener.ancestorMoved(e);
3020 case HierarchyEvent.ANCESTOR_RESIZED:
3021 hierarchyBoundsListener.ancestorResized(e);
3027 * AWT 1.0 event processor.
3029 * @param evt the event to handle
3030 * @return false: since the method was deprecated, the return has no meaning
3031 * @deprecated use {@link #processEvent(AWTEvent)} instead
3033 public boolean handleEvent(Event evt)
3035 // XXX Add backward compatibility handling.
3040 * AWT 1.0 mouse event.
3042 * @param evt the event to handle
3043 * @param x the x coordinate, ignored
3044 * @param y the y coordinate, ignored
3045 * @return false: since the method was deprecated, the return has no meaning
3046 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3048 public boolean mouseDown(Event evt, int x, int y)
3050 // XXX Add backward compatibility handling.
3055 * AWT 1.0 mouse event.
3057 * @param evt the event to handle
3058 * @param x the x coordinate, ignored
3059 * @param y the y coordinate, ignored
3060 * @return false: since the method was deprecated, the return has no meaning
3061 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3063 public boolean mouseDrag(Event evt, int x, int y)
3065 // XXX Add backward compatibility handling.
3070 * AWT 1.0 mouse event.
3072 * @param evt the event to handle
3073 * @param x the x coordinate, ignored
3074 * @param y the y coordinate, ignored
3075 * @return false: since the method was deprecated, the return has no meaning
3076 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3078 public boolean mouseUp(Event evt, int x, int y)
3080 // XXX Add backward compatibility handling.
3085 * AWT 1.0 mouse event.
3087 * @param evt the event to handle
3088 * @param x the x coordinate, ignored
3089 * @param y the y coordinate, ignored
3090 * @return false: since the method was deprecated, the return has no meaning
3091 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3093 public boolean mouseMove(Event evt, int x, int y)
3095 // XXX Add backward compatibility handling.
3100 * AWT 1.0 mouse event.
3102 * @param evt the event to handle
3103 * @param x the x coordinate, ignored
3104 * @param y the y coordinate, ignored
3105 * @return false: since the method was deprecated, the return has no meaning
3106 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3108 public boolean mouseEnter(Event evt, int x, int y)
3110 // XXX Add backward compatibility handling.
3115 * AWT 1.0 mouse event.
3117 * @param evt the event to handle
3118 * @param x the x coordinate, ignored
3119 * @param y the y coordinate, ignored
3120 * @return false: since the method was deprecated, the return has no meaning
3121 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3123 public boolean mouseExit(Event evt, int x, int y)
3125 // XXX Add backward compatibility handling.
3130 * AWT 1.0 key press event.
3132 * @param evt the event to handle
3133 * @param key the key pressed, ignored
3134 * @return false: since the method was deprecated, the return has no meaning
3135 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3137 public boolean keyDown(Event evt, int key)
3139 // XXX Add backward compatibility handling.
3144 * AWT 1.0 key press event.
3146 * @param evt the event to handle
3147 * @param key the key pressed, ignored
3148 * @return false: since the method was deprecated, the return has no meaning
3149 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3151 public boolean keyUp(Event evt, int key)
3153 // XXX Add backward compatibility handling.
3158 * AWT 1.0 action event processor.
3160 * @param evt the event to handle
3161 * @param what the object acted on, ignored
3162 * @return false: since the method was deprecated, the return has no meaning
3163 * @deprecated in classes which support actions, use
3164 * <code>processActionEvent(ActionEvent)</code> instead
3166 public boolean action(Event evt, Object what)
3168 // XXX Add backward compatibility handling.
3173 * Called to inform this component it has been added to a container.
3174 * A native peer - if any - is created at this time. This method is
3175 * called automatically by the AWT system and should not be called by
3178 * @see #isDisplayable()
3179 * @see #removeNotify()
3181 public void addNotify()
3184 peer = getToolkit().createComponent(this);
3185 /* Now that all the children has gotten their peers, we should
3186 have the event mask needed for this component and its
3187 lightweight subcomponents. */
3188 peer.setEventMask(eventMask);
3189 /* We do not invalidate here, but rather leave that job up to
3190 the peer. For efficiency, the peer can choose not to
3191 invalidate if it is happy with the current dimensions,
3196 * Called to inform this component is has been removed from its
3197 * container. Its native peer - if any - is destroyed at this time.
3198 * This method is called automatically by the AWT system and should
3199 * not be called by user level code.
3201 * @see #isDisplayable()
3204 public void removeNotify()
3212 * AWT 1.0 focus event.
3214 * @param evt the event to handle
3215 * @param what the Object focused, ignored
3216 * @return false: since the method was deprecated, the return has no meaning
3217 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3219 public boolean gotFocus(Event evt, Object what)
3221 // XXX Add backward compatibility handling.
3226 * AWT 1.0 focus event.
3228 * @param evt the event to handle
3229 * @param what the Object focused, ignored
3230 * @return false: since the method was deprecated, the return has no meaning
3231 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3233 public boolean lostFocus(Event evt, Object what)
3235 // XXX Add backward compatibility handling.
3240 * Tests whether or not this component is in the group that can be
3241 * traversed using the keyboard traversal mechanism (such as the TAB key).
3243 * @return true if the component is traversed via the TAB key
3244 * @see #setFocusable(boolean)
3246 * @deprecated use {@link #isFocusable()} instead
3248 public boolean isFocusTraversable()
3250 return enabled && visible && (peer == null || peer.isFocusTraversable());
3254 * Tests if this component can receive focus.
3256 * @return true if this component can receive focus
3259 public boolean isFocusable()
3265 * Specify whether this component can receive focus.
3267 * @param focusable the new focusable status
3270 public void setFocusable(boolean focusable)
3272 firePropertyChange("focusable", this.focusable, focusable);
3273 this.focusable = focusable;
3277 * Sets the focus traversal keys for a given type of focus events. Normally,
3278 * the default values should match the operating system's native choices. To
3279 * disable a given traversal, use <code>Collections.EMPTY_SET</code>. The
3280 * event dispatcher will consume PRESSED, RELEASED, and TYPED events for the
3281 * specified key, although focus can only transfer on PRESSED or RELEASED.
3283 * <p>The defauts are:
3285 * <th><td>Identifier</td><td>Meaning</td><td>Default</td></th>
3286 * <tr><td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
3287 * <td>Normal forward traversal</td>
3288 * <td>TAB on KEY_PRESSED, Ctrl-TAB on KEY_PRESSED</td></tr>
3289 * <tr><td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
3290 * <td>Normal backward traversal</td>
3291 * <td>Shift-TAB on KEY_PRESSED, Ctrl-Shift-TAB on KEY_PRESSED</td></tr>
3292 * <tr><td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
3293 * <td>Go up a traversal cycle</td><td>None</td></tr>
3296 * <p>Specifying null allows inheritance from the parent, or from the current
3297 * KeyboardFocusManager default set. If not null, the set must contain only
3298 * AWTKeyStrokes that are not already focus keys and are not KEY_TYPED
3301 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3302 * UP_CYCLE_TRAVERSAL_KEYS
3303 * @param keystrokes a set of keys, or null
3304 * @throws IllegalArgumentException if id or keystrokes is invalid
3305 * @see #getFocusTraversalKeys(int)
3306 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3307 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3308 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3311 public void setFocusTraversalKeys(int id, Set keystrokes)
3313 if (keystrokes == null)
3314 throw new IllegalArgumentException();
3320 case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
3321 sa = getFocusTraversalKeys
3322 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3323 sb = getFocusTraversalKeys
3324 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3325 name = "forwardFocusTraversalKeys";
3327 case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
3328 sa = getFocusTraversalKeys
3329 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3330 sb = getFocusTraversalKeys
3331 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3332 name = "backwardFocusTraversalKeys";
3334 case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
3335 sa = getFocusTraversalKeys
3336 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3337 sb = getFocusTraversalKeys
3338 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3339 name = "upCycleFocusTraversalKeys";
3342 throw new IllegalArgumentException();
3344 int i = keystrokes.size();
3345 Iterator iter = keystrokes.iterator();
3348 Object o = iter.next();
3349 if (! (o instanceof AWTKeyStroke)
3350 || sa.contains(o) || sb.contains(o)
3351 || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
3352 throw new IllegalArgumentException();
3354 if (focusTraversalKeys == null)
3355 focusTraversalKeys = new Set[3];
3356 keystrokes = Collections.unmodifiableSet(new HashSet(keystrokes));
3357 firePropertyChange(name, focusTraversalKeys[id], keystrokes);
3358 focusTraversalKeys[id] = keystrokes;
3362 * Returns the set of keys for a given focus traversal action, as defined
3363 * in <code>setFocusTraversalKeys</code>. If not set, this is inherited from
3364 * the parent component, which may have gotten it from the
3365 * KeyboardFocusManager.
3367 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3368 * UP_CYCLE_TRAVERSAL_KEYS
3369 * @throws IllegalArgumentException if id is invalid
3370 * @see #setFocusTraversalKeys(int, Set)
3371 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3372 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3373 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3376 public Set getFocusTraversalKeys(int id)
3378 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3379 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3380 throw new IllegalArgumentException();
3382 if (focusTraversalKeys != null)
3383 s = focusTraversalKeys[id];
3384 if (s == null && parent != null)
3385 s = parent.getFocusTraversalKeys(id);
3386 return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
3387 .getDefaultFocusTraversalKeys(id)) : s;
3391 * Tests whether the focus traversal keys for a given action are explicitly
3394 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3395 * UP_CYCLE_TRAVERSAL_KEYS
3396 * @return true if that set is explicitly specified
3397 * @throws IllegalArgumentException if id is invalid
3398 * @see #getFocusTraversalKeys(int)
3399 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3400 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3401 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3404 public boolean areFocusTraversalKeysSet(int id)
3406 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3407 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3408 throw new IllegalArgumentException();
3409 return focusTraversalKeys != null && focusTraversalKeys[id] != null;
3413 * Sets whether focus traversal keys are enabled, which consumes traversal
3414 * keys and performs the focus event automatically.
3416 * @param focusTraversalKeysEnabled the new value of the flag
3417 * @see #getFocusTraversalKeysEnabled()
3418 * @see #setFocusTraversalKeys(int, Set)
3419 * @see #getFocusTraversalKeys(int)
3422 public void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)
3424 firePropertyChange("focusTraversalKeysEnabled",
3425 this.focusTraversalKeysEnabled,
3426 focusTraversalKeysEnabled);
3427 this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
3431 * Tests whether focus traversal keys are enabled. If they are, then focus
3432 * traversal keys are consumed and focus events performed automatically,
3433 * without the component seeing the keystrokes.
3435 * @return true if focus traversal is enabled
3436 * @see #setFocusTraversalKeysEnabled(boolean)
3437 * @see #setFocusTraversalKeys(int, Set)
3438 * @see #getFocusTraversalKeys(int)
3441 public boolean getFocusTraversalKeysEnabled()
3443 return focusTraversalKeysEnabled;
3447 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3448 * event will be fired if and only if this request is successful. To be
3449 * successful, the component must be displayable, visible, and focusable,
3450 * and the top-level Window must be able to receive focus. Thus, this
3451 * request may fail, or be delayed until the window receives focus. It is
3452 * recommended that <code>requestFocusInWindow</code> be used where
3453 * possible to be more platform-independent.
3455 * @see #requestFocusInWindow()
3457 * @see #addFocusListener(FocusListener)
3458 * @see #isFocusable()
3459 * @see #isDisplayable()
3460 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3462 public void requestFocus()
3464 // If there's no peer then this component can't get the focus. We
3465 // treat it as a silent rejection of the request.
3467 peer.requestFocus();
3471 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3472 * event will be fired if and only if this request is successful. To be
3473 * successful, the component must be displayable, visible, and focusable,
3474 * and the top-level Window must be able to receive focus. Thus, this
3475 * request may fail, or be delayed until the window receives focus. It is
3476 * recommended that <code>requestFocusInWindow</code> be used where
3477 * possible to be more platform-independent.
3479 * <p>If the return value is false, the request is guaranteed to fail. If
3480 * it is true, it will likely succeed unless the action is vetoed or
3481 * something in the native windowing system intervenes. The temporary flag,
3482 * and thus this method in general, is not designed for public use; rather
3483 * it is a hook for lightweight components to notify their container in
3484 * an attempt to reduce the amount of repainting necessary.
3486 * @param temporary true if the focus request is temporary
3487 * @return true if the request has a chance of success
3488 * @see #requestFocusInWindow()
3490 * @see #addFocusListener(FocusListener)
3491 * @see #isFocusable()
3492 * @see #isDisplayable()
3493 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3496 protected boolean requestFocus(boolean temporary)
3498 // XXX Implement correctly.
3504 * Requests that this component be given focus, if it resides in the
3505 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3506 * event will be fired if and only if this request is successful. To be
3507 * successful, the component must be displayable, visible, and focusable,
3508 * and the top-level Window must be focused.
3510 * <p>If the return value is false, the request is guaranteed to fail. If
3511 * it is true, it will likely succeed unless the action is vetoed or
3512 * something in the native windowing system intervenes. The temporary flag,
3513 * and thus this method in general, is not designed for public use; rather
3514 * it is a hook for lightweight components to notify their container in
3515 * an attempt to reduce the amount of repainting necessary.
3517 * @return true if the request has a chance of success
3518 * @see #requestFocus()
3520 * @see #addFocusListener(FocusListener)
3521 * @see #isFocusable()
3522 * @see #isDisplayable()
3523 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3526 public boolean requestFocusInWindow()
3528 // XXX Implement correctly.
3534 * Requests that this component be given focus, if it resides in the
3535 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3536 * event will be fired if and only if this request is successful. To be
3537 * successful, the component must be displayable, visible, and focusable,
3538 * and the top-level Window must be focused.
3540 * <p>If the return value is false, the request is guaranteed to fail. If
3541 * it is true, it will likely succeed unless the action is vetoed or
3542 * something in the native windowing system intervenes. The temporary flag,
3543 * and thus this method in general, is not designed for public use; rather
3544 * it is a hook for lightweight components to notify their container in
3545 * an attempt to reduce the amount of repainting necessary.
3547 * @param temporary true if the focus request is temporary
3548 * @return true if the request has a chance of success
3549 * @see #requestFocus()
3551 * @see #addFocusListener(FocusListener)
3552 * @see #isFocusable()
3553 * @see #isDisplayable()
3554 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3557 protected boolean requestFocusInWindow(boolean temporary)
3559 // XXX Implement correctly.
3565 * Transfers focus to the next component in the focus traversal order, as
3566 * though this were the current focus owner.
3568 * @see #requestFocus()
3571 public void transferFocus()
3575 next = findNextFocusComponent(null);
3577 next = parent.findNextFocusComponent(this);
3578 if (next != null && next != this)
3579 next.requestFocus();
3583 * Returns the root container that owns the focus cycle where this component
3584 * resides. A focus cycle root is in two cycles, one as the ancestor, and
3585 * one as the focusable element; this call always returns the ancestor.
3587 * @return the ancestor container that owns the focus cycle
3590 public Container getFocusCycleRootAncestor()
3593 throw new Error("not implemented");
3597 * Tests if the container is the ancestor of the focus cycle that this
3598 * component belongs to.
3600 * @param c the container to test
3601 * @return true if c is the focus cycle root
3604 public boolean isFocusCycleRoot(Container c)
3606 return c == getFocusCycleRootAncestor();
3610 * AWT 1.0 focus event processor.
3612 * @deprecated use {@link #transferFocus()} instead
3614 public void nextFocus()
3620 * Transfers focus to the previous component in the focus traversal order, as
3621 * though this were the current focus owner.
3623 * @see #requestFocus()
3626 public void transferFocusBackward()
3629 throw new Error("not implemented");
3633 * Transfers focus to the focus cycle root of this component. However, if
3634 * this is a Window, the default focus owner in the window in the current
3635 * focus cycle is focused instead.
3637 * @see #requestFocus()
3638 * @see #isFocusCycleRoot()
3641 public void transferFocusUpCycle()
3644 throw new Error("not implemented");
3648 * Tests if this component is the focus owner. Use {@link #isFocusOwner()}
3651 * @return true if this component owns focus
3654 public boolean hasFocus()
3656 return isFocusOwner();
3660 * Tests if this component is the focus owner.
3662 * @return true if this component owns focus
3665 public boolean isFocusOwner()
3668 throw new Error("not implemented");
3672 * Adds the specified popup menu to this component.
3674 * @param menu the popup menu to be added
3675 * @see #remove(MenuComponent)
3678 public synchronized void add(PopupMenu popup)
3681 popups = new Vector();
3686 * Removes the specified popup menu from this component.
3688 * @param menu the popup menu to remove
3689 * @see #add(PopupMenu)
3692 public synchronized void remove(MenuComponent popup)
3695 popups.remove(popup);
3699 * Returns a debugging string representing this component. The string may
3700 * be empty but not null.
3702 * @return a string representing this component
3704 protected String paramString()
3706 StringBuffer param = new StringBuffer();
3707 String name = getName();
3709 param.append(name).append(",");
3710 param.append(width).append("x").append(height).append("+").append(x)
3711 .append("+").append(y);
3713 param.append(",invalid");
3715 param.append(",invisible");
3717 param.append(",disabled");
3719 param.append(",translucent");
3720 if (isDoubleBuffered())
3721 param.append(",doublebuffered");
3722 return param.toString();
3726 * Returns a string representation of this component. This is implemented
3727 * as <code>getClass().getName() + '[' + paramString() + ']'</code>.
3729 * @return a string representation of this component
3731 public String toString()
3733 return getClass().getName() + '[' + paramString() + ']';
3737 * Prints a listing of this component to <code>System.out</code>.
3739 * @see #list(PrintStream)
3743 list(System.out, 0);
3747 * Prints a listing of this component to the specified print stream.
3749 * @param stream the <code>PrintStream</code> to print to
3751 public void list(PrintStream out)
3757 * Prints a listing of this component to the specified print stream,
3758 * starting at the specified indentation point.
3760 * @param stream the <code>PrintStream</code> to print to
3761 * @param indent the indentation point
3763 public void list(PrintStream out, int indent)
3765 for (int i = 0; i < indent; ++i)
3767 out.println(toString());
3771 * Prints a listing of this component to the specified print writer.
3773 * @param writer the <code>PrintWrinter</code> to print to
3776 public void list(PrintWriter out)
3782 * Prints a listing of this component to the specified print writer,
3783 * starting at the specified indentation point.
3785 * @param writer the <code>PrintWriter</code> to print to
3786 * @param indent the indentation point
3789 public void list(PrintWriter out, int indent)
3791 for (int i = 0; i < indent; ++i)
3793 out.println(toString());
3797 * Adds the specified property listener to this component. This is harmless
3798 * if the listener is null, but if the listener has already been registered,
3799 * it will now be registered twice. The property listener ignores inherited
3800 * properties. Recognized properties include:<br>
3802 * <li>the font (<code>"font"</code>)</li>
3803 * <li>the background color (<code>"background"</code>)</li>
3804 * <li>the foreground color (<code>"foreground"</code>)</li>
3805 * <li>the focusability (<code>"focusable"</code>)</li>
3806 * <li>the focus key traversal enabled state
3807 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3808 * <li>the set of forward traversal keys
3809 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3810 * <li>the set of backward traversal keys
3811 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3812 * <li>the set of up-cycle traversal keys
3813 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3816 * @param listener the new listener to add
3817 * @see #removePropertyChangeListener(PropertyChangeListener)
3818 * @see #getPropertyChangeListeners()
3819 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3822 public void addPropertyChangeListener(PropertyChangeListener listener)
3824 if (changeSupport == null)
3825 changeSupport = new PropertyChangeSupport(this);
3826 changeSupport.addPropertyChangeListener(listener);
3830 * Removes the specified property listener from the component. This is
3831 * harmless if the listener was not previously registered.
3833 * @param listener the listener to remove
3834 * @see #addPropertyChangeListener(PropertyChangeListener)
3835 * @see #getPropertyChangeListeners()
3836 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3839 public void removePropertyChangeListener(PropertyChangeListener listener)
3841 if (changeSupport != null)
3842 changeSupport.removePropertyChangeListener(listener);
3846 * Returns an array of all specified listeners registered on this component.
3848 * @return an array of listeners
3849 * @see #addPropertyChangeListener(PropertyChangeListener)
3850 * @see #removePropertyChangeListener(PropertyChangeListener)
3851 * @see #getPropertyChangeListeners(String)
3854 public PropertyChangeListener[] getPropertyChangeListeners()
3856 return changeSupport == null ? new PropertyChangeListener[0]
3857 : changeSupport.getPropertyChangeListeners();
3861 * Adds the specified property listener to this component. This is harmless
3862 * if the listener is null, but if the listener has already been registered,
3863 * it will now be registered twice. The property listener ignores inherited
3864 * properties. The listener is keyed to a single property. Recognized
3865 * properties include:<br>
3867 * <li>the font (<code>"font"</code>)</li>
3868 * <li>the background color (<code>"background"</code>)</li>
3869 * <li>the foreground color (<code>"foreground"</code>)</li>
3870 * <li>the focusability (<code>"focusable"</code>)</li>
3871 * <li>the focus key traversal enabled state
3872 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3873 * <li>the set of forward traversal keys
3874 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3875 p * <li>the set of backward traversal keys
3876 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3877 * <li>the set of up-cycle traversal keys
3878 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3881 * @param propertyName the property name to filter on
3882 * @param listener the new listener to add
3883 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3884 * @see #getPropertyChangeListeners(String)
3885 * @see #addPropertyChangeListener(PropertyChangeListener)
3888 public void addPropertyChangeListener(String propertyName,
3889 PropertyChangeListener listener)
3891 if (changeSupport == null)
3892 changeSupport = new PropertyChangeSupport(this);
3893 changeSupport.addPropertyChangeListener(propertyName, listener);
3897 * Removes the specified property listener on a particular property from
3898 * the component. This is harmless if the listener was not previously
3901 * @param propertyName the property name to filter on
3902 * @param listener the listener to remove
3903 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3904 * @see #getPropertyChangeListeners(String)
3905 * @see #removePropertyChangeListener(PropertyChangeListener)
3908 public void removePropertyChangeListener(String propertyName,
3909 PropertyChangeListener listener)
3911 if (changeSupport != null)
3912 changeSupport.removePropertyChangeListener(propertyName, listener);
3916 * Returns an array of all specified listeners on the named property that
3917 * are registered on this component.
3919 * @return an array of listeners
3920 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3921 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3922 * @see #getPropertyChangeListeners()
3925 public PropertyChangeListener[] getPropertyChangeListeners(String property)
3927 return changeSupport == null ? new PropertyChangeListener[0]
3928 : changeSupport.getPropertyChangeListeners(property);
3932 * Report a change in a bound property to any registered property listeners.
3934 * @param propertyName the property that changed
3935 * @param oldValue the old property value
3936 * @param newValue the new property value
3938 protected void firePropertyChange(String propertyName, Object oldValue,
3941 if (changeSupport != null)
3942 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3946 * Report a change in a bound property to any registered property listeners.
3948 * @param propertyName the property that changed
3949 * @param oldValue the old property value
3950 * @param newValue the new property value
3952 protected void firePropertyChange(String propertyName, boolean oldValue,
3955 if (changeSupport != null)
3956 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3960 * Report a change in a bound property to any registered property listeners.
3962 * @param propertyName the property that changed
3963 * @param oldValue the old property value
3964 * @param newValue the new property value
3966 protected void firePropertyChange(String propertyName, int oldValue,
3969 if (changeSupport != null)
3970 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3974 * Sets the text layout orientation of this component. New components default
3975 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects only
3976 * the current component, while
3977 * {@link #applyComponentOrientation(ComponentOrientation)} affects the
3980 * @param o the new orientation
3981 * @throws NullPointerException if o is null
3982 * @see #getComponentOrientation()
3984 public void setComponentOrientation(ComponentOrientation o)
3987 throw new NullPointerException();
3992 * Determines the text layout orientation used by this component.
3994 * @return the component orientation
3995 * @see #setComponentOrientation(ComponentOrientation)
3997 public ComponentOrientation getComponentOrientation()
4003 * Sets the text layout orientation of this component. New components default
4004 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects the
4005 * entire hierarchy, while
4006 * {@link #setComponentOrientation(ComponentOrientation)} affects only the
4007 * current component.
4009 * @param o the new orientation
4010 * @throws NullPointerException if o is null
4011 * @see #getComponentOrientation()
4014 public void applyComponentOrientation(ComponentOrientation o)
4016 setComponentOrientation(o);
4020 * Returns the accessibility framework context of this class. Component is
4021 * not accessible, so the default implementation returns null. Subclasses
4022 * must override this behavior, and return an appropriate subclass of
4023 * {@link AccessibleAWTComponent}.
4025 * @return the accessibility context
4027 public AccessibleContext getAccessibleContext()
4033 // Helper methods; some are package visible for use by subclasses.
4036 * Subclasses should override this to return unique component names like
4039 * @return the generated name for this component
4041 String generateName()
4043 // Component is abstract.
4048 * Sets the peer for this component.
4050 * @param peer the new peer
4052 final void setPeer(ComponentPeer peer)
4058 * Implementation method that allows classes such as Canvas and Window to
4059 * override the graphics configuration without violating the published API.
4061 * @return the graphics configuration
4063 GraphicsConfiguration getGraphicsConfigurationImpl()
4067 GraphicsConfiguration config = peer.getGraphicsConfiguration();
4073 return parent.getGraphicsConfiguration();
4079 * Implementation of dispatchEvent. Allows trusted package classes to
4080 * dispatch additional events first.
4082 * @param e the event to dispatch
4084 void dispatchEventImpl(AWTEvent e)
4086 if (eventTypeEnabled (e.id))
4091 * Tells whether or not an event type is enabled.
4093 boolean eventTypeEnabled (int type)
4095 if (type > AWTEvent.RESERVED_ID_MAX)
4100 case ComponentEvent.COMPONENT_HIDDEN:
4101 case ComponentEvent.COMPONENT_MOVED:
4102 case ComponentEvent.COMPONENT_RESIZED:
4103 case ComponentEvent.COMPONENT_SHOWN:
4104 return (componentListener != null
4105 || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0);
4107 case KeyEvent.KEY_PRESSED:
4108 case KeyEvent.KEY_RELEASED:
4109 case KeyEvent.KEY_TYPED:
4110 return (keyListener != null
4111 || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0);
4113 case MouseEvent.MOUSE_CLICKED:
4114 case MouseEvent.MOUSE_ENTERED:
4115 case MouseEvent.MOUSE_EXITED:
4116 case MouseEvent.MOUSE_PRESSED:
4117 case MouseEvent.MOUSE_RELEASED:
4118 return (mouseListener != null
4119 || mouseMotionListener != null
4120 || (eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0);
4122 case FocusEvent.FOCUS_GAINED:
4123 case FocusEvent.FOCUS_LOST:
4124 return (focusListener != null
4125 || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0);
4127 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
4128 case InputMethodEvent.CARET_POSITION_CHANGED:
4129 return (inputMethodListener != null
4130 || (eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0);
4132 case PaintEvent.PAINT:
4133 case PaintEvent.UPDATE:
4134 return (eventMask & AWTEvent.PAINT_EVENT_MASK) != 0;
4142 * Coalesce paint events. Current heuristic is: Merge if the union of
4143 * areas is less than twice that of the sum of the areas. The X server
4144 * tend to create a lot of paint events that are adjacent but not
4149 * | +-----+ ...will be merged
4155 * +---------------+--+
4156 * | | | ...will not be merged
4157 * +---------------+ |
4166 * @param queuedEvent the first paint event
4167 * @param newEvent the second paint event
4168 * @return the combined paint event, or null
4170 private PaintEvent coalescePaintEvents(PaintEvent queuedEvent,
4171 PaintEvent newEvent)
4173 Rectangle r1 = queuedEvent.getUpdateRect();
4174 Rectangle r2 = newEvent.getUpdateRect();
4175 Rectangle union = r1.union(r2);
4177 int r1a = r1.width * r1.height;
4178 int r2a = r2.width * r2.height;
4179 int ua = union.width * union.height;
4181 if (ua > (r1a+r2a)*2)
4183 /* The 2 factor should maybe be reconsidered. Perhaps 3/2
4186 newEvent.setUpdateRect(union);
4191 * Does the work for a paint event.
4193 * @param event the event to process
4195 private void processPaintEvent(PaintEvent event)
4197 // Can't do graphics without peer
4201 Graphics gfx = getGraphics();
4204 Shape clip = event.getUpdateRect();
4209 case PaintEvent.PAINT:
4212 case PaintEvent.UPDATE:
4216 throw new IllegalArgumentException("unknown paint event");
4226 * This method is used to implement transferFocus(). CHILD is the child
4227 * making the request. This is overridden by Container; when called for an
4228 * ordinary component there is no child and so we always return null.
4230 * @param child the component making the request
4231 * @return the next component to focus on
4233 Component findNextFocusComponent(Component child)
4239 * Deserializes this component. This regenerates all serializable listeners
4240 * which were registered originally.
4242 * @param s the stream to read from
4243 * @throws ClassNotFoundException if deserialization fails
4244 * @throws IOException if the stream fails
4246 private void readObject(ObjectInputStream s)
4247 throws ClassNotFoundException, IOException
4249 s.defaultReadObject();
4250 String key = (String) s.readObject();
4253 Object listener = s.readObject();
4254 if ("componentL".equals(key))
4255 addComponentListener((ComponentListener) listener);
4256 else if ("focusL".equals(key))
4257 addFocusListener((FocusListener) listener);
4258 else if ("keyL".equals(key))
4259 addKeyListener((KeyListener) listener);
4260 else if ("mouseL".equals(key))
4261 addMouseListener((MouseListener) listener);
4262 else if ("mouseMotionL".equals(key))
4263 addMouseMotionListener((MouseMotionListener) listener);
4264 else if ("inputMethodL".equals(key))
4265 addInputMethodListener((InputMethodListener) listener);
4266 else if ("hierarchyL".equals(key))
4267 addHierarchyListener((HierarchyListener) listener);
4268 else if ("hierarchyBoundsL".equals(key))
4269 addHierarchyBoundsListener((HierarchyBoundsListener) listener);
4270 else if ("mouseWheelL".equals(key))
4271 addMouseWheelListener((MouseWheelListener) listener);
4272 key = (String) s.readObject();
4277 * Serializes this component. This ignores all listeners which do not
4278 * implement Serializable, but includes those that do.
4280 * @param s the stream to write to
4281 * @throws IOException if the stream fails
4283 private void writeObject(ObjectOutputStream s) throws IOException
4285 s.defaultWriteObject();
4286 AWTEventMulticaster.save(s, "componentL", componentListener);
4287 AWTEventMulticaster.save(s, "focusL", focusListener);
4288 AWTEventMulticaster.save(s, "keyL", keyListener);
4289 AWTEventMulticaster.save(s, "mouseL", mouseListener);
4290 AWTEventMulticaster.save(s, "mouseMotionL", mouseMotionListener);
4291 AWTEventMulticaster.save(s, "inputMethodL", inputMethodListener);
4292 AWTEventMulticaster.save(s, "hierarchyL", hierarchyListener);
4293 AWTEventMulticaster.save(s, "hierarchyBoundsL", hierarchyBoundsListener);
4294 AWTEventMulticaster.save(s, "mouseWheelL", mouseWheelListener);
4295 s.writeObject(null);
4302 * This class provides accessibility support for subclasses of container.
4304 * @author Eric Blake <ebb9@email.byu.edu>
4306 * @status updated to 1.4
4308 protected abstract class AccessibleAWTComponent extends AccessibleContext
4309 implements Serializable, AccessibleComponent
4312 * Compatible with JDK 1.3+.
4314 private static final long serialVersionUID = 642321655757800191L;
4317 * Converts show/hide events to PropertyChange events, and is registered
4318 * as a component listener on this component.
4320 * @serial the component handler
4322 protected ComponentListener accessibleAWTComponentHandler
4323 = new AccessibleAWTComponentHandler();
4326 * Converts focus events to PropertyChange events, and is registered
4327 * as a focus listener on this component.
4329 * @serial the focus handler
4331 protected FocusListener accessibleAWTFocusHandler
4332 = new AccessibleAWTFocusHandler();
4335 * The default constructor.
4337 protected AccessibleAWTComponent()
4339 Component.this.addComponentListener(accessibleAWTComponentHandler);
4340 Component.this.addFocusListener(accessibleAWTFocusHandler);
4344 * Adds a global property change listener to the accessible component.
4346 * @param l the listener to add
4347 * @see #ACCESSIBLE_NAME_PROPERTY
4348 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
4349 * @see #ACCESSIBLE_STATE_PROPERTY
4350 * @see #ACCESSIBLE_VALUE_PROPERTY
4351 * @see #ACCESSIBLE_SELECTION_PROPERTY
4352 * @see #ACCESSIBLE_TEXT_PROPERTY
4353 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
4355 public void addPropertyChangeListener(PropertyChangeListener l)
4357 Component.this.addPropertyChangeListener(l);
4358 super.addPropertyChangeListener(l);
4362 * Removes a global property change listener from this accessible
4365 * @param l the listener to remove
4367 public void removePropertyChangeListener(PropertyChangeListener l)
4369 Component.this.removePropertyChangeListener(l);
4370 super.removePropertyChangeListener(l);
4374 * Returns the accessible name of this component. It is almost always
4375 * wrong to return getName(), since it is not localized. In fact, for
4376 * things like buttons, this should be the text of the button, not the
4377 * name of the object. The tooltip text might also be appropriate.
4380 * @see #setAccessibleName(String)
4382 public String getAccessibleName()
4384 return accessibleName == null ? getName() : accessibleName;
4388 * Returns a brief description of this accessible context. This should
4391 * @return a description of this component
4392 * @see #setAccessibleDescription(String)
4394 public String getAccessibleDescription()
4396 return accessibleDescription;
4400 * Returns the role of this component.
4402 * @return the accessible role
4404 public AccessibleRole getAccessibleRole()
4406 return AccessibleRole.AWT_COMPONENT;
4410 * Returns a state set describing this component's state.
4412 * @return a new state set
4413 * @see AccessibleState
4415 public AccessibleStateSet getAccessibleStateSet()
4417 AccessibleStateSet s = new AccessibleStateSet();
4418 if (Component.this.isEnabled())
4419 s.add(AccessibleState.ENABLED);
4421 s.add(AccessibleState.FOCUSABLE);
4423 s.add(AccessibleState.FOCUSED);
4425 s.add(AccessibleState.OPAQUE);
4426 if (Component.this.isShowing())
4427 s.add(AccessibleState.SHOWING);
4428 if (Component.this.isVisible())
4429 s.add(AccessibleState.VISIBLE);
4434 * Returns the parent of this component, if it is accessible.
4436 * @return the accessible parent
4438 public Accessible getAccessibleParent()
4440 if (accessibleParent == null)
4442 Container parent = getParent();
4443 accessibleParent = parent instanceof Accessible
4444 ? (Accessible) parent : null;
4446 return accessibleParent;
4450 * Returns the index of this component in its accessible parent.
4452 * @return the index, or -1 if the parent is not accessible
4453 * @see #getAccessibleParent()
4455 public int getAccessibleIndexInParent()
4457 if (getAccessibleParent() == null)
4459 AccessibleContext context
4460 = ((Component) accessibleParent).getAccessibleContext();
4461 if (context == null)
4463 for (int i = context.getAccessibleChildrenCount(); --i >= 0; )
4464 if (context.getAccessibleChild(i) == Component.this)
4470 * Returns the number of children of this component which implement
4471 * Accessible. Subclasses must override this if they can have children.
4473 * @return the number of accessible children, default 0
4475 public int getAccessibleChildrenCount()
4481 * Returns the ith accessible child. Subclasses must override this if
4482 * they can have children.
4484 * @return the ith accessible child, or null
4485 * @see #getAccessibleChildrenCount()
4487 public Accessible getAccessibleChild(int i)
4493 * Returns the locale of this component.
4495 * @return the locale
4496 * @throws IllegalComponentStateException if the locale is unknown
4498 public Locale getLocale()
4500 return Component.this.getLocale();
4504 * Returns this, since it is an accessible component.
4506 * @return the accessible component
4508 public AccessibleComponent getAccessibleComponent()
4514 * Gets the background color.
4516 * @return the background color
4517 * @see #setBackground(Color)
4519 public Color getBackground()
4521 return Component.this.getBackground();
4525 * Sets the background color.
4527 * @param c the background color
4528 * @see #getBackground()
4531 public void setBackground(Color c)
4533 Component.this.setBackground(c);
4537 * Gets the foreground color.
4539 * @return the foreground color
4540 * @see #setForeground(Color)
4542 public Color getForeground()
4544 return Component.this.getForeground();
4548 * Sets the foreground color.
4550 * @param c the foreground color
4551 * @see #getForeground()
4553 public void setForeground(Color c)
4555 Component.this.setForeground(c);
4561 * @return the cursor
4562 * @see #setCursor(Cursor)
4564 public Cursor getCursor()
4566 return Component.this.getCursor();
4572 * @param cursor the cursor
4575 public void setCursor(Cursor cursor)
4577 Component.this.setCursor(cursor);
4584 * @see #setFont(Font)
4586 public Font getFont()
4588 return Component.this.getFont();
4597 public void setFont(Font f)
4599 Component.this.setFont(f);
4603 * Gets the font metrics for a font.
4605 * @param f the font to look up
4606 * @return its metrics
4607 * @throws NullPointerException if f is null
4610 public FontMetrics getFontMetrics(Font f)
4612 return Component.this.getFontMetrics(f);
4616 * Tests if the component is enabled.
4618 * @return true if the component is enabled
4619 * @see #setEnabled(boolean)
4620 * @see #getAccessibleStateSet()
4621 * @see AccessibleState#ENABLED
4623 public boolean isEnabled()
4625 return Component.this.isEnabled();
4629 * Set whether the component is enabled.
4631 * @param b the new enabled status
4634 public void setEnabled(boolean b)
4636 Component.this.setEnabled(b);
4640 * Test whether the component is visible (not necesarily showing).
4642 * @return true if it is visible
4643 * @see #setVisible(boolean)
4644 * @see #getAccessibleStateSet()
4645 * @see AccessibleState#VISIBLE
4647 public boolean isVisible()
4649 return Component.this.isVisible();
4653 * Sets the visibility of this component.
4655 * @param b the desired visibility
4658 public void setVisible(boolean b)
4660 Component.this.setVisible(b);
4664 * Tests if the component is showing.
4666 * @return true if this is showing
4668 public boolean isShowing()
4670 return Component.this.isShowing();
4674 * Tests if the point is contained in this component.
4676 * @param p the point to check
4677 * @return true if it is contained
4678 * @throws NullPointerException if p is null
4680 public boolean contains(Point p)
4682 return Component.this.contains(p.x, p.y);
4686 * Returns the location of this object on the screen, or null if it is
4689 * @return the location relative to screen coordinates, if showing
4691 * @see #getLocation()
4693 public Point getLocationOnScreen()
4695 return Component.this.isShowing() ? Component.this.getLocationOnScreen()
4700 * Returns the location of this object relative to its parent's coordinate
4701 * system, or null if it is not showing.
4703 * @return the location
4705 * @see #getLocationOnScreen()
4707 public Point getLocation()
4709 return Component.this.isShowing() ? Component.this.getLocation() : null;
4713 * Sets the location of this relative to its parent's coordinate system.
4715 * @param p the location
4716 * @throws NullPointerException if p is null
4717 * @see #getLocation()
4719 public void setLocation(Point p)
4721 Component.this.setLocation(p.x, p.y);
4725 * Gets the bounds of this component, or null if it is not on screen.
4727 * @return the bounds
4728 * @see #contains(Point)
4729 * @see #setBounds(Rectangle)
4731 public Rectangle getBounds()
4733 return Component.this.isShowing() ? Component.this.getBounds() : null;
4737 * Sets the bounds of this component.
4739 * @param r the bounds
4740 * @throws NullPointerException if r is null
4743 public void setBounds(Rectangle r)
4745 Component.this.setBounds(r.x, r.y, r.width, r.height);
4749 * Gets the size of this component, or null if it is not showing.
4752 * @see #setSize(Dimension)
4754 public Dimension getSize()
4756 return Component.this.isShowing() ? Component.this.getSize() : null;
4760 * Sets the size of this component.
4763 * @throws NullPointerException if d is null
4766 public void setSize(Dimension d)
4768 Component.this.setSize(d.width, d.height);
4772 * Returns the Accessible child at a point relative to the coordinate
4773 * system of this component, if one exists, or null. Since components
4774 * have no children, subclasses must override this to get anything besides
4777 * @param p the point to check
4778 * @return the accessible child at that point
4779 * @throws NullPointerException if p is null
4781 public Accessible getAccessibleAt(Point p)
4787 * Tests whether this component can accept focus.
4789 * @return true if this is focus traversable
4790 * @see #getAccessibleStateSet()
4791 * @see AccessibleState#FOCUSABLE
4792 * @see AccessibleState#FOCUSED
4794 public boolean isFocusTraversable()
4796 return Component.this.isFocusTraversable();
4800 * Requests focus for this component.
4802 * @see #isFocusTraversable()
4804 public void requestFocus()
4806 Component.this.requestFocus();
4810 * Adds a focus listener.
4812 * @param l the listener to add
4814 public void addFocusListener(FocusListener l)
4816 Component.this.addFocusListener(l);
4820 * Removes a focus listener.
4822 * @param l the listener to remove
4824 public void removeFocusListener(FocusListener l)
4826 Component.this.removeFocusListener(l);
4830 * Converts component changes into property changes.
4832 * @author Eric Blake <ebb9@email.byu.edu>
4834 * @status updated to 1.4
4836 protected class AccessibleAWTComponentHandler implements ComponentListener
4839 * Default constructor.
4841 protected AccessibleAWTComponentHandler()
4846 * Convert a component hidden to a property change.
4848 * @param e the event to convert
4850 public void componentHidden(ComponentEvent e)
4852 AccessibleAWTComponent.this.firePropertyChange
4853 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.VISIBLE, null);
4857 * Convert a component shown to a property change.
4859 * @param e the event to convert
4861 public void componentShown(ComponentEvent e)
4863 AccessibleAWTComponent.this.firePropertyChange
4864 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.VISIBLE);
4868 * Moving a component does not affect properties.
4872 public void componentMoved(ComponentEvent e)
4877 * Resizing a component does not affect properties.
4881 public void componentResized(ComponentEvent e)
4884 } // class AccessibleAWTComponentHandler
4887 * Converts focus changes into property changes.
4889 * @author Eric Blake <ebb9@email.byu.edu>
4891 * @status updated to 1.4
4893 protected class AccessibleAWTFocusHandler implements FocusListener
4896 * Default constructor.
4898 protected AccessibleAWTFocusHandler()
4903 * Convert a focus gained to a property change.
4905 * @param e the event to convert
4907 public void focusGained(FocusEvent e)
4909 AccessibleAWTComponent.this.firePropertyChange
4910 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED);
4914 * Convert a focus lost to a property change.
4916 * @param e the event to convert
4918 public void focusLost(FocusEvent e)
4920 AccessibleAWTComponent.this.firePropertyChange
4921 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null);
4923 } // class AccessibleAWTComponentHandler
4924 } // class AccessibleAWTComponent
4927 * This class provides support for blitting offscreen surfaces.
4929 * @author Eric Blake <ebb9@email.byu.edu>
4931 * @XXX Shell class, to allow compilation. This needs documentation and
4932 * correct implementation.
4934 protected class BltBufferStrategy extends BufferStrategy
4936 protected BufferCapabilities caps;
4937 protected VolatileImage[] backBuffers;
4938 protected boolean validatedContents;
4939 protected int width;
4940 protected int height;
4941 protected BltBufferStrategy(int num, BufferCapabilities caps)
4944 createBackBuffers(num);
4946 protected void createBackBuffers(int num)
4948 backBuffers = new VolatileImage[num];
4950 public BufferCapabilities getCapabilities()
4954 public Graphics getDrawGraphics() { return null; }
4955 public void show() {}
4956 protected void revalidate() {}
4957 public boolean contentsLost() { return false; }
4958 public boolean contentsRestored() { return false; }
4959 } // class BltBufferStrategy
4962 * This class provides support for flipping component buffers. It is only
4963 * designed for use by Canvas and Window.
4965 * @author Eric Blake <ebb9@email.byu.edu>
4967 * @XXX Shell class, to allow compilation. This needs documentation and
4968 * correct implementation.
4970 protected class FlipBufferStrategy extends BufferStrategy
4972 protected int numBuffers;
4973 protected BufferCapabilities caps;
4974 protected Image drawBuffer;
4975 protected VolatileImage drawVBuffer;
4976 protected boolean validatedContents;
4977 protected FlipBufferStrategy(int num, BufferCapabilities caps)
4981 createBuffers(num, caps);
4983 protected void createBuffers(int num, BufferCapabilities caps)
4984 throws AWTException {}
4985 protected Image getBackBuffer()
4989 protected void flip(BufferCapabilities.FlipContents flipAction) {}
4990 protected void destroyBuffers() {}
4991 public BufferCapabilities getCapabilities()
4995 public Graphics getDrawGraphics() { return null; }
4996 protected void revalidate() {}
4997 public boolean contentsLost() { return false; }
4998 public boolean contentsRestored() { return false; }
4999 public void show() {}
5000 } // class FlipBufferStrategy
5001 } // class Component