FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / awt / Component.java
blob88f91810dc0b0fc8e8748a4972d78a6331d09492
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)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
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;
82 import java.util.Set;
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;
91 /**
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.
108 * <p><pre>
109 * import java.awt.*;
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");
125 * MyApp()
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>
139 * @since 1.0
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
157 * other components.
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
167 * other components.
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
177 * other components.
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
186 * other components.
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
195 * other components.
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
205 * as a lock object.
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
217 int x;
220 * The y position of the component in the parent's coordinate system.
222 * @see #getLocation()
223 * @serial the y position
225 int y;
228 * The component width.
230 * @see #getSize()
231 * @serial the width
233 int width;
236 * The component height.
238 * @see #getSize()
239 * @serial the height
241 int height;
244 * The foreground color for the component. This may be null.
246 * @see #getForeground()
247 * @see #setForeground(Color)
248 * @serial the foreground color
250 Color foreground;
253 * The background color for the component. This may be null.
255 * @see #getBackground()
256 * @see #setBackground(Color)
257 * @serial the background color
259 Color background;
262 * The default font used in the component. This may be null.
264 * @see #getFont()
265 * @see #setFont(Font)
266 * @serial the font
268 Font font;
271 * The font in use by the peer, or null if there is no peer.
273 * @serial the peer's font
275 Font peerFont;
278 * The cursor displayed when the pointer is over this component. This may
279 * be null.
281 * @see #getCursor()
282 * @see #setCursor(Cursor)
284 Cursor cursor;
287 * The locale for the component.
289 * @see #getLocale()
290 * @see #setLocale(Locale)
292 Locale locale;
295 * True if the object should ignore repaint events (usually because it is
296 * not showing).
298 * @see #getIgnoreRepaint()
299 * @see #setIgnoreRepaint(boolean)
300 * @serial true to ignore repaints
301 * @since 1.4
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.
309 * @see #isVisible()
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.
319 * @see #isEnabled()
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.
329 * @see #isValid()
330 * @see #validate()
331 * @see #invalidate()
332 * @serial true if layout is valid
334 boolean valid;
337 * The DropTarget for drag-and-drop operations.
339 * @see #getDropTarget()
340 * @see #setDropTarget(DropTarget)
341 * @serial the drop target, or null
342 * @since 1.2
344 DropTarget dropTarget;
347 * The list of popup menus for this component.
349 * @see #add(PopupMenu)
350 * @serial the list of popups
352 Vector popups;
355 * The component's name. May be null, in which case a default name is
356 * generated on the first use.
358 * @see #getName()
359 * @see #setName(String)
360 * @serial the name
362 String name;
365 * True once the user has set the name. Note that the user may set the name
366 * to null.
368 * @see #name
369 * @see #getName()
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)
380 * @since 1.4
382 boolean focusable = true;
385 * Tracks whether this component uses default focus traversal, or has a
386 * different policy.
388 * @see #isFocusTraversableOverridden()
389 * @since 1.4
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)
400 * @since 1.4
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)
411 * @since 1.4
413 boolean focusTraversalKeysEnabled = true;
416 * Cached information on the minimum size. Should have been transient.
418 * @serial ignore
420 Dimension minSize;
423 * Cached information on the preferred size. Should have been transient.
425 * @serial ignore
427 Dimension prefSize;
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)
444 * @see AWTEvent
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
456 * @since 1.2
458 PropertyChangeSupport changeSupport;
461 * True if the component has been packed (layed out).
463 * @serial true if this is packed
465 boolean isPacked;
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
478 * by subclasses.
480 * @see #getAccessibleContext()
481 * @serial the accessibility context
482 * @since 1.2
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.
508 * @since 1.4
510 transient MouseWheelListener mouseWheelListener;
513 * Input method listener chain.
515 * @since 1.2
517 transient InputMethodListener inputMethodListener;
520 * Hierarcy listener chain.
522 * @since 1.3
524 transient HierarchyListener hierarchyListener;
527 * Hierarcy bounds listener chain.
529 * @since 1.3
531 transient HierarchyBoundsListener hierarchyBoundsListener;
533 // Anything else is non-serializable, and should be declared "transient".
535 /** The parent. */
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.
547 * @since 1.4
549 transient GraphicsConfiguration graphicsConfig;
552 * The buffer strategy for repainting.
554 * @since 1.4
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)
575 * @since 1.1
577 public String getName()
579 if (name == null && ! nameExplicitlySet)
580 name = generateName();
581 return name;
585 * Sets the name of this component to the specified name.
587 * @param name the new name of this component
588 * @see #getName()
589 * @since 1.1
591 public void setName(String name)
593 nameExplicitlySet = true;
594 this.name = name;
598 * Returns the parent of this component.
600 * @return the parent of this component
602 public Container getParent()
604 return parent;
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()
618 return peer;
622 * Set the associated drag-and-drop target, which receives events when this
623 * is enabled.
625 * @param dt the new drop target
626 * @see #isEnabled()
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()
640 return dropTarget;
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
648 * @since 1.3
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()
663 return treeLock;
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()
674 if (peer != null)
676 Toolkit tk = peer.getToolkit();
677 if (tk != null)
678 return tk;
680 if (parent != null)
681 return parent.getToolkit();
682 return Toolkit.getDefaultToolkit();
686 * Tests whether or not this component is valid. A invalid component needs
687 * to have its layout redone.
689 * @return true if this component is valid
690 * @see #validate()
691 * @see #invalidate()
693 public boolean isValid()
695 return valid;
699 * Tests if the component is displayable. It must be connected to a native
700 * screen resource, and all its ancestors must be displayable. A containment
701 * hierarchy is made displayable when a window is packed or made visible.
703 * @return true if the component is displayable
704 * @see Container#add(Component)
705 * @see Container#remove(Component)
706 * @see Window#pack()
707 * @see Window#show()
708 * @see Window#dispose()
709 * @since 1.2
711 public boolean isDisplayable()
713 if (parent != null)
714 return parent.isDisplayable();
715 return false;
719 * Tests whether or not this component is visible. Except for top-level
720 * frames, components are initially visible.
722 * @return true if the component is visible
723 * @see #setVisible(boolean)
725 public boolean isVisible()
727 return visible;
731 * Tests whether or not this component is actually being shown on
732 * the screen. This will be true if and only if it this component is
733 * visible and its parent components are all visible.
735 * @return true if the component is showing on the screen
736 * @see #setVisible(boolean)
738 public boolean isShowing()
740 if (! visible || peer == null)
741 return false;
743 return parent == null ? true : parent.isShowing();
747 * Tests whether or not this component is enabled. Components are enabled
748 * by default, and must be enabled to receive user input or generate events.
750 * @return true if the component is enabled
751 * @see #setEnabled(boolean)
753 public boolean isEnabled()
755 return enabled;
759 * Enables or disables this component. The component must be enabled to
760 * receive events (except that lightweight components always receive mouse
761 * events).
763 * @param enabled true to enable this component
764 * @see #isEnabled()
765 * @see #isLightweight()
766 * @since 1.1
768 public void setEnabled(boolean b)
770 this.enabled = b;
771 if (peer != null)
772 peer.setEnabled(b);
776 * Enables this component.
778 * @deprecated use {@link #setEnabled(boolean)} instead
780 public void enable()
782 setEnabled(true);
786 * Enables or disables this component.
788 * @param enabled true to enable this component
789 * @deprecated use {@link #setEnabled(boolean)} instead
791 public void enable(boolean b)
793 setEnabled(b);
797 * Disables this component.
799 * @deprecated use {@link #setEnabled(boolean)} instead
801 public void disable()
803 setEnabled(false);
807 * Checks if this image is painted to an offscreen image buffer that is
808 * later copied to screen (double buffering reduces flicker). This version
809 * returns false, so subclasses must override it if they provide double
810 * buffering.
812 * @return true if this is double buffered; defaults to false
814 public boolean isDoubleBuffered()
816 return false;
820 * Enables or disables input method support for this component. By default,
821 * components have this enabled. Input methods are given the opportunity
822 * to process key events before this component and its listeners.
824 * @param enable true to enable input method processing
825 * @see #processKeyEvent(KeyEvent)
826 * @since 1.2
828 public void enableInputMethods(boolean enable)
830 // XXX Implement.
831 throw new Error("not implemented");
835 * Makes this component visible or invisible. Note that it wtill might
836 * not show the component, if a parent is invisible.
838 * @param visible true to make this component visible
839 * @see #isVisible()
840 * @since 1.1
842 public void setVisible(boolean b)
844 // Inspection by subclassing shows that Sun's implementation calls
845 // show(boolean) which then calls show() or hide(). It is the show()
846 // method that is overriden in subclasses like Window.
847 if (b)
848 show();
849 else
850 hide();
854 * Makes this component visible on the screen.
856 * @deprecated use {@link #setVisible(boolean)} instead
858 public void show()
860 if (peer != null)
861 peer.setVisible(true);
862 this.visible = true;
866 * Makes this component visible or invisible.
868 * @param visible true to make this component visible
869 * @deprecated use {@link #setVisible(boolean)} instead
871 public void show(boolean b)
873 setVisible(b);
877 * Hides this component so that it is no longer shown on the screen.
879 * @deprecated use {@link #setVisible(boolean)} instead
881 public void hide()
883 if (peer != null)
884 peer.setVisible(false);
885 this.visible = false;
889 * Returns this component's foreground color. If not set, this is inherited
890 * from the parent.
892 * @return this component's foreground color, or null
893 * @see #setForeground(Color)
895 public Color getForeground()
897 if (foreground != null)
898 return foreground;
899 return parent == null ? null : parent.getForeground();
903 * Sets this component's foreground color to the specified color. This is a
904 * bound property.
906 * @param c the new foreground color
907 * @see #getForeground()
909 public void setForeground(Color c)
911 firePropertyChange("foreground", foreground, c);
912 if (peer != null)
913 peer.setForeground(c);
914 foreground = c;
918 * Tests if the foreground was explicitly set, or just inherited from the
919 * parent.
921 * @return true if the foreground has been set
922 * @since 1.4
924 public boolean isForegroundSet()
926 return foreground != null;
930 * Returns this component's background color. If not set, this is inherited
931 * from the parent.
933 * @return the background color of the component, or null
934 * @see #setBackground(Color)
936 public Color getBackground()
938 if (background != null)
939 return background;
940 return parent == null ? null : parent.getBackground();
944 * Sets this component's background color to the specified color. The parts
945 * of the component affected by the background color may by system dependent.
946 * This is a bound property.
948 * @param c the new background color
949 * @see #getBackground()
951 public void setBackground(Color c)
953 firePropertyChange("background", background, c);
954 if (peer != null)
955 peer.setBackground(c);
956 background = c;
960 * Tests if the background was explicitly set, or just inherited from the
961 * parent.
963 * @return true if the background has been set
964 * @since 1.4
966 public boolean isBackgroundSet()
968 return background != null;
972 * Returns the font in use for this component. If not set, this is inherited
973 * from the parent.
975 * @return the font for this component
976 * @see #setFont(Font)
978 public Font getFont()
980 if (font != null)
981 return font;
982 return parent == null ? null : parent.getFont();
986 * Sets the font for this component to the specified font. This is a bound
987 * property.
989 * @param font the new font for this component
990 * @see #getFont()
992 public void setFont(Font f)
994 firePropertyChange("font", font, f);
995 if (peer != null)
996 peer.setFont(f);
997 font = f;
1001 * Tests if the font was explicitly set, or just inherited from the parent.
1003 * @return true if the font has been set
1004 * @since 1.4
1006 public boolean isFontSet()
1008 return font != null;
1012 * Returns the locale for this component. If this component does not
1013 * have a locale, the locale of the parent component is returned.
1015 * @return the locale for this component
1016 * @throws IllegalComponentStateException if it has no locale or parent
1017 * @see setLocale(Locale)
1018 * @since 1.1
1020 public Locale getLocale()
1022 if (locale != null)
1023 return locale;
1024 if (parent == null)
1025 throw new IllegalComponentStateException
1026 ("Component has no parent: can't determine Locale");
1027 return parent.getLocale();
1031 * Sets the locale for this component to the specified locale. This is a
1032 * bound property.
1034 * @param locale the new locale for this component
1036 public void setLocale(Locale l)
1038 firePropertyChange("locale", locale, l);
1039 locale = l;
1040 // New writing/layout direction or more/less room for localized labels.
1041 invalidate();
1045 * Returns the color model of the device this componet is displayed on.
1047 * @return this object's color model
1048 * @see Toolkit#getColorModel()
1050 public ColorModel getColorModel()
1052 GraphicsConfiguration config = getGraphicsConfiguration();
1053 return config != null ? config.getColorModel()
1054 : getToolkit().getColorModel();
1058 * Returns the location of this component's top left corner relative to
1059 * its parent component. This may be outdated, so for synchronous behavior,
1060 * you should use a component listner.
1062 * @return the location of this component
1063 * @see #setLocation(int, int)
1064 * @see #getLocationOnScreen()
1065 * @since 1.1
1067 public Point getLocation()
1069 return new Point(x, y);
1073 * Returns the location of this component's top left corner in screen
1074 * coordinates.
1076 * @return the location of this component in screen coordinates
1077 * @throws IllegalComponentStateException if the component is not showing
1079 public Point getLocationOnScreen()
1081 if (! isShowing())
1082 throw new IllegalComponentStateException("component not showing");
1083 // We know peer != null here.
1084 return peer.getLocationOnScreen();
1088 * Returns the location of this component's top left corner relative to
1089 * its parent component.
1091 * @return the location of this component
1092 * @deprecated use {@link #getLocation()} instead
1094 public Point location()
1096 return getLocation();
1100 * Moves this component to the specified location, relative to the parent's
1101 * coordinates. The coordinates are the new upper left corner of this
1102 * component.
1104 * @param x the new X coordinate of this component
1105 * @param y the new Y coordinate of this component
1106 * @see #getLocation()
1107 * @see #setBounds(int, int, int, int)
1109 public void setLocation(int x, int y)
1111 if (this.x == x && this.y == y)
1112 return;
1113 invalidate();
1114 this.x = x;
1115 this.y = y;
1116 if (peer != null)
1117 peer.setBounds(x, y, width, height);
1121 * Moves this component to the specified location, relative to the parent's
1122 * coordinates. The coordinates are the new upper left corner of this
1123 * component.
1125 * @param x the new X coordinate of this component
1126 * @param y the new Y coordinate of this component
1127 * @deprecated use {@link #setLocation(int, int)} instead
1129 public void move(int x, int y)
1131 setLocation(x, y);
1135 * Moves this component to the specified location, relative to the parent's
1136 * coordinates. The coordinates are the new upper left corner of this
1137 * component.
1139 * @param p new coordinates for this component
1140 * @throws NullPointerException if p is null
1141 * @see #getLocation()
1142 * @see #setBounds(int, int, int, int)
1143 * @since 1.1
1145 public void setLocation(Point p)
1147 setLocation(p.x, p.y);
1151 * Returns the size of this object.
1153 * @return the size of this object
1154 * @see #setSize(int, int)
1155 * @since 1.1
1157 public Dimension getSize()
1159 return new Dimension(width, height);
1163 * Returns the size of this object.
1165 * @return the size of this object
1166 * @deprecated use {@link #getSize()} instead
1168 public Dimension size()
1170 return getSize();
1174 * Sets the size of this component to the specified width and height.
1176 * @param width the new width of this component
1177 * @param height the new height of this component
1178 * @see #getSize()
1179 * @see #setBounds(int, int, int, int)
1181 public void setSize(int width, int height)
1183 if (this.width == width && this.height == height)
1184 return;
1185 invalidate();
1186 this.width = width;
1187 this.height = height;
1188 if (peer != null)
1189 peer.setBounds(x, y, width, height);
1193 * Sets the size of this component to the specified value.
1195 * @param width the new width of the component
1196 * @param height the new height of the component
1197 * @deprecated use {@link #setSize(int, int)} instead
1199 public void resize(int width, int height)
1201 setSize(width, height);
1205 * Sets the size of this component to the specified value.
1207 * @param d the new size of this component
1208 * @throws NullPointerException if d is null
1209 * @see #setSize(int, int)
1210 * @see #setBounds(int, int, int, int)
1211 * @since 1.1
1213 public void setSize(Dimension d)
1215 setSize(d.width, d.height);
1219 * Sets the size of this component to the specified value.
1221 * @param d the new size of this component
1222 * @throws NullPointerException if d is null
1223 * @deprecated use {@link #setSize(Dimension)} instead
1225 public void resize(Dimension d)
1227 setSize(d.width, d.height);
1231 * Returns a bounding rectangle for this component. Note that the
1232 * returned rectange is relative to this component's parent, not to
1233 * the screen.
1235 * @return the bounding rectangle for this component
1236 * @see #setBounds(int, int, int, int)
1237 * @see #getLocation()
1238 * @see #getSize()
1240 public Rectangle getBounds()
1242 return new Rectangle(x, y, width, height);
1246 * Returns a bounding rectangle for this component. Note that the
1247 * returned rectange is relative to this component's parent, not to
1248 * the screen.
1250 * @return the bounding rectangle for this component
1251 * @deprecated use {@link #getBounds()} instead
1253 public Rectangle bounds()
1255 return getBounds();
1259 * Sets the bounding rectangle for this component to the specified values.
1260 * Note that these coordinates are relative to the parent, not to the screen.
1262 * @param x the X coordinate of the upper left corner of the rectangle
1263 * @param y the Y coordinate of the upper left corner of the rectangle
1264 * @param w the width of the rectangle
1265 * @param h the height of the rectangle
1266 * @see #getBounds()
1267 * @see #setLocation(int, int)
1268 * @see #setLocation(Point)
1269 * @see #setSize(int, int)
1270 * @see #setSize(Dimension)
1271 * @since 1.1
1273 public void setBounds(int x, int y, int w, int h)
1275 if (this.x == x && this.y == y && width == w && height == h)
1276 return;
1277 invalidate();
1278 this.x = x;
1279 this.y = y;
1280 width = w;
1281 height = h;
1282 if (peer != null)
1283 peer.setBounds(x, y, w, h);
1287 * Sets the bounding rectangle for this component to the specified values.
1288 * Note that these coordinates are relative to the parent, not to the screen.
1290 * @param x the X coordinate of the upper left corner of the rectangle
1291 * @param y the Y coordinate of the upper left corner of the rectangle
1292 * @param w the width of the rectangle
1293 * @param h the height of the rectangle
1294 * @deprecated use {@link #setBounds(int, int, int, int)} instead
1296 public void reshape(int x, int y, int width, int height)
1298 setBounds(x, y, width, height);
1302 * Sets the bounding rectangle for this component to the specified
1303 * rectangle. Note that these coordinates are relative to the parent, not
1304 * to the screen.
1306 * @param r the new bounding rectangle
1307 * @throws NullPointerException if r is null
1308 * @see #getBounds()
1309 * @see #setLocation(Point)
1310 * @see #setSize(Dimension)
1311 * @since 1.1
1313 public void setBounds(Rectangle r)
1315 setBounds(r.x, r.y, r.width, r.height);
1319 * Gets the x coordinate of the upper left corner. This is more efficient
1320 * than getBounds().x or getLocation().x.
1322 * @return the current x coordinate
1323 * @since 1.2
1325 public int getX()
1327 return x;
1331 * Gets the y coordinate of the upper left corner. This is more efficient
1332 * than getBounds().y or getLocation().y.
1334 * @return the current y coordinate
1335 * @since 1.2
1337 public int getY()
1339 return y;
1343 * Gets the width of the component. This is more efficient than
1344 * getBounds().width or getSize().width.
1346 * @return the current width
1347 * @since 1.2
1349 public int getWidth()
1351 return width;
1355 * Gets the height of the component. This is more efficient than
1356 * getBounds().height or getSize().height.
1358 * @return the current width
1359 * @since 1.2
1361 public int getHeight()
1363 return height;
1367 * Returns the bounds of this component. This allows reuse of an existing
1368 * rectangle, if r is non-null.
1370 * @param r the rectangle to use, or null
1371 * @return the bounds
1373 public Rectangle getBounds(Rectangle r)
1375 if (r == null)
1376 r = new Rectangle();
1377 r.x = x;
1378 r.y = y;
1379 r.width = width;
1380 r.height = height;
1381 return r;
1385 * Returns the size of this component. This allows reuse of an existing
1386 * dimension, if d is non-null.
1388 * @param d the dimension to use, or null
1389 * @return the size
1391 public Dimension getSize(Dimension d)
1393 if (d == null)
1394 d = new Dimension();
1395 d.width = width;
1396 d.height = height;
1397 return d;
1401 * Returns the location of this component. This allows reuse of an existing
1402 * point, if p is non-null.
1404 * @param p the point to use, or null
1405 * @return the location
1407 public Point getLocation(Point p)
1409 if (p == null)
1410 p = new Point();
1411 p.x = x;
1412 p.y = y;
1413 return p;
1417 * Tests if this component is opaque. All "heavyweight" (natively-drawn)
1418 * components are opaque. A component is opaque if it draws all pixels in
1419 * the bounds; a lightweight component is partially transparent if it lets
1420 * pixels underneath show through. Subclasses that guarantee that all pixels
1421 * will be drawn should override this.
1423 * @return true if this is opaque
1424 * @see #isLightweight()
1425 * @since 1.2
1427 public boolean isOpaque()
1429 return ! isLightweight();
1433 * Return whether the component is lightweight. That means the component has
1434 * no native peer, but is displayable. This applies to subclasses of
1435 * Component not in this package, such as javax.swing.
1437 * @return true if the component has a lightweight peer
1438 * @see #isDisplayable()
1439 * @since 1.2
1441 public boolean isLightweight()
1443 return peer instanceof LightweightPeer;
1447 * Returns the component's preferred size.
1449 * @return the component's preferred size
1450 * @see #getMinimumSize()
1451 * @see LayoutManager
1453 public Dimension getPreferredSize()
1455 if (prefSize == null)
1456 if (peer == null)
1457 return new Dimension(width, height);
1458 else
1459 prefSize = peer.getPreferredSize();
1460 return prefSize;
1464 * Returns the component's preferred size.
1466 * @return the component's preferred size
1467 * @deprecated use {@link #getPreferredSize()} instead
1469 public Dimension preferredSize()
1471 return getPreferredSize();
1475 * Returns the component's minimum size.
1477 * @return the component's minimum size
1478 * @see #getPreferredSize()
1479 * @see LayoutManager
1481 public Dimension getMinimumSize()
1483 if (minSize == null)
1484 minSize = (peer != null ? peer.getMinimumSize()
1485 : new Dimension(width, height));
1486 return minSize;
1490 * Returns the component's minimum size.
1492 * @return the component's minimum size
1493 * @deprecated use {@link #getMinimumSize()} instead
1495 public Dimension minimumSize()
1497 return getMinimumSize();
1501 * Returns the component's maximum size.
1503 * @return the component's maximum size
1504 * @see #getMinimumSize()
1505 * @see #getPreferredSize()
1506 * @see LayoutManager
1508 public Dimension getMaximumSize()
1510 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1514 * Returns the preferred horizontal alignment of this component. The value
1515 * returned will be between {@link #LEFT_ALIGNMENT} and
1516 * {@link #RIGHT_ALIGNMENT}, inclusive.
1518 * @return the preferred horizontal alignment of this component
1520 public float getAlignmentX()
1522 return CENTER_ALIGNMENT;
1526 * Returns the preferred vertical alignment of this component. The value
1527 * returned will be between {@link #TOP_ALIGNMENT} and
1528 * {@link #BOTTOM_ALIGNMENT}, inclusive.
1530 * @return the preferred vertical alignment of this component
1532 public float getAlignmentY()
1534 return CENTER_ALIGNMENT;
1538 * Calls the layout manager to re-layout the component. This is called
1539 * during validation of a container in most cases.
1541 * @see #validate()
1542 * @see LayoutManager
1544 public void doLayout()
1546 // nothing to do unless we're a container
1550 * Calls the layout manager to re-layout the component. This is called
1551 * during validation of a container in most cases.
1553 * @deprecated use {@link #doLayout()} instead
1555 public void layout()
1557 doLayout();
1561 * Called to ensure that the layout for this component is valid. This is
1562 * usually called on containers.
1564 * @see #invalidate()
1565 * @see #doLayout()
1566 * @see LayoutManager
1567 * @see Container#validate()
1569 public void validate()
1571 valid = true;
1575 * Invalidates this component and all of its parent components. This will
1576 * cause them to have their layout redone. This is called frequently, so
1577 * make it fast.
1579 public void invalidate()
1581 valid = false;
1582 prefSize = null;
1583 minSize = null;
1584 if (parent != null && parent.valid)
1585 parent.invalidate();
1589 * Returns a graphics object for this component. Returns <code>null</code>
1590 * if this component is not currently displayed on the screen.
1592 * @return a graphics object for this component
1593 * @see #paint(Graphics)
1595 public Graphics getGraphics()
1597 if (peer != null)
1599 Graphics gfx = peer.getGraphics();
1600 if (gfx != null)
1601 return gfx;
1602 // create graphics for lightweight:
1603 Container parent = getParent();
1604 if (parent != null)
1606 gfx = parent.getGraphics();
1607 Rectangle bounds = getBounds();
1608 gfx.setClip(bounds);
1609 gfx.translate(bounds.x, bounds.y);
1610 return gfx;
1613 return null;
1617 * Returns the font metrics for the specified font in this component.
1619 * @param font the font to retrieve metrics for
1620 * @return the font metrics for the specified font
1621 * @throws NullPointerException if font is null
1622 * @see #getFont()
1623 * @see Toolkit#getFontMetrics(Font)
1625 public FontMetrics getFontMetrics(Font font)
1627 return peer == null ? getToolkit().getFontMetrics(font)
1628 : peer.getFontMetrics(font);
1632 * Sets the cursor for this component to the specified cursor. The cursor
1633 * is displayed when the point is contained by the component, and the
1634 * component is visible, displayable, and enabled. This is inherited by
1635 * subcomponents unless they set their own cursor.
1637 * @param cursor the new cursor for this component
1638 * @see #isEnabled()
1639 * @see #isShowing()
1640 * @see #getCursor()
1641 * @see #contains(int, int)
1642 * @see Toolkit#createCustomCursor(Image, Point, String)
1644 public void setCursor(Cursor cursor)
1646 this.cursor = cursor;
1647 if (peer != null)
1648 peer.setCursor(cursor);
1652 * Returns the cursor for this component. If not set, this is inherited
1653 * from the parent, or from Cursor.getDefaultCursor().
1655 * @return the cursor for this component
1657 public Cursor getCursor()
1659 if (cursor != null)
1660 return cursor;
1661 return parent != null ? parent.getCursor() : Cursor.getDefaultCursor();
1665 * Tests if the cursor was explicitly set, or just inherited from the parent.
1667 * @return true if the cursor has been set
1668 * @since 1.4
1670 public boolean isCursorSet()
1672 return cursor != null;
1676 * Paints this component on the screen. The clipping region in the graphics
1677 * context will indicate the region that requires painting. This is called
1678 * whenever the component first shows, or needs to be repaired because
1679 * something was temporarily drawn on top. It is not necessary for
1680 * subclasses to call <code>super.paint(g)</code>. Components with no area
1681 * are not painted.
1683 * @param g the graphics context for this paint job
1684 * @see #update(Graphics)
1686 public void paint(Graphics g)
1691 * Updates this component. This is called in response to
1692 * <code>repaint</code>. This method fills the component with the
1693 * background color, then sets the foreground color of the specified
1694 * graphics context to the foreground color of this component and calls
1695 * the <code>paint()</code> method. The coordinates of the graphics are
1696 * relative to this component. Subclasses should call either
1697 * <code>super.update(g)</code> or <code>paint(g)</code>.
1699 * @param graphics the graphics context for this update
1700 * @see #paint(Graphics)
1701 * @see #repaint()
1703 public void update(Graphics g)
1705 paint(g);
1709 * Paints this entire component, including any sub-components.
1711 * @param graphics the graphics context for this paint job
1712 * @see #paint(Graphics)
1714 public void paintAll(Graphics g)
1716 if (! visible)
1717 return;
1718 if (peer != null)
1719 peer.paint(g);
1720 paint(g);
1724 * Repaint this entire component. The <code>update()</code> method
1725 * on this component will be called as soon as possible.
1727 * @see #update(Graphics)
1728 * @see #repaint(long, int, int, int, int)
1730 public void repaint()
1732 repaint(0, 0, 0, width, height);
1736 * Repaint this entire component. The <code>update()</code> method on this
1737 * component will be called in approximate the specified number of
1738 * milliseconds.
1740 * @param tm milliseconds before this component should be repainted
1741 * @see #paint(Graphics)
1742 * @see #repaint(long, int, int, int, int)
1744 public void repaint(long tm)
1746 repaint(tm, 0, 0, width, height);
1750 * Repaints the specified rectangular region within this component. The
1751 * <code>update</code> method on this component will be called as soon as
1752 * possible. The coordinates are relative to this component.
1754 * @param x the X coordinate of the upper left of the region to repaint
1755 * @param y the Y coordinate of the upper left of the region to repaint
1756 * @param w the width of the region to repaint
1757 * @param h the height of the region to repaint
1758 * @see #update(Graphics)
1759 * @see #repaint(long, int, int, int, int)
1761 public void repaint(int x, int y, int w, int h)
1763 repaint(0, x, y, w, h);
1767 * Repaints the specified rectangular region within this component. The
1768 * <code>update</code> method on this component will be called in
1769 * approximately the specified number of milliseconds. The coordinates
1770 * are relative to this component.
1772 * @param tm milliseconds before this component should be repainted
1773 * @param x the X coordinate of the upper left of the region to repaint
1774 * @param y the Y coordinate of the upper left of the region to repaint
1775 * @param w the width of the region to repaint
1776 * @param h the height of the region to repaint
1777 * @see #update(Graphics)
1779 public void repaint(long tm, int x, int y, int width, int height)
1781 // Handle lightweight repainting by forwarding to native parent
1782 if (isLightweight() && parent != null)
1784 if (parent != null)
1785 parent.repaint(tm, x + getX(), y + getY(), width, height);
1787 else if (peer != null)
1788 peer.repaint(tm, x, y, width, height);
1792 * Prints this component. This method is provided so that printing can be
1793 * done in a different manner from painting. However, the implementation
1794 * in this class simply calls the <code>paint()</code> method.
1796 * @param graphics the graphics context of the print device
1797 * @see #paint(Graphics)
1799 public void print(Graphics g)
1801 paint(g);
1805 * Prints this component, including all sub-components. This method is
1806 * provided so that printing can be done in a different manner from
1807 * painting. However, the implementation in this class simply calls the
1808 * <code>paintAll()</code> method.
1810 * @param graphics the graphics context of the print device
1811 * @see #paintAll(Graphics)
1813 public void printAll(Graphics g)
1815 paintAll(g);
1819 * Called when an image has changed so that this component is repainted.
1820 * This incrementally draws an image as more bits are available, when
1821 * possible. Incremental drawing is enabled if the system property
1822 * <code>awt.image.incrementalDraw</code> is not present or is true, in which
1823 * case the redraw rate is set to 100ms or the value of the system property
1824 * <code>awt.image.redrawrate</code>.
1826 * <p>The coordinate system used depends on the particular flags.
1828 * @param image the image that has been updated
1829 * @param flags tlags as specified in <code>ImageObserver</code>
1830 * @param x the X coordinate
1831 * @param y the Y coordinate
1832 * @param w the width
1833 * @param h the height
1834 * @return true if the image has been fully loaded
1835 * @see ImageObserver
1836 * @see Graphics#drawImage(Image, int, int, Color, ImageObserver)
1837 * @see Graphics#drawImage(Image, int, int, ImageObserver)
1838 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
1839 * @see Graphics#drawImage(Image, int, int, int, int, ImageObserver)
1840 * @see ImageObserver#update(Image, int, int, int, int, int)
1842 public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
1844 // XXX Implement.
1845 throw new Error("not implemented");
1849 * Creates an image from the specified producer.
1851 * @param producer the image procedure to create the image from
1852 * @return the resulting image
1854 public Image createImage(ImageProducer producer)
1856 // XXX What if peer or producer is null?
1857 return peer.createImage(producer);
1861 * Creates an image with the specified width and height for use in
1862 * double buffering. Headless environments do not support images.
1864 * @param width the width of the image
1865 * @param height the height of the image
1866 * @return the requested image, or null if it is not supported
1868 public Image createImage(int width, int height)
1870 if (GraphicsEnvironment.isHeadless())
1871 return null;
1872 GraphicsConfiguration config = getGraphicsConfiguration();
1873 return config == null ? null : config.createCompatibleImage(width, height);
1877 * Creates an image with the specified width and height for use in
1878 * double buffering. Headless environments do not support images.
1880 * @param width the width of the image
1881 * @param height the height of the image
1882 * @return the requested image, or null if it is not supported
1883 * @since 1.4
1885 public VolatileImage createVolatileImage(int width, int height)
1887 if (GraphicsEnvironment.isHeadless())
1888 return null;
1889 GraphicsConfiguration config = getGraphicsConfiguration();
1890 return config == null ? null
1891 : config.createCompatibleVolatileImage(width, height);
1895 * Creates an image with the specified width and height for use in
1896 * double buffering. Headless environments do not support images. The image
1897 * will support the specified capabilities.
1899 * @param width the width of the image
1900 * @param height the height of the image
1901 * @param caps the requested capabilities
1902 * @return the requested image, or null if it is not supported
1903 * @throws AWTException if a buffer with the capabilities cannot be created
1904 * @since 1.4
1906 public VolatileImage createVolatileImage(int width, int height,
1907 ImageCapabilities caps)
1908 throws AWTException
1910 if (GraphicsEnvironment.isHeadless())
1911 return null;
1912 GraphicsConfiguration config = getGraphicsConfiguration();
1913 return config == null ? null
1914 : config.createCompatibleVolatileImage(width, height, caps);
1918 * Prepares the specified image for rendering on this component.
1920 * @param image the image to prepare for rendering
1921 * @param observer the observer to notify of image preparation status
1922 * @return true if the image is already fully prepared
1923 * @throws NullPointerException if image is null
1925 public boolean prepareImage(Image image, ImageObserver observer)
1927 return prepareImage(image, image.getWidth(observer),
1928 image.getHeight(observer), observer);
1932 * Prepares the specified image for rendering on this component at the
1933 * specified scaled width and height
1935 * @param image the image to prepare for rendering
1936 * @param width the scaled width of the image
1937 * @param height the scaled height of the image
1938 * @param observer the observer to notify of image preparation status
1939 * @return true if the image is already fully prepared
1941 public boolean prepareImage(Image image, int width, int height,
1942 ImageObserver observer)
1944 return peer.prepareImage(image, width, height, observer);
1948 * Returns the status of the loading of the specified image. The value
1949 * returned will be those flags defined in <code>ImageObserver</code>.
1951 * @param image the image to check on
1952 * @param observer the observer to notify of image loading progress
1953 * @return the image observer flags indicating the status of the load
1954 * @see #prepareImage(Image, int, int, ImageObserver)
1955 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
1956 * @throws NullPointerException if image is null
1958 public int checkImage(Image image, ImageObserver observer)
1960 return checkImage(image, image.getWidth(observer),
1961 image.getHeight(observer), observer);
1965 * Returns the status of the loading of the specified image. The value
1966 * returned will be those flags defined in <code>ImageObserver</code>.
1968 * @param image the image to check on
1969 * @param width the scaled image width
1970 * @param height the scaled image height
1971 * @param observer the observer to notify of image loading progress
1972 * @return the image observer flags indicating the status of the load
1973 * @see #prepareImage(Image, int, int, ImageObserver)
1974 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
1976 public int checkImage(Image image, int width, int height,
1977 ImageObserver observer)
1979 if (peer != null)
1980 return peer.checkImage(image, width, height, observer);
1981 return getToolkit().checkImage(image, width, height, observer);
1985 * Sets whether paint messages delivered by the operating system should be
1986 * ignored. This does not affect messages from AWT, except for those
1987 * triggered by OS messages. Setting this to true can allow faster
1988 * performance in full-screen mode or page-flipping.
1990 * @param ignoreRepaint the new setting for ignoring repaint events
1991 * @see #getIgnoreRepaint()
1992 * @see BufferStrategy
1993 * @see GraphicsDevice.setFullScreenWindow(Window)
1994 * @since 1.4
1996 public void setIgnoreRepaint(boolean ignoreRepaint)
1998 this.ignoreRepaint = ignoreRepaint;
2002 * Test whether paint events from the operating system are ignored.
2004 * @return the status of ignoring paint events
2005 * @see #setIgnoreRepaint(boolean)
2006 * @since 1.4
2008 public boolean getIgnoreRepaint()
2010 return ignoreRepaint;
2014 * Tests whether or not the specified point is contained within this
2015 * component. Coordinates are relative to this component.
2017 * @param x the X coordinate of the point to test
2018 * @param y the Y coordinate of the point to test
2019 * @return true if the point is within this component
2020 * @see #getComponentAt(int, int)
2022 public boolean contains(int x, int y)
2024 return x >= 0 && y >= 0 && x < width && y < height;
2028 * Tests whether or not the specified point is contained within this
2029 * component. Coordinates are relative to this component.
2031 * @param x the X coordinate of the point to test
2032 * @param y the Y coordinate of the point to test
2033 * @return true if the point is within this component
2034 * @deprecated use {@link #contains(int, int)} instead
2036 public boolean inside(int x, int y)
2038 return contains(x, y);
2042 * Tests whether or not the specified point is contained within this
2043 * component. Coordinates are relative to this component.
2045 * @param p the point to test
2046 * @return true if the point is within this component
2047 * @throws NullPointerException if p is null
2048 * @see #getComponentAt(Point)
2049 * @since 1.1
2051 public boolean contains(Point p)
2053 return contains(p.x, p.y);
2057 * Returns the component occupying the position (x,y). This will either
2058 * be this component, an immediate child component, or <code>null</code>
2059 * if neither of the first two occupies the specified location.
2061 * @param x the X coordinate to search for components at
2062 * @param y the Y coordinate to search for components at
2063 * @return the component at the specified location, or null
2064 * @see #contains(int, int)
2066 public Component getComponentAt(int x, int y)
2068 return contains(x, y) ? this : null;
2072 * Returns the component occupying the position (x,y). This will either
2073 * be this component, an immediate child component, or <code>null</code>
2074 * if neither of the first two occupies the specified location.
2076 * @param x the X coordinate to search for components at
2077 * @param y the Y coordinate to search for components at
2078 * @return the component at the specified location, or null
2079 * @deprecated use {@link #getComponentAt(int, int)} instead
2081 public Component locate(int x, int y)
2083 return getComponentAt(x, y);
2087 * Returns the component occupying the position (x,y). This will either
2088 * be this component, an immediate child component, or <code>null</code>
2089 * if neither of the first two occupies the specified location.
2091 * @param p the point to search for components at
2092 * @return the component at the specified location, or null
2093 * @throws NullPointerException if p is null
2094 * @see #contains(Point)
2095 * @since 1.1
2097 public Component getComponentAt(Point p)
2099 return getComponentAt(p.x, p.y);
2103 * AWT 1.0 event dispatcher.
2105 * @param e the event to dispatch
2106 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2108 public void deliverEvent(Event e)
2110 // XXX Add backward compatibility handling.
2114 * Forwards AWT events to processEvent() if:<ul>
2115 * <li>Events have been enabled for this type of event via
2116 * <code>enableEvents()</code></li>,
2117 * <li>There is at least one registered listener for this type of event</li>
2118 * </ul>
2120 * @param e the event to dispatch
2122 public final void dispatchEvent(AWTEvent e)
2124 // Some subclasses in the AWT package need to override this behavior,
2125 // hence the use of dispatchEventImpl().
2126 dispatchEventImpl(e);
2127 if (peer != null && ! e.consumed)
2128 peer.handleEvent(e);
2132 * AWT 1.0 event dispatcher.
2134 * @param e the event to dispatch
2135 * @return false: since the method was deprecated, the return has no meaning
2136 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2138 public boolean postEvent(Event e)
2140 // XXX Add backward compatibility handling.
2141 return false;
2145 * Adds the specified listener to this component. This is harmless if the
2146 * listener is null, but if the listener has already been registered, it
2147 * will now be registered twice.
2149 * @param listener the new listener to add
2150 * @see ComponentEvent
2151 * @see #removeComponentListener(ComponentListener)
2152 * @see #getComponentListeners()
2153 * @since 1.1
2155 public synchronized void addComponentListener(ComponentListener l)
2157 componentListener = AWTEventMulticaster.add(componentListener, l);
2158 if (componentListener != null)
2159 enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
2163 * Removes the specified listener from the component. This is harmless if
2164 * the listener was not previously registered.
2166 * @param listener the listener to remove
2167 * @see ComponentEvent
2168 * @see #addComponentListener(ComponentListener)
2169 * @see #getComponentListeners()
2170 * @since 1.1
2172 public synchronized void removeComponentListener(ComponentListener l)
2174 componentListener = AWTEventMulticaster.remove(componentListener, l);
2178 * Returns an array of all specified listeners registered on this component.
2180 * @return an array of listeners
2181 * @see #addComponentListener(ComponentListener)
2182 * @see #removeComponentListener(ComponentListener)
2183 * @since 1.4
2185 public synchronized ComponentListener[] getComponentListeners()
2187 return (ComponentListener[])
2188 AWTEventMulticaster.getListeners(componentListener,
2189 ComponentListener.class);
2193 * Adds the specified listener to this component. This is harmless if the
2194 * listener is null, but if the listener has already been registered, it
2195 * will now be registered twice.
2197 * @param listener the new listener to add
2198 * @see FocusEvent
2199 * @see #removeFocusListener(FocusListener)
2200 * @see #getFocusListeners()
2201 * @since 1.1
2203 public synchronized void addFocusListener(FocusListener l)
2205 focusListener = AWTEventMulticaster.add(focusListener, l);
2206 if (focusListener != null)
2207 enableEvents(AWTEvent.FOCUS_EVENT_MASK);
2211 * Removes the specified listener from the component. This is harmless if
2212 * the listener was not previously registered.
2214 * @param listener the listener to remove
2215 * @see FocusEvent
2216 * @see #addFocusListener(FocusListener)
2217 * @see #getFocusListeners()
2218 * @since 1.1
2220 public synchronized void removeFocusListener(FocusListener l)
2222 focusListener = AWTEventMulticaster.remove(focusListener, l);
2226 * Returns an array of all specified listeners registered on this component.
2228 * @return an array of listeners
2229 * @see #addFocusListener(FocusListener)
2230 * @see #removeFocusListener(FocusListener)
2231 * @since 1.4
2233 public synchronized FocusListener[] getFocusListeners()
2235 return (FocusListener[])
2236 AWTEventMulticaster.getListeners(focusListener, FocusListener.class);
2240 * Adds the specified listener to this component. This is harmless if the
2241 * listener is null, but if the listener has already been registered, it
2242 * will now be registered twice.
2244 * @param listener the new listener to add
2245 * @see HierarchyEvent
2246 * @see #removeHierarchyListener(HierarchyListener)
2247 * @see #getHierarchyListeners()
2248 * @since 1.3
2250 public synchronized void addHierarchyListener(HierarchyListener l)
2252 hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
2253 if (hierarchyListener != null)
2254 enableEvents(AWTEvent.HIERARCHY_EVENT_MASK);
2258 * Removes the specified listener from the component. This is harmless if
2259 * the listener was not previously registered.
2261 * @param listener the listener to remove
2262 * @see HierarchyEvent
2263 * @see #addHierarchyListener(HierarchyListener)
2264 * @see #getHierarchyListeners()
2265 * @since 1.3
2267 public synchronized void removeHierarchyListener(HierarchyListener l)
2269 hierarchyListener = AWTEventMulticaster.remove(hierarchyListener, l);
2273 * Returns an array of all specified listeners registered on this component.
2275 * @return an array of listeners
2276 * @see #addHierarchyListener(HierarchyListener)
2277 * @see #removeHierarchyListener(HierarchyListener)
2278 * @since 1.4
2280 public synchronized HierarchyListener[] getHierarchyListeners()
2282 return (HierarchyListener[])
2283 AWTEventMulticaster.getListeners(hierarchyListener,
2284 HierarchyListener.class);
2288 * Adds the specified listener to this component. This is harmless if the
2289 * listener is null, but if the listener has already been registered, it
2290 * will now be registered twice.
2292 * @param listener the new listener to add
2293 * @see HierarchyEvent
2294 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2295 * @see #getHierarchyBoundsListeners()
2296 * @since 1.3
2298 public synchronized void
2299 addHierarchyBoundsListener(HierarchyBoundsListener l)
2301 hierarchyBoundsListener =
2302 AWTEventMulticaster.add(hierarchyBoundsListener, l);
2303 if (hierarchyBoundsListener != null)
2304 enableEvents(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
2308 * Removes the specified listener from the component. This is harmless if
2309 * the listener was not previously registered.
2311 * @param listener the listener to remove
2312 * @see HierarchyEvent
2313 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2314 * @see #getHierarchyBoundsListeners()
2315 * @since 1.3
2317 public synchronized void
2318 removeHierarchyBoundsListener(HierarchyBoundsListener l)
2320 hierarchyBoundsListener =
2321 AWTEventMulticaster.remove(hierarchyBoundsListener, l);
2325 * Returns an array of all specified listeners registered on this component.
2327 * @return an array of listeners
2328 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2329 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2330 * @since 1.4
2332 public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners()
2334 return (HierarchyBoundsListener[])
2335 AWTEventMulticaster.getListeners(hierarchyBoundsListener,
2336 HierarchyBoundsListener.class);
2340 * Adds the specified listener to this component. This is harmless if the
2341 * listener is null, but if the listener has already been registered, it
2342 * will now be registered twice.
2344 * @param listener the new listener to add
2345 * @see KeyEvent
2346 * @see #removeKeyListener(KeyListener)
2347 * @see #getKeyListeners()
2348 * @since 1.1
2350 public synchronized void addKeyListener(KeyListener l)
2352 keyListener = AWTEventMulticaster.add(keyListener, l);
2353 if (keyListener != null)
2354 enableEvents(AWTEvent.KEY_EVENT_MASK);
2358 * Removes the specified listener from the component. This is harmless if
2359 * the listener was not previously registered.
2361 * @param listener the listener to remove
2362 * @see KeyEvent
2363 * @see #addKeyListener(KeyListener)
2364 * @see #getKeyListeners()
2365 * @since 1.1
2367 public synchronized void removeKeyListener(KeyListener l)
2369 keyListener = AWTEventMulticaster.remove(keyListener, l);
2373 * Returns an array of all specified listeners registered on this component.
2375 * @return an array of listeners
2376 * @see #addKeyListener(KeyListener)
2377 * @see #removeKeyListener(KeyListener)
2378 * @since 1.4
2380 public synchronized KeyListener[] getKeyListeners()
2382 return (KeyListener[])
2383 AWTEventMulticaster.getListeners(keyListener, KeyListener.class);
2387 * Adds the specified listener to this component. This is harmless if the
2388 * listener is null, but if the listener has already been registered, it
2389 * will now be registered twice.
2391 * @param listener the new listener to add
2392 * @see MouseEvent
2393 * @see #removeMouseListener(MouseListener)
2394 * @see #getMouseListeners()
2395 * @since 1.1
2397 public synchronized void addMouseListener(MouseListener l)
2399 mouseListener = AWTEventMulticaster.add(mouseListener, l);
2400 if (mouseListener != null)
2401 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2405 * Removes the specified listener from the component. This is harmless if
2406 * the listener was not previously registered.
2408 * @param listener the listener to remove
2409 * @see MouseEvent
2410 * @see #addMouseListener(MouseListener)
2411 * @see #getMouseListeners()
2412 * @since 1.1
2414 public synchronized void removeMouseListener(MouseListener l)
2416 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
2420 * Returns an array of all specified listeners registered on this component.
2422 * @return an array of listeners
2423 * @see #addMouseListener(MouseListener)
2424 * @see #removeMouseListener(MouseListener)
2425 * @since 1.4
2427 public synchronized MouseListener[] getMouseListeners()
2429 return (MouseListener[])
2430 AWTEventMulticaster.getListeners(mouseListener, MouseListener.class);
2434 * Adds the specified listener to this component. This is harmless if the
2435 * listener is null, but if the listener has already been registered, it
2436 * will now be registered twice.
2438 * @param listener the new listener to add
2439 * @see MouseEvent
2440 * @see #removeMouseMotionListener(MouseMotionListener)
2441 * @see #getMouseMotionListeners()
2442 * @since 1.1
2444 public synchronized void addMouseMotionListener(MouseMotionListener l)
2446 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, l);
2447 if (mouseMotionListener != null)
2448 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2452 * Removes the specified listener from the component. This is harmless if
2453 * the listener was not previously registered.
2455 * @param listener the listener to remove
2456 * @see MouseEvent
2457 * @see #addMouseMotionListener(MouseMotionListener)
2458 * @see #getMouseMotionListeners()
2459 * @since 1.1
2461 public synchronized void removeMouseMotionListener(MouseMotionListener l)
2463 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
2467 * Returns an array of all specified listeners registered on this component.
2469 * @return an array of listeners
2470 * @see #addMouseMotionListener(MouseMotionListener)
2471 * @see #removeMouseMotionListener(MouseMotionListener)
2472 * @since 1.4
2474 public synchronized MouseMotionListener[] getMouseMotionListeners()
2476 return (MouseMotionListener[])
2477 AWTEventMulticaster.getListeners(mouseMotionListener,
2478 MouseMotionListener.class);
2482 * Adds the specified listener to this component. This is harmless if the
2483 * listener is null, but if the listener has already been registered, it
2484 * will now be registered twice.
2486 * @param listener the new listener to add
2487 * @see MouseEvent
2488 * @see MouseWheelEvent
2489 * @see #removeMouseWheelListener(MouseWheelListener)
2490 * @see #getMouseWheelListeners()
2491 * @since 1.4
2493 public synchronized void addMouseWheelListener(MouseWheelListener l)
2495 mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, l);
2496 if (mouseWheelListener != null)
2497 enableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK);
2501 * Removes the specified listener from the component. This is harmless if
2502 * the listener was not previously registered.
2504 * @param listener the listener to remove
2505 * @see MouseEvent
2506 * @see MouseWheelEvent
2507 * @see #addMouseWheelListener(MouseWheelListener)
2508 * @see #getMouseWheelListeners()
2509 * @since 1.4
2511 public synchronized void removeMouseWheelListener(MouseWheelListener l)
2513 mouseWheelListener = AWTEventMulticaster.remove(mouseWheelListener, l);
2517 * Returns an array of all specified listeners registered on this component.
2519 * @return an array of listeners
2520 * @see #addMouseWheelListener(MouseWheelListener)
2521 * @see #removeMouseWheelListener(MouseWheelListener)
2522 * @since 1.4
2524 public synchronized MouseWheelListener[] getMouseWheelListeners()
2526 return (MouseWheelListener[])
2527 AWTEventMulticaster.getListeners(mouseWheelListener,
2528 MouseWheelListener.class);
2532 * Adds the specified listener to this component. This is harmless if the
2533 * listener is null, but if the listener has already been registered, it
2534 * will now be registered twice.
2536 * @param listener the new listener to add
2537 * @see InputMethodEvent
2538 * @see #removeInputMethodListener(InputMethodListener)
2539 * @see #getInputMethodListeners()
2540 * @see #getInputMethodRequests()
2541 * @since 1.2
2543 public synchronized void addInputMethodListener(InputMethodListener l)
2545 inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
2546 if (inputMethodListener != null)
2547 enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK);
2551 * Removes the specified listener from the component. This is harmless if
2552 * the listener was not previously registered.
2554 * @param listener the listener to remove
2555 * @see InputMethodEvent
2556 * @see #addInputMethodListener(InputMethodListener)
2557 * @see #getInputMethodRequests()
2558 * @since 1.2
2560 public synchronized void removeInputMethodListener(InputMethodListener l)
2562 inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
2566 * Returns an array of all specified listeners registered on this component.
2568 * @return an array of listeners
2569 * @see #addInputMethodListener(InputMethodListener)
2570 * @see #removeInputMethodListener(InputMethodListener)
2571 * @since 1.4
2573 public synchronized InputMethodListener[] getInputMethodListeners()
2575 return (InputMethodListener[])
2576 AWTEventMulticaster.getListeners(inputMethodListener,
2577 InputMethodListener.class);
2581 * Returns all registered EventListers of the given listenerType.
2583 * @param listenerType the class of listeners to filter
2584 * @return an array of registered listeners
2585 * @see #getComponentListeners()
2586 * @see #getFocusListeners()
2587 * @see #getHierarchyListeners()
2588 * @see #getHierarchyBoundsListeners()
2589 * @see #getKeyListeners()
2590 * @see #getMouseListeners()
2591 * @see #getMouseMotionListeners()
2592 * @see #getMouseWheelListeners()
2593 * @see #getInputMethodListeners()
2594 * @see #getPropertyChangeListeners()
2595 * @since 1.3
2597 public EventListener[] getListeners(Class listenerType)
2599 if (listenerType == ComponentListener.class)
2600 return getComponentListeners();
2601 if (listenerType == FocusListener.class)
2602 return getFocusListeners();
2603 if (listenerType == HierarchyListener.class)
2604 return getHierarchyListeners();
2605 if (listenerType == HierarchyBoundsListener.class)
2606 return getHierarchyBoundsListeners();
2607 if (listenerType == KeyListener.class)
2608 return getKeyListeners();
2609 if (listenerType == MouseListener.class)
2610 return getMouseListeners();
2611 if (listenerType == MouseMotionListener.class)
2612 return getMouseMotionListeners();
2613 if (listenerType == MouseWheelListener.class)
2614 return getMouseWheelListeners();
2615 if (listenerType == InputMethodListener.class)
2616 return getInputMethodListeners();
2617 if (listenerType == PropertyChangeListener.class)
2618 return getPropertyChangeListeners();
2619 return (EventListener[]) Array.newInstance(listenerType, 0);
2623 * Returns the input method request handler, for subclasses which support
2624 * on-the-spot text input. By default, input methods are handled by AWT,
2625 * and this returns null.
2627 * @return the input method handler, null by default
2628 * @since 1.2
2630 public InputMethodRequests getInputMethodRequests()
2632 return null;
2636 * Gets the input context of this component, which is inherited from the
2637 * parent unless this is overridden.
2639 * @return the text input context
2640 * @since 1.2
2642 public InputContext getInputContext()
2644 return parent == null ? null : parent.getInputContext();
2648 * Enables the specified events. The events to enable are specified
2649 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2651 * <p>Events are enabled by default when a listener is attached to the
2652 * component for that event type. This method can be used by subclasses
2653 * to ensure the delivery of a specified event regardless of whether
2654 * or not a listener is attached.
2656 * @param eventsToEnable the desired events to enable
2657 * @see #processEvent(AWTEvent)
2658 * @see #disableEvents(long)
2659 * @see AWTEvent
2660 * @since 1.1
2662 protected final void enableEvents(long eventsToEnable)
2664 eventMask |= eventsToEnable;
2665 // TODO: Unlike Sun's implementation, I think we should try and
2666 // enable/disable events at the peer (gtk/X) level. This will avoid
2667 // clogging the event pipeline with useless mousemove events that
2668 // we arn't interested in, etc. This will involve extending the peer
2669 // interface, but thats okay because the peer interfaces have been
2670 // deprecated for a long time, and no longer feature in the
2671 // API specification at all.
2672 if (isLightweight() && parent != null)
2673 parent.enableEvents(eventsToEnable);
2674 else if (peer != null)
2675 peer.setEventMask(eventMask);
2679 * Disables the specified events. The events to disable are specified
2680 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2682 * @param eventsToDisable the desired events to disable
2683 * @see #enableEvents(long)
2684 * @since 1.1
2686 protected final void disableEvents(long eventsToDisable)
2688 eventMask &= ~eventsToDisable;
2689 // forward new event mask to peer?
2693 * This is called by the EventQueue if two events with the same event id
2694 * and owner component are queued. Returns a new combined event, or null if
2695 * no combining is done. The coelesced events are currently mouse moves
2696 * (intermediate ones are discarded) and paint events (a merged paint is
2697 * created in place of the two events).
2699 * @param existingEvent the event on the queue
2700 * @param newEvent the new event that might be entered on the queue
2701 * @return null if both events are kept, or the replacement coelesced event
2703 protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent)
2705 switch (existingEvent.id)
2707 case MouseEvent.MOUSE_MOVED:
2708 case MouseEvent.MOUSE_DRAGGED:
2709 // Just drop the old (intermediate) event and return the new one.
2710 return newEvent;
2711 case PaintEvent.PAINT:
2712 case PaintEvent.UPDATE:
2713 return coalescePaintEvents((PaintEvent) existingEvent,
2714 (PaintEvent) newEvent);
2715 default:
2716 return null;
2721 * Processes the specified event. In this class, this method simply
2722 * calls one of the more specific event handlers.
2724 * @param event the event to process
2725 * @throws NullPointerException if e is null
2726 * @see #processComponentEvent(ComponentEvent)
2727 * @see #processFocusEvent(FocusEvent)
2728 * @see #processKeyEvent(KeyEvent)
2729 * @see #processMouseEvent(MouseEvent)
2730 * @see #processMouseMotionEvent(MouseEvent)
2731 * @see #processInputMethodEvent(InputMethodEvent)
2732 * @see #processHierarchyEvent(HierarchyEvent)
2733 * @see #processMouseWheelEvent(MouseWheelEvent)
2734 * @since 1.1
2736 protected void processEvent(AWTEvent e)
2738 /* Note: the order of these if statements are
2739 important. Subclasses must be checked first. Eg. MouseEvent
2740 must be checked before ComponentEvent, since a MouseEvent
2741 object is also an instance of a ComponentEvent. */
2743 if (e instanceof FocusEvent)
2744 processFocusEvent((FocusEvent) e);
2745 else if (e instanceof PaintEvent)
2746 processPaintEvent((PaintEvent) e);
2747 else if (e instanceof MouseWheelEvent)
2748 processMouseWheelEvent((MouseWheelEvent) e);
2749 else if (e instanceof MouseEvent)
2751 if (e.id == MouseEvent.MOUSE_MOVED
2752 || e.id == MouseEvent.MOUSE_DRAGGED)
2753 processMouseMotionEvent((MouseEvent) e);
2754 else
2755 processMouseEvent((MouseEvent) e);
2757 else if (e instanceof KeyEvent)
2758 processKeyEvent((KeyEvent) e);
2759 else if (e instanceof InputMethodEvent)
2760 processInputMethodEvent((InputMethodEvent) e);
2761 else if (e instanceof ComponentEvent)
2762 processComponentEvent((ComponentEvent) e);
2763 else if (e instanceof HierarchyEvent)
2765 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2766 processHierarchyEvent((HierarchyEvent) e);
2767 else
2768 processHierarchyBoundsEvent((HierarchyEvent) e);
2773 * Called when a component event is dispatched and component events are
2774 * enabled. This method passes the event along to any listeners
2775 * that are attached.
2777 * @param event the <code>ComponentEvent</code> to process
2778 * @throws NullPointerException if e is null
2779 * @see ComponentListener
2780 * @see #addComponentListener(ComponentListener)
2781 * @see #enableEvents(long)
2782 * @since 1.1
2784 protected void processComponentEvent(ComponentEvent e)
2786 if (componentListener == null)
2787 return;
2788 switch (e.id)
2790 case ComponentEvent.COMPONENT_HIDDEN:
2791 componentListener.componentHidden(e);
2792 break;
2793 case ComponentEvent.COMPONENT_MOVED:
2794 componentListener.componentMoved(e);
2795 break;
2796 case ComponentEvent.COMPONENT_RESIZED:
2797 componentListener.componentResized(e);
2798 break;
2799 case ComponentEvent.COMPONENT_SHOWN:
2800 componentListener.componentShown(e);
2801 break;
2806 * Called when a focus event is dispatched and component events are
2807 * enabled. This method passes the event along to any listeners
2808 * that are attached.
2810 * @param event the <code>FocusEvent</code> to process
2811 * @throws NullPointerException if e is null
2812 * @see FocusListener
2813 * @see #addFocusListener(FocusListener)
2814 * @see #enableEvents(long)
2815 * @since 1.1
2817 protected void processFocusEvent(FocusEvent e)
2819 if (focusListener == null)
2820 return;
2821 switch (e.id)
2823 case FocusEvent.FOCUS_GAINED:
2824 focusListener.focusGained(e);
2825 break;
2826 case FocusEvent.FOCUS_LOST:
2827 focusListener.focusLost(e);
2828 break;
2833 * Called when a key event is dispatched and component events are
2834 * enabled. This method passes the event along to any listeners
2835 * that are attached.
2837 * @param event the <code>KeyEvent</code> to process
2838 * @throws NullPointerException if e is null
2839 * @see KeyListener
2840 * @see #addKeyListener(KeyListener)
2841 * @see #enableEvents(long)
2842 * @since 1.1
2844 protected void processKeyEvent(KeyEvent e)
2846 if (keyListener == null)
2847 return;
2848 switch (e.id)
2850 case KeyEvent.KEY_PRESSED:
2851 keyListener.keyPressed(e);
2852 break;
2853 case KeyEvent.KEY_RELEASED:
2854 keyListener.keyReleased(e);
2855 break;
2856 case KeyEvent.KEY_TYPED:
2857 keyListener.keyTyped(e);
2858 break;
2863 * Called when a regular mouse event is dispatched and component events are
2864 * enabled. This method passes the event along to any listeners
2865 * that are attached.
2867 * @param event the <code>MouseEvent</code> to process
2868 * @throws NullPointerException if e is null
2869 * @see MouseListener
2870 * @see #addMouseListener(MouseListener)
2871 * @see #enableEvents(long)
2872 * @since 1.1
2874 protected void processMouseEvent(MouseEvent e)
2876 if (mouseListener == null)
2877 return;
2878 switch (e.id)
2880 case MouseEvent.MOUSE_CLICKED:
2881 mouseListener.mouseClicked(e);
2882 break;
2883 case MouseEvent.MOUSE_ENTERED:
2884 mouseListener.mouseEntered(e);
2885 break;
2886 case MouseEvent.MOUSE_EXITED:
2887 mouseListener.mouseExited(e);
2888 break;
2889 case MouseEvent.MOUSE_PRESSED:
2890 mouseListener.mousePressed(e);
2891 break;
2892 case MouseEvent.MOUSE_RELEASED:
2893 mouseListener.mouseReleased(e);
2894 break;
2899 * Called when a mouse motion event is dispatched and component events are
2900 * enabled. This method passes the event along to any listeners
2901 * that are attached.
2903 * @param event the <code>MouseMotionEvent</code> to process
2904 * @throws NullPointerException if e is null
2905 * @see MouseMotionListener
2906 * @see #addMouseMotionListener(MouseMotionListener)
2907 * @see #enableEvents(long)
2908 * @since 1.1
2910 protected void processMouseMotionEvent(MouseEvent e)
2912 if (mouseMotionListener == null)
2913 return;
2914 switch (e.id)
2916 case MouseEvent.MOUSE_DRAGGED:
2917 mouseMotionListener.mouseDragged(e);
2918 break;
2919 case MouseEvent.MOUSE_MOVED:
2920 mouseMotionListener.mouseMoved(e);
2921 break;
2926 * Called when a mouse wheel event is dispatched and component events are
2927 * enabled. This method passes the event along to any listeners that are
2928 * attached.
2930 * @param event the <code>MouseWheelEvent</code> to process
2931 * @throws NullPointerException if e is null
2932 * @see MouseWheelListener
2933 * @see #addMouseWheelListener(MouseWheelListener)
2934 * @see #enableEvents(long)
2935 * @since 1.4
2937 protected void processMouseWheelEvent(MouseWheelEvent e)
2939 if (mouseWheelListener != null
2940 && e.id == MouseEvent.MOUSE_WHEEL)
2941 mouseWheelListener.mouseWheelMoved(e);
2945 * Called when an input method event is dispatched and component events are
2946 * enabled. This method passes the event along to any listeners that are
2947 * attached.
2949 * @param event the <code>InputMethodEvent</code> to process
2950 * @throws NullPointerException if e is null
2951 * @see InputMethodListener
2952 * @see #addInputMethodListener(InputMethodListener)
2953 * @see #enableEvents(long)
2954 * @since 1.2
2956 protected void processInputMethodEvent(InputMethodEvent e)
2958 if (inputMethodListener == null)
2959 return;
2960 switch (e.id)
2962 case InputMethodEvent.CARET_POSITION_CHANGED:
2963 inputMethodListener.caretPositionChanged(e);
2964 break;
2965 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
2966 inputMethodListener.inputMethodTextChanged(e);
2967 break;
2972 * Called when a hierarchy change event is dispatched and component events
2973 * are enabled. This method passes the event along to any listeners that are
2974 * attached.
2976 * @param event the <code>HierarchyEvent</code> to process
2977 * @throws NullPointerException if e is null
2978 * @see HierarchyListener
2979 * @see #addHierarchyListener(HierarchyListener)
2980 * @see #enableEvents(long)
2981 * @since 1.3
2983 protected void processHierarchyEvent(HierarchyEvent e)
2985 if (hierarchyListener == null)
2986 return;
2987 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2988 hierarchyListener.hierarchyChanged(e);
2992 * Called when a hierarchy bounds event is dispatched and component events
2993 * are enabled. This method passes the event along to any listeners that are
2994 * attached.
2996 * @param event the <code>HierarchyEvent</code> to process
2997 * @throws NullPointerException if e is null
2998 * @see HierarchyBoundsListener
2999 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
3000 * @see #enableEvents(long)
3001 * @since 1.3
3003 protected void processHierarchyBoundsEvent(HierarchyEvent e)
3005 if (hierarchyBoundsListener == null)
3006 return;
3007 switch (e.id)
3009 case HierarchyEvent.ANCESTOR_MOVED:
3010 hierarchyBoundsListener.ancestorMoved(e);
3011 break;
3012 case HierarchyEvent.ANCESTOR_RESIZED:
3013 hierarchyBoundsListener.ancestorResized(e);
3014 break;
3019 * AWT 1.0 event processor.
3021 * @param evt the event to handle
3022 * @return false: since the method was deprecated, the return has no meaning
3023 * @deprecated use {@link #processEvent(AWTEvent)} instead
3025 public boolean handleEvent(Event evt)
3027 // XXX Add backward compatibility handling.
3028 return false;
3032 * AWT 1.0 mouse event.
3034 * @param evt the event to handle
3035 * @param x the x coordinate, ignored
3036 * @param y the y coordinate, ignored
3037 * @return false: since the method was deprecated, the return has no meaning
3038 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3040 public boolean mouseDown(Event evt, int x, int y)
3042 // XXX Add backward compatibility handling.
3043 return false;
3047 * AWT 1.0 mouse event.
3049 * @param evt the event to handle
3050 * @param x the x coordinate, ignored
3051 * @param y the y coordinate, ignored
3052 * @return false: since the method was deprecated, the return has no meaning
3053 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3055 public boolean mouseDrag(Event evt, int x, int y)
3057 // XXX Add backward compatibility handling.
3058 return false;
3062 * AWT 1.0 mouse event.
3064 * @param evt the event to handle
3065 * @param x the x coordinate, ignored
3066 * @param y the y coordinate, ignored
3067 * @return false: since the method was deprecated, the return has no meaning
3068 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3070 public boolean mouseUp(Event evt, int x, int y)
3072 // XXX Add backward compatibility handling.
3073 return false;
3077 * AWT 1.0 mouse event.
3079 * @param evt the event to handle
3080 * @param x the x coordinate, ignored
3081 * @param y the y coordinate, ignored
3082 * @return false: since the method was deprecated, the return has no meaning
3083 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3085 public boolean mouseMove(Event evt, int x, int y)
3087 // XXX Add backward compatibility handling.
3088 return false;
3092 * AWT 1.0 mouse event.
3094 * @param evt the event to handle
3095 * @param x the x coordinate, ignored
3096 * @param y the y coordinate, ignored
3097 * @return false: since the method was deprecated, the return has no meaning
3098 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3100 public boolean mouseEnter(Event evt, int x, int y)
3102 // XXX Add backward compatibility handling.
3103 return false;
3107 * AWT 1.0 mouse event.
3109 * @param evt the event to handle
3110 * @param x the x coordinate, ignored
3111 * @param y the y coordinate, ignored
3112 * @return false: since the method was deprecated, the return has no meaning
3113 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3115 public boolean mouseExit(Event evt, int x, int y)
3117 // XXX Add backward compatibility handling.
3118 return false;
3122 * AWT 1.0 key press event.
3124 * @param evt the event to handle
3125 * @param key the key pressed, ignored
3126 * @return false: since the method was deprecated, the return has no meaning
3127 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3129 public boolean keyDown(Event evt, int key)
3131 // XXX Add backward compatibility handling.
3132 return false;
3136 * AWT 1.0 key press event.
3138 * @param evt the event to handle
3139 * @param key the key pressed, ignored
3140 * @return false: since the method was deprecated, the return has no meaning
3141 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3143 public boolean keyUp(Event evt, int key)
3145 // XXX Add backward compatibility handling.
3146 return false;
3150 * AWT 1.0 action event processor.
3152 * @param evt the event to handle
3153 * @param what the object acted on, ignored
3154 * @return false: since the method was deprecated, the return has no meaning
3155 * @deprecated in classes which support actions, use
3156 * <code>processActionEvent(ActionEvent)</code> instead
3158 public boolean action(Event evt, Object what)
3160 // XXX Add backward compatibility handling.
3161 return false;
3165 * Called to inform this component it has been added to a container.
3166 * A native peer - if any - is created at this time. This method is
3167 * called automatically by the AWT system and should not be called by
3168 * user level code.
3170 * @see #isDisplayable()
3171 * @see #removeNotify()
3173 public void addNotify()
3175 if (peer == null)
3176 peer = getToolkit().createComponent(this);
3177 /* Now that all the children has gotten their peers, we should
3178 have the event mask needed for this component and its
3179 lightweight subcomponents. */
3180 peer.setEventMask(eventMask);
3181 /* We do not invalidate here, but rather leave that job up to
3182 the peer. For efficiency, the peer can choose not to
3183 invalidate if it is happy with the current dimensions,
3184 etc. */
3188 * Called to inform this component is has been removed from its
3189 * container. Its native peer - if any - is destroyed at this time.
3190 * This method is called automatically by the AWT system and should
3191 * not be called by user level code.
3193 * @see #isDisplayable()
3194 * @see #addNotify()
3196 public void removeNotify()
3198 if (peer != null)
3199 peer.dispose();
3200 peer = null;
3204 * AWT 1.0 focus event.
3206 * @param evt the event to handle
3207 * @param what the Object focused, ignored
3208 * @return false: since the method was deprecated, the return has no meaning
3209 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3211 public boolean gotFocus(Event evt, Object what)
3213 // XXX Add backward compatibility handling.
3214 return false;
3218 * AWT 1.0 focus event.
3220 * @param evt the event to handle
3221 * @param what the Object focused, ignored
3222 * @return false: since the method was deprecated, the return has no meaning
3223 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3225 public boolean lostFocus(Event evt, Object what)
3227 // XXX Add backward compatibility handling.
3228 return false;
3232 * Tests whether or not this component is in the group that can be
3233 * traversed using the keyboard traversal mechanism (such as the TAB key).
3235 * @return true if the component is traversed via the TAB key
3236 * @see #setFocusable(boolean)
3237 * @since 1.1
3238 * @deprecated use {@link #isFocusable()} instead
3240 public boolean isFocusTraversable()
3242 return enabled && visible && (peer == null || peer.isFocusTraversable());
3246 * Tests if this component can receive focus.
3248 * @return true if this component can receive focus
3249 * @since 1.4
3251 public boolean isFocusable()
3253 return focusable;
3257 * Specify whether this component can receive focus.
3259 * @param focusable the new focusable status
3260 * @since 1.4
3262 public void setFocusable(boolean focusable)
3264 firePropertyChange("focusable", this.focusable, focusable);
3265 this.focusable = focusable;
3269 * Sets the focus traversal keys for a given type of focus events. Normally,
3270 * the default values should match the operating system's native choices. To
3271 * disable a given traversal, use <code>Collections.EMPTY_SET</code>. The
3272 * event dispatcher will consume PRESSED, RELEASED, and TYPED events for the
3273 * specified key, although focus can only transfer on PRESSED or RELEASED.
3275 * <p>The defauts are:
3276 * <table>
3277 * <th><td>Identifier</td><td>Meaning</td><td>Default</td></th>
3278 * <tr><td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
3279 * <td>Normal forward traversal</td>
3280 * <td>TAB on KEY_PRESSED, Ctrl-TAB on KEY_PRESSED</td></tr>
3281 * <tr><td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
3282 * <td>Normal backward traversal</td>
3283 * <td>Shift-TAB on KEY_PRESSED, Ctrl-Shift-TAB on KEY_PRESSED</td></tr>
3284 * <tr><td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
3285 * <td>Go up a traversal cycle</td><td>None</td></tr>
3286 * </table>
3288 * <p>Specifying null allows inheritance from the parent, or from the current
3289 * KeyboardFocusManager default set. If not null, the set must contain only
3290 * AWTKeyStrokes that are not already focus keys and are not KEY_TYPED
3291 * events.
3293 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3294 * UP_CYCLE_TRAVERSAL_KEYS
3295 * @param keystrokes a set of keys, or null
3296 * @throws IllegalArgumentException if id or keystrokes is invalid
3297 * @see #getFocusTraversalKeys(int)
3298 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3299 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3300 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3301 * @since 1.4
3303 public void setFocusTraversalKeys(int id, Set keystrokes)
3305 if (keystrokes == null)
3306 throw new IllegalArgumentException();
3307 Set sa;
3308 Set sb;
3309 String name;
3310 switch (id)
3312 case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
3313 sa = getFocusTraversalKeys
3314 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3315 sb = getFocusTraversalKeys
3316 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3317 name = "forwardFocusTraversalKeys";
3318 break;
3319 case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
3320 sa = getFocusTraversalKeys
3321 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3322 sb = getFocusTraversalKeys
3323 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3324 name = "backwardFocusTraversalKeys";
3325 break;
3326 case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
3327 sa = getFocusTraversalKeys
3328 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3329 sb = getFocusTraversalKeys
3330 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3331 name = "upCycleFocusTraversalKeys";
3332 break;
3333 default:
3334 throw new IllegalArgumentException();
3336 int i = keystrokes.size();
3337 Iterator iter = keystrokes.iterator();
3338 while (--i >= 0)
3340 Object o = iter.next();
3341 if (! (o instanceof AWTKeyStroke)
3342 || sa.contains(o) || sb.contains(o)
3343 || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
3344 throw new IllegalArgumentException();
3346 if (focusTraversalKeys == null)
3347 focusTraversalKeys = new Set[3];
3348 keystrokes = Collections.unmodifiableSet(new HashSet(keystrokes));
3349 firePropertyChange(name, focusTraversalKeys[id], keystrokes);
3350 focusTraversalKeys[id] = keystrokes;
3354 * Returns the set of keys for a given focus traversal action, as defined
3355 * in <code>setFocusTraversalKeys</code>. If not set, this is inherited from
3356 * the parent component, which may have gotten it from the
3357 * KeyboardFocusManager.
3359 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3360 * UP_CYCLE_TRAVERSAL_KEYS
3361 * @throws IllegalArgumentException if id is invalid
3362 * @see #setFocusTraversalKeys(int, Set)
3363 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3364 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3365 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3366 * @since 1.4
3368 public Set getFocusTraversalKeys(int id)
3370 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3371 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3372 throw new IllegalArgumentException();
3373 Set s = null;
3374 if (focusTraversalKeys != null)
3375 s = focusTraversalKeys[id];
3376 if (s == null && parent != null)
3377 s = parent.getFocusTraversalKeys(id);
3378 return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
3379 .getDefaultFocusTraversalKeys(id)) : s;
3383 * Tests whether the focus traversal keys for a given action are explicitly
3384 * set or inherited.
3386 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3387 * UP_CYCLE_TRAVERSAL_KEYS
3388 * @return true if that set is explicitly specified
3389 * @throws IllegalArgumentException if id is invalid
3390 * @see #getFocusTraversalKeys(int)
3391 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3392 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3393 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3394 * @since 1.4
3396 public boolean areFocusTraversalKeysSet(int id)
3398 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3399 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3400 throw new IllegalArgumentException();
3401 return focusTraversalKeys != null && focusTraversalKeys[id] != null;
3405 * Sets whether focus traversal keys are enabled, which consumes traversal
3406 * keys and performs the focus event automatically.
3408 * @param focusTraversalKeysEnabled the new value of the flag
3409 * @see #getFocusTraversalKeysEnabled()
3410 * @see #setFocusTraversalKeys(int, Set)
3411 * @see #getFocusTraversalKeys(int)
3412 * @since 1.4
3414 public void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)
3416 firePropertyChange("focusTraversalKeysEnabled",
3417 this.focusTraversalKeysEnabled,
3418 focusTraversalKeysEnabled);
3419 this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
3423 * Tests whether focus traversal keys are enabled. If they are, then focus
3424 * traversal keys are consumed and focus events performed automatically,
3425 * without the component seeing the keystrokes.
3427 * @return true if focus traversal is enabled
3428 * @see #setFocusTraversalKeysEnabled(boolean)
3429 * @see #setFocusTraversalKeys(int, Set)
3430 * @see #getFocusTraversalKeys(int)
3431 * @since 1.4
3433 public boolean getFocusTraversalKeysEnabled()
3435 return focusTraversalKeysEnabled;
3439 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3440 * event will be fired if and only if this request is successful. To be
3441 * successful, the component must be displayable, visible, and focusable,
3442 * and the top-level Window must be able to receive focus. Thus, this
3443 * request may fail, or be delayed until the window receives focus. It is
3444 * recommended that <code>requestFocusInWindow</code> be used where
3445 * possible to be more platform-independent.
3447 * @see #requestFocusInWindow()
3448 * @see FocusEvent
3449 * @see #addFocusListener(FocusListener)
3450 * @see #isFocusable()
3451 * @see #isDisplayable()
3452 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3454 public void requestFocus()
3456 // If there's no peer then this component can't get the focus. We
3457 // treat it as a silent rejection of the request.
3458 if (peer != null)
3459 peer.requestFocus();
3463 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3464 * event will be fired if and only if this request is successful. To be
3465 * successful, the component must be displayable, visible, and focusable,
3466 * and the top-level Window must be able to receive focus. Thus, this
3467 * request may fail, or be delayed until the window receives focus. It is
3468 * recommended that <code>requestFocusInWindow</code> be used where
3469 * possible to be more platform-independent.
3471 * <p>If the return value is false, the request is guaranteed to fail. If
3472 * it is true, it will likely succeed unless the action is vetoed or
3473 * something in the native windowing system intervenes. The temporary flag,
3474 * and thus this method in general, is not designed for public use; rather
3475 * it is a hook for lightweight components to notify their container in
3476 * an attempt to reduce the amount of repainting necessary.
3478 * @param temporary true if the focus request is temporary
3479 * @return true if the request has a chance of success
3480 * @see #requestFocusInWindow()
3481 * @see FocusEvent
3482 * @see #addFocusListener(FocusListener)
3483 * @see #isFocusable()
3484 * @see #isDisplayable()
3485 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3486 * @since 1.4
3488 protected boolean requestFocus(boolean temporary)
3490 // XXX Implement correctly.
3491 requestFocus();
3492 return true;
3496 * Requests that this component be given focus, if it resides in the
3497 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3498 * event will be fired if and only if this request is successful. To be
3499 * successful, the component must be displayable, visible, and focusable,
3500 * and the top-level Window must be focused.
3502 * <p>If the return value is false, the request is guaranteed to fail. If
3503 * it is true, it will likely succeed unless the action is vetoed or
3504 * something in the native windowing system intervenes. The temporary flag,
3505 * and thus this method in general, is not designed for public use; rather
3506 * it is a hook for lightweight components to notify their container in
3507 * an attempt to reduce the amount of repainting necessary.
3509 * @return true if the request has a chance of success
3510 * @see #requestFocus()
3511 * @see FocusEvent
3512 * @see #addFocusListener(FocusListener)
3513 * @see #isFocusable()
3514 * @see #isDisplayable()
3515 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3516 * @since 1.4
3518 public boolean requestFocusInWindow()
3520 // XXX Implement correctly.
3521 requestFocus();
3522 return true;
3526 * Requests that this component be given focus, if it resides in the
3527 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3528 * event will be fired if and only if this request is successful. To be
3529 * successful, the component must be displayable, visible, and focusable,
3530 * and the top-level Window must be focused.
3532 * <p>If the return value is false, the request is guaranteed to fail. If
3533 * it is true, it will likely succeed unless the action is vetoed or
3534 * something in the native windowing system intervenes. The temporary flag,
3535 * and thus this method in general, is not designed for public use; rather
3536 * it is a hook for lightweight components to notify their container in
3537 * an attempt to reduce the amount of repainting necessary.
3539 * @param temporary true if the focus request is temporary
3540 * @return true if the request has a chance of success
3541 * @see #requestFocus()
3542 * @see FocusEvent
3543 * @see #addFocusListener(FocusListener)
3544 * @see #isFocusable()
3545 * @see #isDisplayable()
3546 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3547 * @since 1.4
3549 protected boolean requestFocusInWindow(boolean temporary)
3551 // XXX Implement correctly.
3552 requestFocus();
3553 return true;
3557 * Transfers focus to the next component in the focus traversal order, as
3558 * though this were the current focus owner.
3560 * @see #requestFocus()
3561 * @since 1.1
3563 public void transferFocus()
3565 Component next;
3566 if (parent == null)
3567 next = findNextFocusComponent(null);
3568 else
3569 next = parent.findNextFocusComponent(this);
3570 if (next != null && next != this)
3571 next.requestFocus();
3575 * Returns the root container that owns the focus cycle where this component
3576 * resides. A focus cycle root is in two cycles, one as the ancestor, and
3577 * one as the focusable element; this call always returns the ancestor.
3579 * @return the ancestor container that owns the focus cycle
3580 * @since 1.4
3582 public Container getFocusCycleRootAncestor()
3584 // XXX Implement.
3585 throw new Error("not implemented");
3589 * Tests if the container is the ancestor of the focus cycle that this
3590 * component belongs to.
3592 * @param c the container to test
3593 * @return true if c is the focus cycle root
3594 * @since 1.4
3596 public boolean isFocusCycleRoot(Container c)
3598 return c == getFocusCycleRootAncestor();
3602 * AWT 1.0 focus event processor.
3604 * @deprecated use {@link #transferFocus()} instead
3606 public void nextFocus()
3608 transferFocus();
3612 * Transfers focus to the previous component in the focus traversal order, as
3613 * though this were the current focus owner.
3615 * @see #requestFocus()
3616 * @since 1.4
3618 public void transferFocusBackward()
3620 // XXX Implement.
3621 throw new Error("not implemented");
3625 * Transfers focus to the focus cycle root of this component. However, if
3626 * this is a Window, the default focus owner in the window in the current
3627 * focus cycle is focused instead.
3629 * @see #requestFocus()
3630 * @see #isFocusCycleRoot()
3631 * @since 1.4
3633 public void transferFocusUpCycle()
3635 // XXX Implement.
3636 throw new Error("not implemented");
3640 * Tests if this component is the focus owner. Use {@link #isFocusOwner()}
3641 * instead.
3643 * @return true if this component owns focus
3644 * @since 1.2
3646 public boolean hasFocus()
3648 return isFocusOwner();
3652 * Tests if this component is the focus owner.
3654 * @return true if this component owns focus
3655 * @since 1.4
3657 public boolean isFocusOwner()
3659 // XXX Implement.
3660 throw new Error("not implemented");
3664 * Adds the specified popup menu to this component.
3666 * @param menu the popup menu to be added
3667 * @see #remove(MenuComponent)
3668 * @since 1.1
3670 public synchronized void add(PopupMenu popup)
3672 if (popups == null)
3673 popups = new Vector();
3674 popups.add(popup);
3678 * Removes the specified popup menu from this component.
3680 * @param menu the popup menu to remove
3681 * @see #add(PopupMenu)
3682 * @since 1.1
3684 public synchronized void remove(MenuComponent popup)
3686 if (popups != null)
3687 popups.remove(popup);
3691 * Returns a debugging string representing this component. The string may
3692 * be empty but not null.
3694 * @return a string representing this component
3696 protected String paramString()
3698 StringBuffer param = new StringBuffer();
3699 String name = getName();
3700 if (name != null)
3701 param.append(name).append(",");
3702 param.append(width).append("x").append(height).append("+").append(x)
3703 .append("+").append(y);
3704 if (! isValid())
3705 param.append(",invalid");
3706 if (! isVisible())
3707 param.append(",invisible");
3708 if (! isEnabled())
3709 param.append(",disabled");
3710 if (! isOpaque())
3711 param.append(",translucent");
3712 if (isDoubleBuffered())
3713 param.append(",doublebuffered");
3714 return param.toString();
3718 * Returns a string representation of this component. This is implemented
3719 * as <code>getClass().getName() + '[' + paramString() + ']'</code>.
3721 * @return a string representation of this component
3723 public String toString()
3725 return getClass().getName() + '[' + paramString() + ']';
3729 * Prints a listing of this component to <code>System.out</code>.
3731 * @see #list(PrintStream)
3733 public void list()
3735 list(System.out, 0);
3739 * Prints a listing of this component to the specified print stream.
3741 * @param stream the <code>PrintStream</code> to print to
3743 public void list(PrintStream out)
3745 list(out, 0);
3749 * Prints a listing of this component to the specified print stream,
3750 * starting at the specified indentation point.
3752 * @param stream the <code>PrintStream</code> to print to
3753 * @param indent the indentation point
3755 public void list(PrintStream out, int indent)
3757 for (int i = 0; i < indent; ++i)
3758 out.print(' ');
3759 out.println(toString());
3763 * Prints a listing of this component to the specified print writer.
3765 * @param writer the <code>PrintWrinter</code> to print to
3766 * @since 1.1
3768 public void list(PrintWriter out)
3770 list(out, 0);
3774 * Prints a listing of this component to the specified print writer,
3775 * starting at the specified indentation point.
3777 * @param writer the <code>PrintWriter</code> to print to
3778 * @param indent the indentation point
3779 * @since 1.1
3781 public void list(PrintWriter out, int indent)
3783 for (int i = 0; i < indent; ++i)
3784 out.print(' ');
3785 out.println(toString());
3789 * Adds the specified property listener to this component. This is harmless
3790 * if the listener is null, but if the listener has already been registered,
3791 * it will now be registered twice. The property listener ignores inherited
3792 * properties. Recognized properties include:<br>
3793 * <ul>
3794 * <li>the font (<code>"font"</code>)</li>
3795 * <li>the background color (<code>"background"</code>)</li>
3796 * <li>the foreground color (<code>"foreground"</code>)</li>
3797 * <li>the focusability (<code>"focusable"</code>)</li>
3798 * <li>the focus key traversal enabled state
3799 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3800 * <li>the set of forward traversal keys
3801 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3802 * <li>the set of backward traversal keys
3803 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3804 * <li>the set of up-cycle traversal keys
3805 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3806 * </ul>
3808 * @param listener the new listener to add
3809 * @see #removePropertyChangeListener(PropertyChangeListener)
3810 * @see #getPropertyChangeListeners()
3811 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3812 * @since 1.1
3814 public void addPropertyChangeListener(PropertyChangeListener listener)
3816 if (changeSupport == null)
3817 changeSupport = new PropertyChangeSupport(this);
3818 changeSupport.addPropertyChangeListener(listener);
3822 * Removes the specified property listener from the component. This is
3823 * harmless if the listener was not previously registered.
3825 * @param listener the listener to remove
3826 * @see #addPropertyChangeListener(PropertyChangeListener)
3827 * @see #getPropertyChangeListeners()
3828 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3829 * @since 1.1
3831 public void removePropertyChangeListener(PropertyChangeListener listener)
3833 if (changeSupport != null)
3834 changeSupport.removePropertyChangeListener(listener);
3838 * Returns an array of all specified listeners registered on this component.
3840 * @return an array of listeners
3841 * @see #addPropertyChangeListener(PropertyChangeListener)
3842 * @see #removePropertyChangeListener(PropertyChangeListener)
3843 * @see #getPropertyChangeListeners(String)
3844 * @since 1.4
3846 public PropertyChangeListener[] getPropertyChangeListeners()
3848 return changeSupport == null ? new PropertyChangeListener[0]
3849 : changeSupport.getPropertyChangeListeners();
3853 * Adds the specified property listener to this component. This is harmless
3854 * if the listener is null, but if the listener has already been registered,
3855 * it will now be registered twice. The property listener ignores inherited
3856 * properties. The listener is keyed to a single property. Recognized
3857 * properties include:<br>
3858 * <ul>
3859 * <li>the font (<code>"font"</code>)</li>
3860 * <li>the background color (<code>"background"</code>)</li>
3861 * <li>the foreground color (<code>"foreground"</code>)</li>
3862 * <li>the focusability (<code>"focusable"</code>)</li>
3863 * <li>the focus key traversal enabled state
3864 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3865 * <li>the set of forward traversal keys
3866 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3867 p * <li>the set of backward traversal keys
3868 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3869 * <li>the set of up-cycle traversal keys
3870 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3871 * </ul>
3873 * @param propertyName the property name to filter on
3874 * @param listener the new listener to add
3875 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3876 * @see #getPropertyChangeListeners(String)
3877 * @see #addPropertyChangeListener(PropertyChangeListener)
3878 * @since 1.1
3880 public void addPropertyChangeListener(String propertyName,
3881 PropertyChangeListener listener)
3883 if (changeSupport == null)
3884 changeSupport = new PropertyChangeSupport(this);
3885 changeSupport.addPropertyChangeListener(propertyName, listener);
3889 * Removes the specified property listener on a particular property from
3890 * the component. This is harmless if the listener was not previously
3891 * registered.
3893 * @param propertyName the property name to filter on
3894 * @param listener the listener to remove
3895 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3896 * @see #getPropertyChangeListeners(String)
3897 * @see #removePropertyChangeListener(PropertyChangeListener)
3898 * @since 1.1
3900 public void removePropertyChangeListener(String propertyName,
3901 PropertyChangeListener listener)
3903 if (changeSupport != null)
3904 changeSupport.removePropertyChangeListener(propertyName, listener);
3908 * Returns an array of all specified listeners on the named property that
3909 * are registered on this component.
3911 * @return an array of listeners
3912 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3913 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3914 * @see #getPropertyChangeListeners()
3915 * @since 1.4
3917 public PropertyChangeListener[] getPropertyChangeListeners(String property)
3919 return changeSupport == null ? new PropertyChangeListener[0]
3920 : changeSupport.getPropertyChangeListeners(property);
3924 * Report a change in a bound property to any registered property listeners.
3926 * @param propertyName the property that changed
3927 * @param oldValue the old property value
3928 * @param newValue the new property value
3930 protected void firePropertyChange(String propertyName, Object oldValue,
3931 Object newValue)
3933 if (changeSupport != null)
3934 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3938 * Report a change in a bound property to any registered property listeners.
3940 * @param propertyName the property that changed
3941 * @param oldValue the old property value
3942 * @param newValue the new property value
3944 protected void firePropertyChange(String propertyName, boolean oldValue,
3945 boolean newValue)
3947 if (changeSupport != null)
3948 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3952 * Report a change in a bound property to any registered property listeners.
3954 * @param propertyName the property that changed
3955 * @param oldValue the old property value
3956 * @param newValue the new property value
3958 protected void firePropertyChange(String propertyName, int oldValue,
3959 int newValue)
3961 if (changeSupport != null)
3962 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3966 * Sets the text layout orientation of this component. New components default
3967 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects only
3968 * the current component, while
3969 * {@link #applyComponentOrientation(ComponentOrientation)} affects the
3970 * entire hierarchy.
3972 * @param o the new orientation
3973 * @throws NullPointerException if o is null
3974 * @see #getComponentOrientation()
3976 public void setComponentOrientation(ComponentOrientation o)
3978 if (o == null)
3979 throw new NullPointerException();
3980 orientation = o;
3984 * Determines the text layout orientation used by this component.
3986 * @return the component orientation
3987 * @see #setComponentOrientation(ComponentOrientation)
3989 public ComponentOrientation getComponentOrientation()
3991 return orientation;
3995 * Sets the text layout orientation of this component. New components default
3996 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects the
3997 * entire hierarchy, while
3998 * {@link #setComponentOrientation(ComponentOrientation)} affects only the
3999 * current component.
4001 * @param o the new orientation
4002 * @throws NullPointerException if o is null
4003 * @see #getComponentOrientation()
4004 * @since 1.4
4006 public void applyComponentOrientation(ComponentOrientation o)
4008 setComponentOrientation(o);
4012 * Returns the accessibility framework context of this class. Component is
4013 * not accessible, so the default implementation returns null. Subclasses
4014 * must override this behavior, and return an appropriate subclass of
4015 * {@link AccessibleAWTComponent}.
4017 * @return the accessibility context
4019 public AccessibleContext getAccessibleContext()
4021 return null;
4025 // Helper methods; some are package visible for use by subclasses.
4028 * Subclasses should override this to return unique component names like
4029 * "menuitem0".
4031 * @return the generated name for this component
4033 String generateName()
4035 // Component is abstract.
4036 return null;
4040 * Sets the peer for this component.
4042 * @param peer the new peer
4044 final void setPeer(ComponentPeer peer)
4046 this.peer = peer;
4050 * Implementation method that allows classes such as Canvas and Window to
4051 * override the graphics configuration without violating the published API.
4053 * @return the graphics configuration
4055 GraphicsConfiguration getGraphicsConfigurationImpl()
4057 if (peer != null)
4059 GraphicsConfiguration config = peer.getGraphicsConfiguration();
4060 if (config != null)
4061 return config;
4064 if (parent != null)
4065 return parent.getGraphicsConfiguration();
4067 return null;
4071 * Implementation of dispatchEvent. Allows trusted package classes to
4072 * dispatch additional events first.
4074 * @param e the event to dispatch
4076 void dispatchEventImpl(AWTEvent e)
4078 // Make use of event id's in order to avoid multiple instanceof tests.
4079 if (e.id <= ComponentEvent.COMPONENT_LAST
4080 && e.id >= ComponentEvent.COMPONENT_FIRST
4081 && (componentListener != null
4082 || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0))
4083 processEvent(e);
4084 else if (e.id <= KeyEvent.KEY_LAST
4085 && e.id >= KeyEvent.KEY_FIRST
4086 && (keyListener != null
4087 || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0))
4088 processEvent(e);
4089 else if (e.id <= MouseEvent.MOUSE_LAST
4090 && e.id >= MouseEvent.MOUSE_FIRST
4091 && (mouseListener != null
4092 || mouseMotionListener != null
4093 || (eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0))
4094 processEvent(e);
4095 else if (e.id <= FocusEvent.FOCUS_LAST
4096 && e.id >= FocusEvent.FOCUS_FIRST
4097 && (focusListener != null
4098 || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0))
4099 processEvent(e);
4100 else if (e.id <= InputMethodEvent.INPUT_METHOD_LAST
4101 && e.id >= InputMethodEvent.INPUT_METHOD_FIRST
4102 && (inputMethodListener != null
4103 || (eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0))
4104 processEvent(e);
4105 else if (e.id <= HierarchyEvent.HIERARCHY_LAST
4106 && e.id >= HierarchyEvent.HIERARCHY_FIRST
4107 && (hierarchyListener != null
4108 || hierarchyBoundsListener != null
4109 || (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0))
4110 processEvent(e);
4111 else if (e.id <= PaintEvent.PAINT_LAST
4112 && e.id >= PaintEvent.PAINT_FIRST
4113 && (eventMask & AWTEvent.PAINT_EVENT_MASK) != 0)
4114 processEvent(e);
4118 * Coalesce paint events. Current heuristic is: Merge if the union of
4119 * areas is less than twice that of the sum of the areas. The X server
4120 * tend to create a lot of paint events that are adjacent but not
4121 * overlapping.
4123 * <pre>
4124 * +------+
4125 * | +-----+ ...will be merged
4126 * | | |
4127 * | | |
4128 * +------+ |
4129 * +-----+
4131 * +---------------+--+
4132 * | | | ...will not be merged
4133 * +---------------+ |
4134 * | |
4135 * | |
4136 * | |
4137 * | |
4138 * | |
4139 * +--+
4140 * </pre>
4142 * @param queuedEvent the first paint event
4143 * @param newEvent the second paint event
4144 * @return the combined paint event, or null
4146 private PaintEvent coalescePaintEvents(PaintEvent queuedEvent,
4147 PaintEvent newEvent)
4149 Rectangle r1 = queuedEvent.getUpdateRect();
4150 Rectangle r2 = newEvent.getUpdateRect();
4151 Rectangle union = r1.union(r2);
4153 int r1a = r1.width * r1.height;
4154 int r2a = r2.width * r2.height;
4155 int ua = union.width * union.height;
4157 if (ua > (r1a+r2a)*2)
4158 return null;
4159 /* The 2 factor should maybe be reconsidered. Perhaps 3/2
4160 would be better? */
4162 newEvent.setUpdateRect(union);
4163 return newEvent;
4167 * Does the work for a paint event.
4169 * @param event the event to process
4171 private void processPaintEvent(PaintEvent event)
4173 // Can't do graphics without peer
4174 if (peer == null)
4175 return;
4177 Graphics gfx = getGraphics();
4180 Shape clip = event.getUpdateRect();
4181 gfx.setClip(clip);
4183 switch (event.id)
4185 case PaintEvent.PAINT:
4186 paint(gfx);
4187 break;
4188 case PaintEvent.UPDATE:
4189 update(gfx);
4190 break;
4191 default:
4192 throw new IllegalArgumentException("unknown paint event");
4195 finally
4197 gfx.dispose();
4202 * This method is used to implement transferFocus(). CHILD is the child
4203 * making the request. This is overridden by Container; when called for an
4204 * ordinary component there is no child and so we always return null.
4206 * @param child the component making the request
4207 * @return the next component to focus on
4209 Component findNextFocusComponent(Component child)
4211 return null;
4215 * Deserializes this component. This regenerates all serializable listeners
4216 * which were registered originally.
4218 * @param s the stream to read from
4219 * @throws ClassNotFoundException if deserialization fails
4220 * @throws IOException if the stream fails
4222 private void readObject(ObjectInputStream s)
4223 throws ClassNotFoundException, IOException
4225 s.defaultReadObject();
4226 String key = (String) s.readObject();
4227 while (key != null)
4229 Object listener = s.readObject();
4230 if ("componentL".equals(key))
4231 addComponentListener((ComponentListener) listener);
4232 else if ("focusL".equals(key))
4233 addFocusListener((FocusListener) listener);
4234 else if ("keyL".equals(key))
4235 addKeyListener((KeyListener) listener);
4236 else if ("mouseL".equals(key))
4237 addMouseListener((MouseListener) listener);
4238 else if ("mouseMotionL".equals(key))
4239 addMouseMotionListener((MouseMotionListener) listener);
4240 else if ("inputMethodL".equals(key))
4241 addInputMethodListener((InputMethodListener) listener);
4242 else if ("hierarchyL".equals(key))
4243 addHierarchyListener((HierarchyListener) listener);
4244 else if ("hierarchyBoundsL".equals(key))
4245 addHierarchyBoundsListener((HierarchyBoundsListener) listener);
4246 else if ("mouseWheelL".equals(key))
4247 addMouseWheelListener((MouseWheelListener) listener);
4248 key = (String) s.readObject();
4253 * Serializes this component. This ignores all listeners which do not
4254 * implement Serializable, but includes those that do.
4256 * @param s the stream to write to
4257 * @throws IOException if the stream fails
4259 private void writeObject(ObjectOutputStream s) throws IOException
4261 s.defaultWriteObject();
4262 AWTEventMulticaster.save(s, "componentL", componentListener);
4263 AWTEventMulticaster.save(s, "focusL", focusListener);
4264 AWTEventMulticaster.save(s, "keyL", keyListener);
4265 AWTEventMulticaster.save(s, "mouseL", mouseListener);
4266 AWTEventMulticaster.save(s, "mouseMotionL", mouseMotionListener);
4267 AWTEventMulticaster.save(s, "inputMethodL", inputMethodListener);
4268 AWTEventMulticaster.save(s, "hierarchyL", hierarchyListener);
4269 AWTEventMulticaster.save(s, "hierarchyBoundsL", hierarchyBoundsListener);
4270 AWTEventMulticaster.save(s, "mouseWheelL", mouseWheelListener);
4271 s.writeObject(null);
4275 // Nested classes.
4278 * This class provides accessibility support for subclasses of container.
4280 * @author Eric Blake <ebb9@email.byu.edu>
4281 * @since 1.3
4282 * @status updated to 1.4
4284 protected abstract class AccessibleAWTComponent extends AccessibleContext
4285 implements Serializable, AccessibleComponent
4288 * Compatible with JDK 1.3+.
4290 private static final long serialVersionUID = 642321655757800191L;
4293 * Converts show/hide events to PropertyChange events, and is registered
4294 * as a component listener on this component.
4296 * @serial the component handler
4298 protected ComponentListener accessibleAWTComponentHandler
4299 = new AccessibleAWTComponentHandler();
4302 * Converts focus events to PropertyChange events, and is registered
4303 * as a focus listener on this component.
4305 * @serial the focus handler
4307 protected FocusListener accessibleAWTFocusHandler
4308 = new AccessibleAWTFocusHandler();
4311 * The default constructor.
4313 protected AccessibleAWTComponent()
4315 Component.this.addComponentListener(accessibleAWTComponentHandler);
4316 Component.this.addFocusListener(accessibleAWTFocusHandler);
4320 * Adds a global property change listener to the accessible component.
4322 * @param l the listener to add
4323 * @see #ACCESSIBLE_NAME_PROPERTY
4324 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
4325 * @see #ACCESSIBLE_STATE_PROPERTY
4326 * @see #ACCESSIBLE_VALUE_PROPERTY
4327 * @see #ACCESSIBLE_SELECTION_PROPERTY
4328 * @see #ACCESSIBLE_TEXT_PROPERTY
4329 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
4331 public void addPropertyChangeListener(PropertyChangeListener l)
4333 Component.this.addPropertyChangeListener(l);
4334 super.addPropertyChangeListener(l);
4338 * Removes a global property change listener from this accessible
4339 * component.
4341 * @param l the listener to remove
4343 public void removePropertyChangeListener(PropertyChangeListener l)
4345 Component.this.removePropertyChangeListener(l);
4346 super.removePropertyChangeListener(l);
4350 * Returns the accessible name of this component. It is almost always
4351 * wrong to return getName(), since it is not localized. In fact, for
4352 * things like buttons, this should be the text of the button, not the
4353 * name of the object. The tooltip text might also be appropriate.
4355 * @return the name
4356 * @see #setAccessibleName(String)
4358 public String getAccessibleName()
4360 return accessibleName == null ? getName() : accessibleName;
4364 * Returns a brief description of this accessible context. This should
4365 * be localized.
4367 * @return a description of this component
4368 * @see #setAccessibleDescription(String)
4370 public String getAccessibleDescription()
4372 return accessibleDescription;
4376 * Returns the role of this component.
4378 * @return the accessible role
4380 public AccessibleRole getAccessibleRole()
4382 return AccessibleRole.AWT_COMPONENT;
4386 * Returns a state set describing this component's state.
4388 * @return a new state set
4389 * @see AccessibleState
4391 public AccessibleStateSet getAccessibleStateSet()
4393 AccessibleStateSet s = new AccessibleStateSet();
4394 if (Component.this.isEnabled())
4395 s.add(AccessibleState.ENABLED);
4396 if (isFocusable())
4397 s.add(AccessibleState.FOCUSABLE);
4398 if (isFocusOwner())
4399 s.add(AccessibleState.FOCUSED);
4400 if (isOpaque())
4401 s.add(AccessibleState.OPAQUE);
4402 if (Component.this.isShowing())
4403 s.add(AccessibleState.SHOWING);
4404 if (Component.this.isVisible())
4405 s.add(AccessibleState.VISIBLE);
4406 return s;
4410 * Returns the parent of this component, if it is accessible.
4412 * @return the accessible parent
4414 public Accessible getAccessibleParent()
4416 if (accessibleParent == null)
4418 Container parent = getParent();
4419 accessibleParent = parent instanceof Accessible
4420 ? (Accessible) parent : null;
4422 return accessibleParent;
4426 * Returns the index of this component in its accessible parent.
4428 * @return the index, or -1 if the parent is not accessible
4429 * @see #getAccessibleParent()
4431 public int getAccessibleIndexInParent()
4433 if (getAccessibleParent() == null)
4434 return -1;
4435 AccessibleContext context
4436 = ((Component) accessibleParent).getAccessibleContext();
4437 if (context == null)
4438 return -1;
4439 for (int i = context.getAccessibleChildrenCount(); --i >= 0; )
4440 if (context.getAccessibleChild(i) == Component.this)
4441 return i;
4442 return -1;
4446 * Returns the number of children of this component which implement
4447 * Accessible. Subclasses must override this if they can have children.
4449 * @return the number of accessible children, default 0
4451 public int getAccessibleChildrenCount()
4453 return 0;
4457 * Returns the ith accessible child. Subclasses must override this if
4458 * they can have children.
4460 * @return the ith accessible child, or null
4461 * @see #getAccessibleChildrenCount()
4463 public Accessible getAccessibleChild(int i)
4465 return null;
4469 * Returns the locale of this component.
4471 * @return the locale
4472 * @throws IllegalComponentStateException if the locale is unknown
4474 public Locale getLocale()
4476 return Component.this.getLocale();
4480 * Returns this, since it is an accessible component.
4482 * @return the accessible component
4484 public AccessibleComponent getAccessibleComponent()
4486 return this;
4490 * Gets the background color.
4492 * @return the background color
4493 * @see #setBackground(Color)
4495 public Color getBackground()
4497 return Component.this.getBackground();
4501 * Sets the background color.
4503 * @param c the background color
4504 * @see #getBackground()
4505 * @see #isOpaque()
4507 public void setBackground(Color c)
4509 Component.this.setBackground(c);
4513 * Gets the foreground color.
4515 * @return the foreground color
4516 * @see #setForeground(Color)
4518 public Color getForeground()
4520 return Component.this.getForeground();
4524 * Sets the foreground color.
4526 * @param c the foreground color
4527 * @see #getForeground()
4529 public void setForeground(Color c)
4531 Component.this.setForeground(c);
4535 * Gets the cursor.
4537 * @return the cursor
4538 * @see #setCursor(Cursor)
4540 public Cursor getCursor()
4542 return Component.this.getCursor();
4546 * Sets the cursor.
4548 * @param cursor the cursor
4549 * @see #getCursor()
4551 public void setCursor(Cursor cursor)
4553 Component.this.setCursor(cursor);
4557 * Gets the font.
4559 * @return the font
4560 * @see #setFont(Font)
4562 public Font getFont()
4564 return Component.this.getFont();
4568 * Sets the font.
4570 * @param f the font
4571 * @see #getFont()
4573 public void setFont(Font f)
4575 Component.this.setFont(f);
4579 * Gets the font metrics for a font.
4581 * @param f the font to look up
4582 * @return its metrics
4583 * @throws NullPointerException if f is null
4584 * @see #getFont()
4586 public FontMetrics getFontMetrics(Font f)
4588 return Component.this.getFontMetrics(f);
4592 * Tests if the component is enabled.
4594 * @return true if the component is enabled
4595 * @see #setEnabled(boolean)
4596 * @see #getAccessibleStateSet()
4597 * @see AccessibleState#ENABLED
4599 public boolean isEnabled()
4601 return Component.this.isEnabled();
4605 * Set whether the component is enabled.
4607 * @param b the new enabled status
4608 * @see #isEnabled()
4610 public void setEnabled(boolean b)
4612 Component.this.setEnabled(b);
4616 * Test whether the component is visible (not necesarily showing).
4618 * @return true if it is visible
4619 * @see #setVisible(boolean)
4620 * @see #getAccessibleStateSet()
4621 * @see AccessibleState#VISIBLE
4623 public boolean isVisible()
4625 return Component.this.isVisible();
4629 * Sets the visibility of this component.
4631 * @param b the desired visibility
4632 * @see #isVisible()
4634 public void setVisible(boolean b)
4636 Component.this.setVisible(b);
4640 * Tests if the component is showing.
4642 * @return true if this is showing
4644 public boolean isShowing()
4646 return Component.this.isShowing();
4650 * Tests if the point is contained in this component.
4652 * @param p the point to check
4653 * @return true if it is contained
4654 * @throws NullPointerException if p is null
4656 public boolean contains(Point p)
4658 return Component.this.contains(p.x, p.y);
4662 * Returns the location of this object on the screen, or null if it is
4663 * not showing.
4665 * @return the location relative to screen coordinates, if showing
4666 * @see #getBounds()
4667 * @see #getLocation()
4669 public Point getLocationOnScreen()
4671 return Component.this.isShowing() ? Component.this.getLocationOnScreen()
4672 : null;
4676 * Returns the location of this object relative to its parent's coordinate
4677 * system, or null if it is not showing.
4679 * @return the location
4680 * @see #getBounds()
4681 * @see #getLocationOnScreen()
4683 public Point getLocation()
4685 return Component.this.isShowing() ? Component.this.getLocation() : null;
4689 * Sets the location of this relative to its parent's coordinate system.
4691 * @param p the location
4692 * @throws NullPointerException if p is null
4693 * @see #getLocation()
4695 public void setLocation(Point p)
4697 Component.this.setLocation(p.x, p.y);
4701 * Gets the bounds of this component, or null if it is not on screen.
4703 * @return the bounds
4704 * @see #contains(Point)
4705 * @see #setBounds(Rectangle)
4707 public Rectangle getBounds()
4709 return Component.this.isShowing() ? Component.this.getBounds() : null;
4713 * Sets the bounds of this component.
4715 * @param r the bounds
4716 * @throws NullPointerException if r is null
4717 * @see #getBounds()
4719 public void setBounds(Rectangle r)
4721 Component.this.setBounds(r.x, r.y, r.width, r.height);
4725 * Gets the size of this component, or null if it is not showing.
4727 * @return the size
4728 * @see #setSize(Dimension)
4730 public Dimension getSize()
4732 return Component.this.isShowing() ? Component.this.getSize() : null;
4736 * Sets the size of this component.
4738 * @param d the size
4739 * @throws NullPointerException if d is null
4740 * @see #getSize()
4742 public void setSize(Dimension d)
4744 Component.this.setSize(d.width, d.height);
4748 * Returns the Accessible child at a point relative to the coordinate
4749 * system of this component, if one exists, or null. Since components
4750 * have no children, subclasses must override this to get anything besides
4751 * null.
4753 * @param p the point to check
4754 * @return the accessible child at that point
4755 * @throws NullPointerException if p is null
4757 public Accessible getAccessibleAt(Point p)
4759 return null;
4763 * Tests whether this component can accept focus.
4765 * @return true if this is focus traversable
4766 * @see #getAccessibleStateSet()
4767 * @see AccessibleState#FOCUSABLE
4768 * @see AccessibleState#FOCUSED
4770 public boolean isFocusTraversable()
4772 return Component.this.isFocusTraversable();
4776 * Requests focus for this component.
4778 * @see #isFocusTraversable()
4780 public void requestFocus()
4782 Component.this.requestFocus();
4786 * Adds a focus listener.
4788 * @param l the listener to add
4790 public void addFocusListener(FocusListener l)
4792 Component.this.addFocusListener(l);
4796 * Removes a focus listener.
4798 * @param l the listener to remove
4800 public void removeFocusListener(FocusListener l)
4802 Component.this.removeFocusListener(l);
4806 * Converts component changes into property changes.
4808 * @author Eric Blake <ebb9@email.byu.edu>
4809 * @since 1.3
4810 * @status updated to 1.4
4812 protected class AccessibleAWTComponentHandler implements ComponentListener
4815 * Default constructor.
4817 protected AccessibleAWTComponentHandler()
4822 * Convert a component hidden to a property change.
4824 * @param e the event to convert
4826 public void componentHidden(ComponentEvent e)
4828 AccessibleAWTComponent.this.firePropertyChange
4829 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.VISIBLE, null);
4833 * Convert a component shown to a property change.
4835 * @param e the event to convert
4837 public void componentShown(ComponentEvent e)
4839 AccessibleAWTComponent.this.firePropertyChange
4840 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.VISIBLE);
4844 * Moving a component does not affect properties.
4846 * @param e ignored
4848 public void componentMoved(ComponentEvent e)
4853 * Resizing a component does not affect properties.
4855 * @param e ignored
4857 public void componentResized(ComponentEvent e)
4860 } // class AccessibleAWTComponentHandler
4863 * Converts focus changes into property changes.
4865 * @author Eric Blake <ebb9@email.byu.edu>
4866 * @since 1.3
4867 * @status updated to 1.4
4869 protected class AccessibleAWTFocusHandler implements FocusListener
4872 * Default constructor.
4874 protected AccessibleAWTFocusHandler()
4879 * Convert a focus gained to a property change.
4881 * @param e the event to convert
4883 public void focusGained(FocusEvent e)
4885 AccessibleAWTComponent.this.firePropertyChange
4886 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED);
4890 * Convert a focus lost to a property change.
4892 * @param e the event to convert
4894 public void focusLost(FocusEvent e)
4896 AccessibleAWTComponent.this.firePropertyChange
4897 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null);
4899 } // class AccessibleAWTComponentHandler
4900 } // class AccessibleAWTComponent
4903 * This class provides support for blitting offscreen surfaces.
4905 * @author Eric Blake <ebb9@email.byu.edu>
4906 * @since 1.4
4907 * @XXX Shell class, to allow compilation. This needs documentation and
4908 * correct implementation.
4910 protected class BltBufferStrategy extends BufferStrategy
4912 protected BufferCapabilities caps;
4913 protected VolatileImage[] backBuffers;
4914 protected boolean validatedContents;
4915 protected int width;
4916 protected int height;
4917 protected BltBufferStrategy(int num, BufferCapabilities caps)
4919 this.caps = caps;
4920 createBackBuffers(num);
4922 protected void createBackBuffers(int num)
4924 backBuffers = new VolatileImage[num];
4926 public BufferCapabilities getCapabilities()
4928 return caps;
4930 public Graphics getDrawGraphics() { return null; }
4931 public void show() {}
4932 protected void revalidate() {}
4933 public boolean contentsLost() { return false; }
4934 public boolean contentsRestored() { return false; }
4935 } // class BltBufferStrategy
4938 * This class provides support for flipping component buffers. It is only
4939 * designed for use by Canvas and Window.
4941 * @author Eric Blake <ebb9@email.byu.edu>
4942 * @since 1.4
4943 * @XXX Shell class, to allow compilation. This needs documentation and
4944 * correct implementation.
4946 protected class FlipBufferStrategy extends BufferStrategy
4948 protected int numBuffers;
4949 protected BufferCapabilities caps;
4950 protected Image drawBuffer;
4951 protected VolatileImage drawVBuffer;
4952 protected boolean validatedContents;
4953 protected FlipBufferStrategy(int num, BufferCapabilities caps)
4954 throws AWTException
4956 this.caps = caps;
4957 createBuffers(num, caps);
4959 protected void createBuffers(int num, BufferCapabilities caps)
4960 throws AWTException {}
4961 protected Image getBackBuffer()
4963 return drawBuffer;
4965 protected void flip(BufferCapabilities.FlipContents flipAction) {}
4966 protected void destroyBuffers() {}
4967 public BufferCapabilities getCapabilities()
4969 return caps;
4971 public Graphics getDrawGraphics() { return null; }
4972 protected void revalidate() {}
4973 public boolean contentsLost() { return false; }
4974 public boolean contentsRestored() { return false; }
4975 public void show() {}
4976 } // class FlipBufferStrategy
4977 } // class Component