2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / Component.java
blob9c93bd0a52783af1e382e90fcb1b25096cf1518f
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 * The system properties that affect image updating.
561 private static transient boolean incrementalDraw;
562 private static transient Long redrawRate;
564 static
566 incrementalDraw = Boolean.getBoolean ("awt.image.incrementalDraw");
567 redrawRate = Long.getLong ("awt.image.redrawrate");
570 // Public and protected API.
573 * Default constructor for subclasses. When Component is extended directly,
574 * it forms a lightweight component that must be hosted in an opaque native
575 * container higher in the tree.
577 protected Component()
582 * Returns the name of this component.
584 * @return the name of this component
585 * @see #setName(String)
586 * @since 1.1
588 public String getName()
590 if (name == null && ! nameExplicitlySet)
591 name = generateName();
592 return name;
596 * Sets the name of this component to the specified name.
598 * @param name the new name of this component
599 * @see #getName()
600 * @since 1.1
602 public void setName(String name)
604 nameExplicitlySet = true;
605 this.name = name;
609 * Returns the parent of this component.
611 * @return the parent of this component
613 public Container getParent()
615 return parent;
619 * Returns the native windowing system peer for this component. Only the
620 * platform specific implementation code should call this method.
622 * @return the peer for this component
623 * @deprecated user programs should not directly manipulate peers; use
624 * {@link #isDisplayable()} instead
626 // Classpath's Gtk peers rely on this.
627 public ComponentPeer getPeer()
629 return peer;
633 * Set the associated drag-and-drop target, which receives events when this
634 * is enabled.
636 * @param dt the new drop target
637 * @see #isEnabled()
639 public void setDropTarget(DropTarget dt)
641 this.dropTarget = dt;
645 * Gets the associated drag-and-drop target, if there is one.
647 * @return the drop target
649 public DropTarget getDropTarget()
651 return dropTarget;
655 * Returns the graphics configuration of this component, if there is one.
656 * If it has not been set, it is inherited from the parent.
658 * @return the graphics configuration, or null
659 * @since 1.3
661 public GraphicsConfiguration getGraphicsConfiguration()
663 return getGraphicsConfigurationImpl();
667 * Returns the object used for synchronization locks on this component
668 * when performing tree and layout functions.
670 * @return the synchronization lock for this component
672 public final Object getTreeLock()
674 return treeLock;
678 * Returns the toolkit in use for this component. The toolkit is associated
679 * with the frame this component belongs to.
681 * @return the toolkit for this component
683 public Toolkit getToolkit()
685 if (peer != null)
687 Toolkit tk = peer.getToolkit();
688 if (tk != null)
689 return tk;
691 // Get toolkit for lightweight component.
692 if (parent != null)
693 return parent.getToolkit();
694 return Toolkit.getDefaultToolkit();
698 * Tests whether or not this component is valid. A invalid component needs
699 * to have its layout redone.
701 * @return true if this component is valid
702 * @see #validate()
703 * @see #invalidate()
705 public boolean isValid()
707 return valid;
711 * Tests if the component is displayable. It must be connected to a native
712 * screen resource, and all its ancestors must be displayable. A containment
713 * hierarchy is made displayable when a window is packed or made visible.
715 * @return true if the component is displayable
716 * @see Container#add(Component)
717 * @see Container#remove(Component)
718 * @see Window#pack()
719 * @see Window#show()
720 * @see Window#dispose()
721 * @since 1.2
723 public boolean isDisplayable()
725 if (parent != null)
726 return parent.isDisplayable();
727 return false;
731 * Tests whether or not this component is visible. Except for top-level
732 * frames, components are initially visible.
734 * @return true if the component is visible
735 * @see #setVisible(boolean)
737 public boolean isVisible()
739 return visible;
743 * Tests whether or not this component is actually being shown on
744 * the screen. This will be true if and only if it this component is
745 * visible and its parent components are all visible.
747 * @return true if the component is showing on the screen
748 * @see #setVisible(boolean)
750 public boolean isShowing()
752 if (! visible || peer == null)
753 return false;
755 return parent == null ? true : parent.isShowing();
759 * Tests whether or not this component is enabled. Components are enabled
760 * by default, and must be enabled to receive user input or generate events.
762 * @return true if the component is enabled
763 * @see #setEnabled(boolean)
765 public boolean isEnabled()
767 return enabled;
771 * Enables or disables this component. The component must be enabled to
772 * receive events (except that lightweight components always receive mouse
773 * events).
775 * @param enabled true to enable this component
776 * @see #isEnabled()
777 * @see #isLightweight()
778 * @since 1.1
780 public void setEnabled(boolean b)
782 this.enabled = b;
783 if (peer != null)
784 peer.setEnabled(b);
788 * Enables this component.
790 * @deprecated use {@link #setEnabled(boolean)} instead
792 public void enable()
794 setEnabled(true);
798 * Enables or disables this component.
800 * @param enabled true to enable this component
801 * @deprecated use {@link #setEnabled(boolean)} instead
803 public void enable(boolean b)
805 setEnabled(b);
809 * Disables this component.
811 * @deprecated use {@link #setEnabled(boolean)} instead
813 public void disable()
815 setEnabled(false);
819 * Checks if this image is painted to an offscreen image buffer that is
820 * later copied to screen (double buffering reduces flicker). This version
821 * returns false, so subclasses must override it if they provide double
822 * buffering.
824 * @return true if this is double buffered; defaults to false
826 public boolean isDoubleBuffered()
828 return false;
832 * Enables or disables input method support for this component. By default,
833 * components have this enabled. Input methods are given the opportunity
834 * to process key events before this component and its listeners.
836 * @param enable true to enable input method processing
837 * @see #processKeyEvent(KeyEvent)
838 * @since 1.2
840 public void enableInputMethods(boolean enable)
842 // XXX Implement.
843 throw new Error("not implemented");
847 * Makes this component visible or invisible. Note that it wtill might
848 * not show the component, if a parent is invisible.
850 * @param visible true to make this component visible
851 * @see #isVisible()
852 * @since 1.1
854 public void setVisible(boolean b)
856 // Inspection by subclassing shows that Sun's implementation calls
857 // show(boolean) which then calls show() or hide(). It is the show()
858 // method that is overriden in subclasses like Window.
859 if (b)
860 show();
861 else
862 hide();
866 * Makes this component visible on the screen.
868 * @deprecated use {@link #setVisible(boolean)} instead
870 public void show()
872 if (peer != null)
873 peer.setVisible(true);
874 this.visible = true;
878 * Makes this component visible or invisible.
880 * @param visible true to make this component visible
881 * @deprecated use {@link #setVisible(boolean)} instead
883 public void show(boolean b)
885 setVisible(b);
889 * Hides this component so that it is no longer shown on the screen.
891 * @deprecated use {@link #setVisible(boolean)} instead
893 public void hide()
895 if (peer != null)
896 peer.setVisible(false);
897 this.visible = false;
901 * Returns this component's foreground color. If not set, this is inherited
902 * from the parent.
904 * @return this component's foreground color, or null
905 * @see #setForeground(Color)
907 public Color getForeground()
909 if (foreground != null)
910 return foreground;
911 return parent == null ? null : parent.getForeground();
915 * Sets this component's foreground color to the specified color. This is a
916 * bound property.
918 * @param c the new foreground color
919 * @see #getForeground()
921 public void setForeground(Color c)
923 firePropertyChange("foreground", foreground, c);
924 if (peer != null)
925 peer.setForeground(c);
926 foreground = c;
930 * Tests if the foreground was explicitly set, or just inherited from the
931 * parent.
933 * @return true if the foreground has been set
934 * @since 1.4
936 public boolean isForegroundSet()
938 return foreground != null;
942 * Returns this component's background color. If not set, this is inherited
943 * from the parent.
945 * @return the background color of the component, or null
946 * @see #setBackground(Color)
948 public Color getBackground()
950 if (background != null)
951 return background;
952 return parent == null ? null : parent.getBackground();
956 * Sets this component's background color to the specified color. The parts
957 * of the component affected by the background color may by system dependent.
958 * This is a bound property.
960 * @param c the new background color
961 * @see #getBackground()
963 public void setBackground(Color c)
965 firePropertyChange("background", background, c);
966 if (peer != null)
967 peer.setBackground(c);
968 background = c;
972 * Tests if the background was explicitly set, or just inherited from the
973 * parent.
975 * @return true if the background has been set
976 * @since 1.4
978 public boolean isBackgroundSet()
980 return background != null;
984 * Returns the font in use for this component. If not set, this is inherited
985 * from the parent.
987 * @return the font for this component
988 * @see #setFont(Font)
990 public Font getFont()
992 if (font != null)
993 return font;
994 return parent == null ? null : parent.getFont();
998 * Sets the font for this component to the specified font. This is a bound
999 * property.
1001 * @param font the new font for this component
1002 * @see #getFont()
1004 public void setFont(Font f)
1006 firePropertyChange("font", font, f);
1007 if (peer != null)
1008 peer.setFont(f);
1009 font = f;
1013 * Tests if the font was explicitly set, or just inherited from the parent.
1015 * @return true if the font has been set
1016 * @since 1.4
1018 public boolean isFontSet()
1020 return font != null;
1024 * Returns the locale for this component. If this component does not
1025 * have a locale, the locale of the parent component is returned.
1027 * @return the locale for this component
1028 * @throws IllegalComponentStateException if it has no locale or parent
1029 * @see setLocale(Locale)
1030 * @since 1.1
1032 public Locale getLocale()
1034 if (locale != null)
1035 return locale;
1036 if (parent == null)
1037 throw new IllegalComponentStateException
1038 ("Component has no parent: can't determine Locale");
1039 return parent.getLocale();
1043 * Sets the locale for this component to the specified locale. This is a
1044 * bound property.
1046 * @param locale the new locale for this component
1048 public void setLocale(Locale l)
1050 firePropertyChange("locale", locale, l);
1051 locale = l;
1052 // New writing/layout direction or more/less room for localized labels.
1053 invalidate();
1057 * Returns the color model of the device this componet is displayed on.
1059 * @return this object's color model
1060 * @see Toolkit#getColorModel()
1062 public ColorModel getColorModel()
1064 GraphicsConfiguration config = getGraphicsConfiguration();
1065 return config != null ? config.getColorModel()
1066 : getToolkit().getColorModel();
1070 * Returns the location of this component's top left corner relative to
1071 * its parent component. This may be outdated, so for synchronous behavior,
1072 * you should use a component listner.
1074 * @return the location of this component
1075 * @see #setLocation(int, int)
1076 * @see #getLocationOnScreen()
1077 * @since 1.1
1079 public Point getLocation()
1081 return new Point(x, y);
1085 * Returns the location of this component's top left corner in screen
1086 * coordinates.
1088 * @return the location of this component in screen coordinates
1089 * @throws IllegalComponentStateException if the component is not showing
1091 public Point getLocationOnScreen()
1093 if (! isShowing())
1094 throw new IllegalComponentStateException("component not showing");
1095 // We know peer != null here.
1096 return peer.getLocationOnScreen();
1100 * Returns the location of this component's top left corner relative to
1101 * its parent component.
1103 * @return the location of this component
1104 * @deprecated use {@link #getLocation()} instead
1106 public Point location()
1108 return getLocation();
1112 * Moves this component to the specified location, relative to the parent's
1113 * coordinates. The coordinates are the new upper left corner of this
1114 * component.
1116 * @param x the new X coordinate of this component
1117 * @param y the new Y coordinate of this component
1118 * @see #getLocation()
1119 * @see #setBounds(int, int, int, int)
1121 public void setLocation(int x, int y)
1123 if (this.x == x && this.y == y)
1124 return;
1125 invalidate();
1126 this.x = x;
1127 this.y = y;
1128 if (peer != null)
1129 peer.setBounds(x, y, width, height);
1133 * Moves this component to the specified location, relative to the parent's
1134 * coordinates. The coordinates are the new upper left corner of this
1135 * component.
1137 * @param x the new X coordinate of this component
1138 * @param y the new Y coordinate of this component
1139 * @deprecated use {@link #setLocation(int, int)} instead
1141 public void move(int x, int y)
1143 setLocation(x, y);
1147 * Moves this component to the specified location, relative to the parent's
1148 * coordinates. The coordinates are the new upper left corner of this
1149 * component.
1151 * @param p new coordinates for this component
1152 * @throws NullPointerException if p is null
1153 * @see #getLocation()
1154 * @see #setBounds(int, int, int, int)
1155 * @since 1.1
1157 public void setLocation(Point p)
1159 setLocation(p.x, p.y);
1163 * Returns the size of this object.
1165 * @return the size of this object
1166 * @see #setSize(int, int)
1167 * @since 1.1
1169 public Dimension getSize()
1171 return new Dimension(width, height);
1175 * Returns the size of this object.
1177 * @return the size of this object
1178 * @deprecated use {@link #getSize()} instead
1180 public Dimension size()
1182 return getSize();
1186 * Sets the size of this component to the specified width and height.
1188 * @param width the new width of this component
1189 * @param height the new height of this component
1190 * @see #getSize()
1191 * @see #setBounds(int, int, int, int)
1193 public void setSize(int width, int height)
1195 if (this.width == width && this.height == height)
1196 return;
1197 invalidate();
1198 this.width = width;
1199 this.height = height;
1200 if (peer != null)
1201 peer.setBounds(x, y, width, height);
1205 * Sets the size of this component to the specified value.
1207 * @param width the new width of the component
1208 * @param height the new height of the component
1209 * @deprecated use {@link #setSize(int, int)} instead
1211 public void resize(int width, int height)
1213 setSize(width, height);
1217 * Sets the size of this component to the specified value.
1219 * @param d the new size of this component
1220 * @throws NullPointerException if d is null
1221 * @see #setSize(int, int)
1222 * @see #setBounds(int, int, int, int)
1223 * @since 1.1
1225 public void setSize(Dimension d)
1227 setSize(d.width, d.height);
1231 * Sets the size of this component to the specified value.
1233 * @param d the new size of this component
1234 * @throws NullPointerException if d is null
1235 * @deprecated use {@link #setSize(Dimension)} instead
1237 public void resize(Dimension d)
1239 setSize(d.width, d.height);
1243 * Returns a bounding rectangle for this component. Note that the
1244 * returned rectange is relative to this component's parent, not to
1245 * the screen.
1247 * @return the bounding rectangle for this component
1248 * @see #setBounds(int, int, int, int)
1249 * @see #getLocation()
1250 * @see #getSize()
1252 public Rectangle getBounds()
1254 return new Rectangle(x, y, width, height);
1258 * Returns a bounding rectangle for this component. Note that the
1259 * returned rectange is relative to this component's parent, not to
1260 * the screen.
1262 * @return the bounding rectangle for this component
1263 * @deprecated use {@link #getBounds()} instead
1265 public Rectangle bounds()
1267 return getBounds();
1271 * Sets the bounding rectangle for this component to the specified values.
1272 * Note that these coordinates are relative to the parent, not to the screen.
1274 * @param x the X coordinate of the upper left corner of the rectangle
1275 * @param y the Y coordinate of the upper left corner of the rectangle
1276 * @param w the width of the rectangle
1277 * @param h the height of the rectangle
1278 * @see #getBounds()
1279 * @see #setLocation(int, int)
1280 * @see #setLocation(Point)
1281 * @see #setSize(int, int)
1282 * @see #setSize(Dimension)
1283 * @since 1.1
1285 public void setBounds(int x, int y, int w, int h)
1287 if (this.x == x && this.y == y && width == w && height == h)
1288 return;
1289 invalidate();
1290 this.x = x;
1291 this.y = y;
1292 width = w;
1293 height = h;
1294 if (peer != null)
1295 peer.setBounds(x, y, w, h);
1299 * Sets the bounding rectangle for this component to the specified values.
1300 * Note that these coordinates are relative to the parent, not to the screen.
1302 * @param x the X coordinate of the upper left corner of the rectangle
1303 * @param y the Y coordinate of the upper left corner of the rectangle
1304 * @param w the width of the rectangle
1305 * @param h the height of the rectangle
1306 * @deprecated use {@link #setBounds(int, int, int, int)} instead
1308 public void reshape(int x, int y, int width, int height)
1310 setBounds(x, y, width, height);
1314 * Sets the bounding rectangle for this component to the specified
1315 * rectangle. Note that these coordinates are relative to the parent, not
1316 * to the screen.
1318 * @param r the new bounding rectangle
1319 * @throws NullPointerException if r is null
1320 * @see #getBounds()
1321 * @see #setLocation(Point)
1322 * @see #setSize(Dimension)
1323 * @since 1.1
1325 public void setBounds(Rectangle r)
1327 setBounds(r.x, r.y, r.width, r.height);
1331 * Gets the x coordinate of the upper left corner. This is more efficient
1332 * than getBounds().x or getLocation().x.
1334 * @return the current x coordinate
1335 * @since 1.2
1337 public int getX()
1339 return x;
1343 * Gets the y coordinate of the upper left corner. This is more efficient
1344 * than getBounds().y or getLocation().y.
1346 * @return the current y coordinate
1347 * @since 1.2
1349 public int getY()
1351 return y;
1355 * Gets the width of the component. This is more efficient than
1356 * getBounds().width or getSize().width.
1358 * @return the current width
1359 * @since 1.2
1361 public int getWidth()
1363 return width;
1367 * Gets the height of the component. This is more efficient than
1368 * getBounds().height or getSize().height.
1370 * @return the current width
1371 * @since 1.2
1373 public int getHeight()
1375 return height;
1379 * Returns the bounds of this component. This allows reuse of an existing
1380 * rectangle, if r is non-null.
1382 * @param r the rectangle to use, or null
1383 * @return the bounds
1385 public Rectangle getBounds(Rectangle r)
1387 if (r == null)
1388 r = new Rectangle();
1389 r.x = x;
1390 r.y = y;
1391 r.width = width;
1392 r.height = height;
1393 return r;
1397 * Returns the size of this component. This allows reuse of an existing
1398 * dimension, if d is non-null.
1400 * @param d the dimension to use, or null
1401 * @return the size
1403 public Dimension getSize(Dimension d)
1405 if (d == null)
1406 d = new Dimension();
1407 d.width = width;
1408 d.height = height;
1409 return d;
1413 * Returns the location of this component. This allows reuse of an existing
1414 * point, if p is non-null.
1416 * @param p the point to use, or null
1417 * @return the location
1419 public Point getLocation(Point p)
1421 if (p == null)
1422 p = new Point();
1423 p.x = x;
1424 p.y = y;
1425 return p;
1429 * Tests if this component is opaque. All "heavyweight" (natively-drawn)
1430 * components are opaque. A component is opaque if it draws all pixels in
1431 * the bounds; a lightweight component is partially transparent if it lets
1432 * pixels underneath show through. Subclasses that guarantee that all pixels
1433 * will be drawn should override this.
1435 * @return true if this is opaque
1436 * @see #isLightweight()
1437 * @since 1.2
1439 public boolean isOpaque()
1441 return ! isLightweight();
1445 * Return whether the component is lightweight. That means the component has
1446 * no native peer, but is displayable. This applies to subclasses of
1447 * Component not in this package, such as javax.swing.
1449 * @return true if the component has a lightweight peer
1450 * @see #isDisplayable()
1451 * @since 1.2
1453 public boolean isLightweight()
1455 return peer instanceof LightweightPeer;
1459 * Returns the component's preferred size.
1461 * @return the component's preferred size
1462 * @see #getMinimumSize()
1463 * @see LayoutManager
1465 public Dimension getPreferredSize()
1467 return preferredSize();
1471 * Returns the component's preferred size.
1473 * @return the component's preferred size
1474 * @deprecated use {@link #getPreferredSize()} instead
1476 public Dimension preferredSize()
1478 if (prefSize == null)
1479 if (peer == null)
1480 return new Dimension(width, height);
1481 else
1482 prefSize = peer.getPreferredSize();
1483 return prefSize;
1487 * Returns the component's minimum size.
1489 * @return the component's minimum size
1490 * @see #getPreferredSize()
1491 * @see LayoutManager
1493 public Dimension getMinimumSize()
1495 return minimumSize();
1499 * Returns the component's minimum size.
1501 * @return the component's minimum size
1502 * @deprecated use {@link #getMinimumSize()} instead
1504 public Dimension minimumSize()
1506 if (minSize == null)
1507 minSize = (peer != null ? peer.getMinimumSize()
1508 : new Dimension(width, height));
1509 return minSize;
1513 * Returns the component's maximum size.
1515 * @return the component's maximum size
1516 * @see #getMinimumSize()
1517 * @see #getPreferredSize()
1518 * @see LayoutManager
1520 public Dimension getMaximumSize()
1522 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1526 * Returns the preferred horizontal alignment of this component. The value
1527 * returned will be between {@link #LEFT_ALIGNMENT} and
1528 * {@link #RIGHT_ALIGNMENT}, inclusive.
1530 * @return the preferred horizontal alignment of this component
1532 public float getAlignmentX()
1534 return CENTER_ALIGNMENT;
1538 * Returns the preferred vertical alignment of this component. The value
1539 * returned will be between {@link #TOP_ALIGNMENT} and
1540 * {@link #BOTTOM_ALIGNMENT}, inclusive.
1542 * @return the preferred vertical alignment of this component
1544 public float getAlignmentY()
1546 return CENTER_ALIGNMENT;
1550 * Calls the layout manager to re-layout the component. This is called
1551 * during validation of a container in most cases.
1553 * @see #validate()
1554 * @see LayoutManager
1556 public void doLayout()
1558 // nothing to do unless we're a container
1562 * Calls the layout manager to re-layout the component. This is called
1563 * during validation of a container in most cases.
1565 * @deprecated use {@link #doLayout()} instead
1567 public void layout()
1569 doLayout();
1573 * Called to ensure that the layout for this component is valid. This is
1574 * usually called on containers.
1576 * @see #invalidate()
1577 * @see #doLayout()
1578 * @see LayoutManager
1579 * @see Container#validate()
1581 public void validate()
1583 valid = true;
1587 * Invalidates this component and all of its parent components. This will
1588 * cause them to have their layout redone. This is called frequently, so
1589 * make it fast.
1591 public void invalidate()
1593 valid = false;
1594 prefSize = null;
1595 minSize = null;
1596 if (parent != null && parent.valid)
1597 parent.invalidate();
1601 * Returns a graphics object for this component. Returns <code>null</code>
1602 * if this component is not currently displayed on the screen.
1604 * @return a graphics object for this component
1605 * @see #paint(Graphics)
1607 public Graphics getGraphics()
1609 if (peer != null)
1611 Graphics gfx = peer.getGraphics();
1612 if (gfx != null)
1613 return gfx;
1614 // create graphics for lightweight:
1615 Container parent = getParent();
1616 if (parent != null)
1618 gfx = parent.getGraphics();
1619 Rectangle bounds = getBounds();
1620 gfx.setClip(bounds);
1621 gfx.translate(bounds.x, bounds.y);
1622 return gfx;
1625 return null;
1629 * Returns the font metrics for the specified font in this component.
1631 * @param font the font to retrieve metrics for
1632 * @return the font metrics for the specified font
1633 * @throws NullPointerException if font is null
1634 * @see #getFont()
1635 * @see Toolkit#getFontMetrics(Font)
1637 public FontMetrics getFontMetrics(Font font)
1639 return peer == null ? getToolkit().getFontMetrics(font)
1640 : peer.getFontMetrics(font);
1644 * Sets the cursor for this component to the specified cursor. The cursor
1645 * is displayed when the point is contained by the component, and the
1646 * component is visible, displayable, and enabled. This is inherited by
1647 * subcomponents unless they set their own cursor.
1649 * @param cursor the new cursor for this component
1650 * @see #isEnabled()
1651 * @see #isShowing()
1652 * @see #getCursor()
1653 * @see #contains(int, int)
1654 * @see Toolkit#createCustomCursor(Image, Point, String)
1656 public void setCursor(Cursor cursor)
1658 this.cursor = cursor;
1659 if (peer != null)
1660 peer.setCursor(cursor);
1664 * Returns the cursor for this component. If not set, this is inherited
1665 * from the parent, or from Cursor.getDefaultCursor().
1667 * @return the cursor for this component
1669 public Cursor getCursor()
1671 if (cursor != null)
1672 return cursor;
1673 return parent != null ? parent.getCursor() : Cursor.getDefaultCursor();
1677 * Tests if the cursor was explicitly set, or just inherited from the parent.
1679 * @return true if the cursor has been set
1680 * @since 1.4
1682 public boolean isCursorSet()
1684 return cursor != null;
1688 * Paints this component on the screen. The clipping region in the graphics
1689 * context will indicate the region that requires painting. This is called
1690 * whenever the component first shows, or needs to be repaired because
1691 * something was temporarily drawn on top. It is not necessary for
1692 * subclasses to call <code>super.paint(g)</code>. Components with no area
1693 * are not painted.
1695 * @param g the graphics context for this paint job
1696 * @see #update(Graphics)
1698 public void paint(Graphics g)
1703 * Updates this component. This is called in response to
1704 * <code>repaint</code>. This method fills the component with the
1705 * background color, then sets the foreground color of the specified
1706 * graphics context to the foreground color of this component and calls
1707 * the <code>paint()</code> method. The coordinates of the graphics are
1708 * relative to this component. Subclasses should call either
1709 * <code>super.update(g)</code> or <code>paint(g)</code>.
1711 * @param graphics the graphics context for this update
1712 * @see #paint(Graphics)
1713 * @see #repaint()
1715 public void update(Graphics g)
1717 paint(g);
1721 * Paints this entire component, including any sub-components.
1723 * @param graphics the graphics context for this paint job
1724 * @see #paint(Graphics)
1726 public void paintAll(Graphics g)
1728 if (! visible)
1729 return;
1730 if (peer != null)
1731 peer.paint(g);
1732 paint(g);
1736 * Repaint this entire component. The <code>update()</code> method
1737 * on this component will be called as soon as possible.
1739 * @see #update(Graphics)
1740 * @see #repaint(long, int, int, int, int)
1742 public void repaint()
1744 repaint(0, 0, 0, width, height);
1748 * Repaint this entire component. The <code>update()</code> method on this
1749 * component will be called in approximate the specified number of
1750 * milliseconds.
1752 * @param tm milliseconds before this component should be repainted
1753 * @see #paint(Graphics)
1754 * @see #repaint(long, int, int, int, int)
1756 public void repaint(long tm)
1758 repaint(tm, 0, 0, width, height);
1762 * Repaints the specified rectangular region within this component. The
1763 * <code>update</code> method on this component will be called as soon as
1764 * possible. The coordinates are relative to this component.
1766 * @param x the X coordinate of the upper left of the region to repaint
1767 * @param y the Y coordinate of the upper left of the region to repaint
1768 * @param w the width of the region to repaint
1769 * @param h the height of the region to repaint
1770 * @see #update(Graphics)
1771 * @see #repaint(long, int, int, int, int)
1773 public void repaint(int x, int y, int w, int h)
1775 repaint(0, x, y, w, h);
1779 * Repaints the specified rectangular region within this component. The
1780 * <code>update</code> method on this component will be called in
1781 * approximately the specified number of milliseconds. The coordinates
1782 * are relative to this component.
1784 * @param tm milliseconds before this component should be repainted
1785 * @param x the X coordinate of the upper left of the region to repaint
1786 * @param y the Y coordinate of the upper left of the region to repaint
1787 * @param w the width of the region to repaint
1788 * @param h the height of the region to repaint
1789 * @see #update(Graphics)
1791 public void repaint(long tm, int x, int y, int width, int height)
1793 // Handle lightweight repainting by forwarding to native parent
1794 if (isLightweight() && parent != null)
1796 if (parent != null)
1797 parent.repaint(tm, x + getX(), y + getY(), width, height);
1799 else if (peer != null)
1800 peer.repaint(tm, x, y, width, height);
1804 * Prints this component. This method is provided so that printing can be
1805 * done in a different manner from painting. However, the implementation
1806 * in this class simply calls the <code>paint()</code> method.
1808 * @param graphics the graphics context of the print device
1809 * @see #paint(Graphics)
1811 public void print(Graphics g)
1813 paint(g);
1817 * Prints this component, including all sub-components. This method is
1818 * provided so that printing can be done in a different manner from
1819 * painting. However, the implementation in this class simply calls the
1820 * <code>paintAll()</code> method.
1822 * @param graphics the graphics context of the print device
1823 * @see #paintAll(Graphics)
1825 public void printAll(Graphics g)
1827 paintAll(g);
1831 * Called when an image has changed so that this component is repainted.
1832 * This incrementally draws an image as more bits are available, when
1833 * possible. Incremental drawing is enabled if the system property
1834 * <code>awt.image.incrementalDraw</code> is not present or is true, in which
1835 * case the redraw rate is set to 100ms or the value of the system property
1836 * <code>awt.image.redrawrate</code>.
1838 * <p>The coordinate system used depends on the particular flags.
1840 * @param image the image that has been updated
1841 * @param flags tlags as specified in <code>ImageObserver</code>
1842 * @param x the X coordinate
1843 * @param y the Y coordinate
1844 * @param w the width
1845 * @param h the height
1846 * @return false if the image is completely loaded, loading has been
1847 * aborted, or an error has occurred. true if more updates are
1848 * required.
1849 * @see ImageObserver
1850 * @see Graphics#drawImage(Image, int, int, Color, ImageObserver)
1851 * @see Graphics#drawImage(Image, int, int, ImageObserver)
1852 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
1853 * @see Graphics#drawImage(Image, int, int, int, int, ImageObserver)
1854 * @see ImageObserver#update(Image, int, int, int, int, int)
1856 public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
1858 if ((flags & (FRAMEBITS | ALLBITS)) != 0)
1859 repaint ();
1860 else if ((flags & SOMEBITS) != 0)
1862 if (incrementalDraw)
1864 if (redrawRate != null)
1866 long tm = redrawRate.longValue();
1867 if (tm < 0)
1868 tm = 0;
1869 repaint (tm);
1871 else
1872 repaint (100);
1875 return (flags & (ALLBITS | ABORT | ERROR)) == 0;
1879 * Creates an image from the specified producer.
1881 * @param producer the image procedure to create the image from
1882 * @return the resulting image
1884 public Image createImage(ImageProducer producer)
1886 // Sun allows producer to be null.
1887 if (peer != null)
1888 return peer.createImage(producer);
1889 else
1890 return getToolkit().createImage(producer);
1894 * Creates an image with the specified width and height for use in
1895 * double buffering. Headless environments do not support images.
1897 * @param width the width of the image
1898 * @param height the height of the image
1899 * @return the requested image, or null if it is not supported
1901 public Image createImage (int width, int height)
1903 Image returnValue = null;
1904 if (!GraphicsEnvironment.isHeadless ())
1906 if (isLightweight () && parent != null)
1907 returnValue = parent.createImage (width, height);
1908 else if (peer != null)
1909 returnValue = peer.createImage (width, height);
1911 return returnValue;
1915 * Creates an image with the specified width and height for use in
1916 * double buffering. Headless environments do not support images.
1918 * @param width the width of the image
1919 * @param height the height of the image
1920 * @return the requested image, or null if it is not supported
1921 * @since 1.4
1923 public VolatileImage createVolatileImage(int width, int height)
1925 if (GraphicsEnvironment.isHeadless())
1926 return null;
1927 GraphicsConfiguration config = getGraphicsConfiguration();
1928 return config == null ? null
1929 : config.createCompatibleVolatileImage(width, height);
1933 * Creates an image with the specified width and height for use in
1934 * double buffering. Headless environments do not support images. The image
1935 * will support the specified capabilities.
1937 * @param width the width of the image
1938 * @param height the height of the image
1939 * @param caps the requested capabilities
1940 * @return the requested image, or null if it is not supported
1941 * @throws AWTException if a buffer with the capabilities cannot be created
1942 * @since 1.4
1944 public VolatileImage createVolatileImage(int width, int height,
1945 ImageCapabilities caps)
1946 throws AWTException
1948 if (GraphicsEnvironment.isHeadless())
1949 return null;
1950 GraphicsConfiguration config = getGraphicsConfiguration();
1951 return config == null ? null
1952 : config.createCompatibleVolatileImage(width, height, caps);
1956 * Prepares the specified image for rendering on this component.
1958 * @param image the image to prepare for rendering
1959 * @param observer the observer to notify of image preparation status
1960 * @return true if the image is already fully prepared
1961 * @throws NullPointerException if image is null
1963 public boolean prepareImage(Image image, ImageObserver observer)
1965 return prepareImage(image, image.getWidth(observer),
1966 image.getHeight(observer), observer);
1970 * Prepares the specified image for rendering on this component at the
1971 * specified scaled width and height
1973 * @param image the image to prepare for rendering
1974 * @param width the scaled width of the image
1975 * @param height the scaled height of the image
1976 * @param observer the observer to notify of image preparation status
1977 * @return true if the image is already fully prepared
1979 public boolean prepareImage(Image image, int width, int height,
1980 ImageObserver observer)
1982 if (peer != null)
1983 return peer.prepareImage(image, width, height, observer);
1984 else
1985 return getToolkit().prepareImage(image, width, height, observer);
1989 * Returns the status of the loading of the specified image. The value
1990 * returned will be those flags defined in <code>ImageObserver</code>.
1992 * @param image the image to check on
1993 * @param observer the observer to notify of image loading progress
1994 * @return the image observer flags indicating the status of the load
1995 * @see #prepareImage(Image, int, int, ImageObserver)
1996 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
1997 * @throws NullPointerException if image is null
1999 public int checkImage(Image image, ImageObserver observer)
2001 return checkImage(image, -1, -1, observer);
2005 * Returns the status of the loading of the specified image. The value
2006 * returned will be those flags defined in <code>ImageObserver</code>.
2008 * @param image the image to check on
2009 * @param width the scaled image width
2010 * @param height the scaled image height
2011 * @param observer the observer to notify of image loading progress
2012 * @return the image observer flags indicating the status of the load
2013 * @see #prepareImage(Image, int, int, ImageObserver)
2014 * @see #Toolkit#checkImage(Image, int, int, ImageObserver)
2016 public int checkImage(Image image, int width, int height,
2017 ImageObserver observer)
2019 if (peer != null)
2020 return peer.checkImage(image, width, height, observer);
2021 return getToolkit().checkImage(image, width, height, observer);
2025 * Sets whether paint messages delivered by the operating system should be
2026 * ignored. This does not affect messages from AWT, except for those
2027 * triggered by OS messages. Setting this to true can allow faster
2028 * performance in full-screen mode or page-flipping.
2030 * @param ignoreRepaint the new setting for ignoring repaint events
2031 * @see #getIgnoreRepaint()
2032 * @see BufferStrategy
2033 * @see GraphicsDevice.setFullScreenWindow(Window)
2034 * @since 1.4
2036 public void setIgnoreRepaint(boolean ignoreRepaint)
2038 this.ignoreRepaint = ignoreRepaint;
2042 * Test whether paint events from the operating system are ignored.
2044 * @return the status of ignoring paint events
2045 * @see #setIgnoreRepaint(boolean)
2046 * @since 1.4
2048 public boolean getIgnoreRepaint()
2050 return ignoreRepaint;
2054 * Tests whether or not the specified point is contained within this
2055 * component. Coordinates are relative to this component.
2057 * @param x the X coordinate of the point to test
2058 * @param y the Y coordinate of the point to test
2059 * @return true if the point is within this component
2060 * @see #getComponentAt(int, int)
2062 public boolean contains(int x, int y)
2064 return x >= 0 && y >= 0 && x < width && y < height;
2068 * Tests whether or not the specified point is contained within this
2069 * component. Coordinates are relative to this component.
2071 * @param x the X coordinate of the point to test
2072 * @param y the Y coordinate of the point to test
2073 * @return true if the point is within this component
2074 * @deprecated use {@link #contains(int, int)} instead
2076 public boolean inside(int x, int y)
2078 return contains(x, y);
2082 * Tests whether or not the specified point is contained within this
2083 * component. Coordinates are relative to this component.
2085 * @param p the point to test
2086 * @return true if the point is within this component
2087 * @throws NullPointerException if p is null
2088 * @see #getComponentAt(Point)
2089 * @since 1.1
2091 public boolean contains(Point p)
2093 return contains(p.x, p.y);
2097 * Returns the component occupying the position (x,y). This will either
2098 * be this component, an immediate child component, or <code>null</code>
2099 * if neither of the first two occupies the specified location.
2101 * @param x the X coordinate to search for components at
2102 * @param y the Y coordinate to search for components at
2103 * @return the component at the specified location, or null
2104 * @see #contains(int, int)
2106 public Component getComponentAt(int x, int y)
2108 return contains(x, y) ? this : null;
2112 * Returns the component occupying the position (x,y). This will either
2113 * be this component, an immediate child component, or <code>null</code>
2114 * if neither of the first two occupies the specified location.
2116 * @param x the X coordinate to search for components at
2117 * @param y the Y coordinate to search for components at
2118 * @return the component at the specified location, or null
2119 * @deprecated use {@link #getComponentAt(int, int)} instead
2121 public Component locate(int x, int y)
2123 return getComponentAt(x, y);
2127 * Returns the component occupying the position (x,y). This will either
2128 * be this component, an immediate child component, or <code>null</code>
2129 * if neither of the first two occupies the specified location.
2131 * @param p the point to search for components at
2132 * @return the component at the specified location, or null
2133 * @throws NullPointerException if p is null
2134 * @see #contains(Point)
2135 * @since 1.1
2137 public Component getComponentAt(Point p)
2139 return getComponentAt(p.x, p.y);
2143 * AWT 1.0 event dispatcher.
2145 * @param e the event to dispatch
2146 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2148 public void deliverEvent(Event e)
2150 // XXX Add backward compatibility handling.
2154 * Forwards AWT events to processEvent() if:<ul>
2155 * <li>Events have been enabled for this type of event via
2156 * <code>enableEvents()</code></li>,
2157 * <li>There is at least one registered listener for this type of event</li>
2158 * </ul>
2160 * @param e the event to dispatch
2162 public final void dispatchEvent(AWTEvent e)
2164 // Some subclasses in the AWT package need to override this behavior,
2165 // hence the use of dispatchEventImpl().
2166 dispatchEventImpl(e);
2167 if (peer != null && ! e.consumed)
2168 peer.handleEvent(e);
2172 * AWT 1.0 event dispatcher.
2174 * @param e the event to dispatch
2175 * @return false: since the method was deprecated, the return has no meaning
2176 * @deprecated use {@link #dispatchEvent(AWTEvent)} instead
2178 public boolean postEvent(Event e)
2180 // XXX Add backward compatibility handling.
2181 return false;
2185 * Adds the specified listener to this component. This is harmless if the
2186 * listener is null, but if the listener has already been registered, it
2187 * will now be registered twice.
2189 * @param listener the new listener to add
2190 * @see ComponentEvent
2191 * @see #removeComponentListener(ComponentListener)
2192 * @see #getComponentListeners()
2193 * @since 1.1
2195 public synchronized void addComponentListener(ComponentListener l)
2197 componentListener = AWTEventMulticaster.add(componentListener, l);
2198 if (componentListener != null)
2199 enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
2203 * Removes the specified listener from the component. This is harmless if
2204 * the listener was not previously registered.
2206 * @param listener the listener to remove
2207 * @see ComponentEvent
2208 * @see #addComponentListener(ComponentListener)
2209 * @see #getComponentListeners()
2210 * @since 1.1
2212 public synchronized void removeComponentListener(ComponentListener l)
2214 componentListener = AWTEventMulticaster.remove(componentListener, l);
2218 * Returns an array of all specified listeners registered on this component.
2220 * @return an array of listeners
2221 * @see #addComponentListener(ComponentListener)
2222 * @see #removeComponentListener(ComponentListener)
2223 * @since 1.4
2225 public synchronized ComponentListener[] getComponentListeners()
2227 return (ComponentListener[])
2228 AWTEventMulticaster.getListeners(componentListener,
2229 ComponentListener.class);
2233 * Adds the specified listener to this component. This is harmless if the
2234 * listener is null, but if the listener has already been registered, it
2235 * will now be registered twice.
2237 * @param listener the new listener to add
2238 * @see FocusEvent
2239 * @see #removeFocusListener(FocusListener)
2240 * @see #getFocusListeners()
2241 * @since 1.1
2243 public synchronized void addFocusListener(FocusListener l)
2245 focusListener = AWTEventMulticaster.add(focusListener, l);
2246 if (focusListener != null)
2247 enableEvents(AWTEvent.FOCUS_EVENT_MASK);
2251 * Removes the specified listener from the component. This is harmless if
2252 * the listener was not previously registered.
2254 * @param listener the listener to remove
2255 * @see FocusEvent
2256 * @see #addFocusListener(FocusListener)
2257 * @see #getFocusListeners()
2258 * @since 1.1
2260 public synchronized void removeFocusListener(FocusListener l)
2262 focusListener = AWTEventMulticaster.remove(focusListener, l);
2266 * Returns an array of all specified listeners registered on this component.
2268 * @return an array of listeners
2269 * @see #addFocusListener(FocusListener)
2270 * @see #removeFocusListener(FocusListener)
2271 * @since 1.4
2273 public synchronized FocusListener[] getFocusListeners()
2275 return (FocusListener[])
2276 AWTEventMulticaster.getListeners(focusListener, FocusListener.class);
2280 * Adds the specified listener to this component. This is harmless if the
2281 * listener is null, but if the listener has already been registered, it
2282 * will now be registered twice.
2284 * @param listener the new listener to add
2285 * @see HierarchyEvent
2286 * @see #removeHierarchyListener(HierarchyListener)
2287 * @see #getHierarchyListeners()
2288 * @since 1.3
2290 public synchronized void addHierarchyListener(HierarchyListener l)
2292 hierarchyListener = AWTEventMulticaster.add(hierarchyListener, l);
2293 if (hierarchyListener != null)
2294 enableEvents(AWTEvent.HIERARCHY_EVENT_MASK);
2298 * Removes the specified listener from the component. This is harmless if
2299 * the listener was not previously registered.
2301 * @param listener the listener to remove
2302 * @see HierarchyEvent
2303 * @see #addHierarchyListener(HierarchyListener)
2304 * @see #getHierarchyListeners()
2305 * @since 1.3
2307 public synchronized void removeHierarchyListener(HierarchyListener l)
2309 hierarchyListener = AWTEventMulticaster.remove(hierarchyListener, l);
2313 * Returns an array of all specified listeners registered on this component.
2315 * @return an array of listeners
2316 * @see #addHierarchyListener(HierarchyListener)
2317 * @see #removeHierarchyListener(HierarchyListener)
2318 * @since 1.4
2320 public synchronized HierarchyListener[] getHierarchyListeners()
2322 return (HierarchyListener[])
2323 AWTEventMulticaster.getListeners(hierarchyListener,
2324 HierarchyListener.class);
2328 * Adds the specified listener to this component. This is harmless if the
2329 * listener is null, but if the listener has already been registered, it
2330 * will now be registered twice.
2332 * @param listener the new listener to add
2333 * @see HierarchyEvent
2334 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2335 * @see #getHierarchyBoundsListeners()
2336 * @since 1.3
2338 public synchronized void
2339 addHierarchyBoundsListener(HierarchyBoundsListener l)
2341 hierarchyBoundsListener =
2342 AWTEventMulticaster.add(hierarchyBoundsListener, l);
2343 if (hierarchyBoundsListener != null)
2344 enableEvents(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
2348 * Removes the specified listener from the component. This is harmless if
2349 * the listener was not previously registered.
2351 * @param listener the listener to remove
2352 * @see HierarchyEvent
2353 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2354 * @see #getHierarchyBoundsListeners()
2355 * @since 1.3
2357 public synchronized void
2358 removeHierarchyBoundsListener(HierarchyBoundsListener l)
2360 hierarchyBoundsListener =
2361 AWTEventMulticaster.remove(hierarchyBoundsListener, l);
2365 * Returns an array of all specified listeners registered on this component.
2367 * @return an array of listeners
2368 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
2369 * @see #removeHierarchyBoundsListener(HierarchyBoundsListener)
2370 * @since 1.4
2372 public synchronized HierarchyBoundsListener[] getHierarchyBoundsListeners()
2374 return (HierarchyBoundsListener[])
2375 AWTEventMulticaster.getListeners(hierarchyBoundsListener,
2376 HierarchyBoundsListener.class);
2380 * Adds the specified listener to this component. This is harmless if the
2381 * listener is null, but if the listener has already been registered, it
2382 * will now be registered twice.
2384 * @param listener the new listener to add
2385 * @see KeyEvent
2386 * @see #removeKeyListener(KeyListener)
2387 * @see #getKeyListeners()
2388 * @since 1.1
2390 public synchronized void addKeyListener(KeyListener l)
2392 keyListener = AWTEventMulticaster.add(keyListener, l);
2393 if (keyListener != null)
2394 enableEvents(AWTEvent.KEY_EVENT_MASK);
2398 * Removes the specified listener from the component. This is harmless if
2399 * the listener was not previously registered.
2401 * @param listener the listener to remove
2402 * @see KeyEvent
2403 * @see #addKeyListener(KeyListener)
2404 * @see #getKeyListeners()
2405 * @since 1.1
2407 public synchronized void removeKeyListener(KeyListener l)
2409 keyListener = AWTEventMulticaster.remove(keyListener, l);
2413 * Returns an array of all specified listeners registered on this component.
2415 * @return an array of listeners
2416 * @see #addKeyListener(KeyListener)
2417 * @see #removeKeyListener(KeyListener)
2418 * @since 1.4
2420 public synchronized KeyListener[] getKeyListeners()
2422 return (KeyListener[])
2423 AWTEventMulticaster.getListeners(keyListener, KeyListener.class);
2427 * Adds the specified listener to this component. This is harmless if the
2428 * listener is null, but if the listener has already been registered, it
2429 * will now be registered twice.
2431 * @param listener the new listener to add
2432 * @see MouseEvent
2433 * @see #removeMouseListener(MouseListener)
2434 * @see #getMouseListeners()
2435 * @since 1.1
2437 public synchronized void addMouseListener(MouseListener l)
2439 mouseListener = AWTEventMulticaster.add(mouseListener, l);
2440 if (mouseListener != null)
2441 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2445 * Removes the specified listener from the component. This is harmless if
2446 * the listener was not previously registered.
2448 * @param listener the listener to remove
2449 * @see MouseEvent
2450 * @see #addMouseListener(MouseListener)
2451 * @see #getMouseListeners()
2452 * @since 1.1
2454 public synchronized void removeMouseListener(MouseListener l)
2456 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
2460 * Returns an array of all specified listeners registered on this component.
2462 * @return an array of listeners
2463 * @see #addMouseListener(MouseListener)
2464 * @see #removeMouseListener(MouseListener)
2465 * @since 1.4
2467 public synchronized MouseListener[] getMouseListeners()
2469 return (MouseListener[])
2470 AWTEventMulticaster.getListeners(mouseListener, MouseListener.class);
2474 * Adds the specified listener to this component. This is harmless if the
2475 * listener is null, but if the listener has already been registered, it
2476 * will now be registered twice.
2478 * @param listener the new listener to add
2479 * @see MouseEvent
2480 * @see #removeMouseMotionListener(MouseMotionListener)
2481 * @see #getMouseMotionListeners()
2482 * @since 1.1
2484 public synchronized void addMouseMotionListener(MouseMotionListener l)
2486 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, l);
2487 if (mouseMotionListener != null)
2488 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
2492 * Removes the specified listener from the component. This is harmless if
2493 * the listener was not previously registered.
2495 * @param listener the listener to remove
2496 * @see MouseEvent
2497 * @see #addMouseMotionListener(MouseMotionListener)
2498 * @see #getMouseMotionListeners()
2499 * @since 1.1
2501 public synchronized void removeMouseMotionListener(MouseMotionListener l)
2503 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
2507 * Returns an array of all specified listeners registered on this component.
2509 * @return an array of listeners
2510 * @see #addMouseMotionListener(MouseMotionListener)
2511 * @see #removeMouseMotionListener(MouseMotionListener)
2512 * @since 1.4
2514 public synchronized MouseMotionListener[] getMouseMotionListeners()
2516 return (MouseMotionListener[])
2517 AWTEventMulticaster.getListeners(mouseMotionListener,
2518 MouseMotionListener.class);
2522 * Adds the specified listener to this component. This is harmless if the
2523 * listener is null, but if the listener has already been registered, it
2524 * will now be registered twice.
2526 * @param listener the new listener to add
2527 * @see MouseEvent
2528 * @see MouseWheelEvent
2529 * @see #removeMouseWheelListener(MouseWheelListener)
2530 * @see #getMouseWheelListeners()
2531 * @since 1.4
2533 public synchronized void addMouseWheelListener(MouseWheelListener l)
2535 mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, l);
2536 if (mouseWheelListener != null)
2537 enableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK);
2541 * Removes the specified listener from the component. This is harmless if
2542 * the listener was not previously registered.
2544 * @param listener the listener to remove
2545 * @see MouseEvent
2546 * @see MouseWheelEvent
2547 * @see #addMouseWheelListener(MouseWheelListener)
2548 * @see #getMouseWheelListeners()
2549 * @since 1.4
2551 public synchronized void removeMouseWheelListener(MouseWheelListener l)
2553 mouseWheelListener = AWTEventMulticaster.remove(mouseWheelListener, l);
2557 * Returns an array of all specified listeners registered on this component.
2559 * @return an array of listeners
2560 * @see #addMouseWheelListener(MouseWheelListener)
2561 * @see #removeMouseWheelListener(MouseWheelListener)
2562 * @since 1.4
2564 public synchronized MouseWheelListener[] getMouseWheelListeners()
2566 return (MouseWheelListener[])
2567 AWTEventMulticaster.getListeners(mouseWheelListener,
2568 MouseWheelListener.class);
2572 * Adds the specified listener to this component. This is harmless if the
2573 * listener is null, but if the listener has already been registered, it
2574 * will now be registered twice.
2576 * @param listener the new listener to add
2577 * @see InputMethodEvent
2578 * @see #removeInputMethodListener(InputMethodListener)
2579 * @see #getInputMethodListeners()
2580 * @see #getInputMethodRequests()
2581 * @since 1.2
2583 public synchronized void addInputMethodListener(InputMethodListener l)
2585 inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
2586 if (inputMethodListener != null)
2587 enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK);
2591 * Removes the specified listener from the component. This is harmless if
2592 * the listener was not previously registered.
2594 * @param listener the listener to remove
2595 * @see InputMethodEvent
2596 * @see #addInputMethodListener(InputMethodListener)
2597 * @see #getInputMethodRequests()
2598 * @since 1.2
2600 public synchronized void removeInputMethodListener(InputMethodListener l)
2602 inputMethodListener = AWTEventMulticaster.remove(inputMethodListener, l);
2606 * Returns an array of all specified listeners registered on this component.
2608 * @return an array of listeners
2609 * @see #addInputMethodListener(InputMethodListener)
2610 * @see #removeInputMethodListener(InputMethodListener)
2611 * @since 1.4
2613 public synchronized InputMethodListener[] getInputMethodListeners()
2615 return (InputMethodListener[])
2616 AWTEventMulticaster.getListeners(inputMethodListener,
2617 InputMethodListener.class);
2621 * Returns all registered EventListers of the given listenerType.
2623 * @param listenerType the class of listeners to filter
2624 * @return an array of registered listeners
2625 * @see #getComponentListeners()
2626 * @see #getFocusListeners()
2627 * @see #getHierarchyListeners()
2628 * @see #getHierarchyBoundsListeners()
2629 * @see #getKeyListeners()
2630 * @see #getMouseListeners()
2631 * @see #getMouseMotionListeners()
2632 * @see #getMouseWheelListeners()
2633 * @see #getInputMethodListeners()
2634 * @see #getPropertyChangeListeners()
2635 * @since 1.3
2637 public EventListener[] getListeners(Class listenerType)
2639 if (listenerType == ComponentListener.class)
2640 return getComponentListeners();
2641 if (listenerType == FocusListener.class)
2642 return getFocusListeners();
2643 if (listenerType == HierarchyListener.class)
2644 return getHierarchyListeners();
2645 if (listenerType == HierarchyBoundsListener.class)
2646 return getHierarchyBoundsListeners();
2647 if (listenerType == KeyListener.class)
2648 return getKeyListeners();
2649 if (listenerType == MouseListener.class)
2650 return getMouseListeners();
2651 if (listenerType == MouseMotionListener.class)
2652 return getMouseMotionListeners();
2653 if (listenerType == MouseWheelListener.class)
2654 return getMouseWheelListeners();
2655 if (listenerType == InputMethodListener.class)
2656 return getInputMethodListeners();
2657 if (listenerType == PropertyChangeListener.class)
2658 return getPropertyChangeListeners();
2659 return (EventListener[]) Array.newInstance(listenerType, 0);
2663 * Returns the input method request handler, for subclasses which support
2664 * on-the-spot text input. By default, input methods are handled by AWT,
2665 * and this returns null.
2667 * @return the input method handler, null by default
2668 * @since 1.2
2670 public InputMethodRequests getInputMethodRequests()
2672 return null;
2676 * Gets the input context of this component, which is inherited from the
2677 * parent unless this is overridden.
2679 * @return the text input context
2680 * @since 1.2
2682 public InputContext getInputContext()
2684 return parent == null ? null : parent.getInputContext();
2688 * Enables the specified events. The events to enable are specified
2689 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2691 * <p>Events are enabled by default when a listener is attached to the
2692 * component for that event type. This method can be used by subclasses
2693 * to ensure the delivery of a specified event regardless of whether
2694 * or not a listener is attached.
2696 * @param eventsToEnable the desired events to enable
2697 * @see #processEvent(AWTEvent)
2698 * @see #disableEvents(long)
2699 * @see AWTEvent
2700 * @since 1.1
2702 protected final void enableEvents(long eventsToEnable)
2704 eventMask |= eventsToEnable;
2705 // TODO: Unlike Sun's implementation, I think we should try and
2706 // enable/disable events at the peer (gtk/X) level. This will avoid
2707 // clogging the event pipeline with useless mousemove events that
2708 // we arn't interested in, etc. This will involve extending the peer
2709 // interface, but thats okay because the peer interfaces have been
2710 // deprecated for a long time, and no longer feature in the
2711 // API specification at all.
2712 if (isLightweight() && parent != null)
2713 parent.enableEvents(eventsToEnable);
2714 else if (peer != null)
2715 peer.setEventMask(eventMask);
2719 * Disables the specified events. The events to disable are specified
2720 * by OR-ing together the desired masks from <code>AWTEvent</code>.
2722 * @param eventsToDisable the desired events to disable
2723 * @see #enableEvents(long)
2724 * @since 1.1
2726 protected final void disableEvents(long eventsToDisable)
2728 eventMask &= ~eventsToDisable;
2729 // forward new event mask to peer?
2733 * This is called by the EventQueue if two events with the same event id
2734 * and owner component are queued. Returns a new combined event, or null if
2735 * no combining is done. The coelesced events are currently mouse moves
2736 * (intermediate ones are discarded) and paint events (a merged paint is
2737 * created in place of the two events).
2739 * @param existingEvent the event on the queue
2740 * @param newEvent the new event that might be entered on the queue
2741 * @return null if both events are kept, or the replacement coelesced event
2743 protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent)
2745 switch (existingEvent.id)
2747 case MouseEvent.MOUSE_MOVED:
2748 case MouseEvent.MOUSE_DRAGGED:
2749 // Just drop the old (intermediate) event and return the new one.
2750 return newEvent;
2751 case PaintEvent.PAINT:
2752 case PaintEvent.UPDATE:
2753 return coalescePaintEvents((PaintEvent) existingEvent,
2754 (PaintEvent) newEvent);
2755 default:
2756 return null;
2761 * Processes the specified event. In this class, this method simply
2762 * calls one of the more specific event handlers.
2764 * @param event the event to process
2765 * @throws NullPointerException if e is null
2766 * @see #processComponentEvent(ComponentEvent)
2767 * @see #processFocusEvent(FocusEvent)
2768 * @see #processKeyEvent(KeyEvent)
2769 * @see #processMouseEvent(MouseEvent)
2770 * @see #processMouseMotionEvent(MouseEvent)
2771 * @see #processInputMethodEvent(InputMethodEvent)
2772 * @see #processHierarchyEvent(HierarchyEvent)
2773 * @see #processMouseWheelEvent(MouseWheelEvent)
2774 * @since 1.1
2776 protected void processEvent(AWTEvent e)
2778 /* Note: the order of these if statements are
2779 important. Subclasses must be checked first. Eg. MouseEvent
2780 must be checked before ComponentEvent, since a MouseEvent
2781 object is also an instance of a ComponentEvent. */
2783 if (e instanceof FocusEvent)
2784 processFocusEvent((FocusEvent) e);
2785 else if (e instanceof PaintEvent)
2786 processPaintEvent((PaintEvent) e);
2787 else if (e instanceof MouseWheelEvent)
2788 processMouseWheelEvent((MouseWheelEvent) e);
2789 else if (e instanceof MouseEvent)
2791 if (e.id == MouseEvent.MOUSE_MOVED
2792 || e.id == MouseEvent.MOUSE_DRAGGED)
2793 processMouseMotionEvent((MouseEvent) e);
2794 else
2795 processMouseEvent((MouseEvent) e);
2797 else if (e instanceof KeyEvent)
2798 processKeyEvent((KeyEvent) e);
2799 else if (e instanceof InputMethodEvent)
2800 processInputMethodEvent((InputMethodEvent) e);
2801 else if (e instanceof ComponentEvent)
2802 processComponentEvent((ComponentEvent) e);
2803 else if (e instanceof HierarchyEvent)
2805 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
2806 processHierarchyEvent((HierarchyEvent) e);
2807 else
2808 processHierarchyBoundsEvent((HierarchyEvent) e);
2813 * Called when a component event is dispatched and component events are
2814 * enabled. This method passes the event along to any listeners
2815 * that are attached.
2817 * @param event the <code>ComponentEvent</code> to process
2818 * @throws NullPointerException if e is null
2819 * @see ComponentListener
2820 * @see #addComponentListener(ComponentListener)
2821 * @see #enableEvents(long)
2822 * @since 1.1
2824 protected void processComponentEvent(ComponentEvent e)
2826 if (componentListener == null)
2827 return;
2828 switch (e.id)
2830 case ComponentEvent.COMPONENT_HIDDEN:
2831 componentListener.componentHidden(e);
2832 break;
2833 case ComponentEvent.COMPONENT_MOVED:
2834 componentListener.componentMoved(e);
2835 break;
2836 case ComponentEvent.COMPONENT_RESIZED:
2837 componentListener.componentResized(e);
2838 break;
2839 case ComponentEvent.COMPONENT_SHOWN:
2840 componentListener.componentShown(e);
2841 break;
2846 * Called when a focus event is dispatched and component events are
2847 * enabled. This method passes the event along to any listeners
2848 * that are attached.
2850 * @param event the <code>FocusEvent</code> to process
2851 * @throws NullPointerException if e is null
2852 * @see FocusListener
2853 * @see #addFocusListener(FocusListener)
2854 * @see #enableEvents(long)
2855 * @since 1.1
2857 protected void processFocusEvent(FocusEvent e)
2859 if (focusListener == null)
2860 return;
2861 switch (e.id)
2863 case FocusEvent.FOCUS_GAINED:
2864 focusListener.focusGained(e);
2865 break;
2866 case FocusEvent.FOCUS_LOST:
2867 focusListener.focusLost(e);
2868 break;
2873 * Called when a key event is dispatched and component events are
2874 * enabled. This method passes the event along to any listeners
2875 * that are attached.
2877 * @param event the <code>KeyEvent</code> to process
2878 * @throws NullPointerException if e is null
2879 * @see KeyListener
2880 * @see #addKeyListener(KeyListener)
2881 * @see #enableEvents(long)
2882 * @since 1.1
2884 protected void processKeyEvent(KeyEvent e)
2886 if (keyListener == null)
2887 return;
2888 switch (e.id)
2890 case KeyEvent.KEY_PRESSED:
2891 keyListener.keyPressed(e);
2892 break;
2893 case KeyEvent.KEY_RELEASED:
2894 keyListener.keyReleased(e);
2895 break;
2896 case KeyEvent.KEY_TYPED:
2897 keyListener.keyTyped(e);
2898 break;
2903 * Called when a regular mouse event is dispatched and component events are
2904 * enabled. This method passes the event along to any listeners
2905 * that are attached.
2907 * @param event the <code>MouseEvent</code> to process
2908 * @throws NullPointerException if e is null
2909 * @see MouseListener
2910 * @see #addMouseListener(MouseListener)
2911 * @see #enableEvents(long)
2912 * @since 1.1
2914 protected void processMouseEvent(MouseEvent e)
2916 if (mouseListener == null)
2917 return;
2918 switch (e.id)
2920 case MouseEvent.MOUSE_CLICKED:
2921 mouseListener.mouseClicked(e);
2922 break;
2923 case MouseEvent.MOUSE_ENTERED:
2924 mouseListener.mouseEntered(e);
2925 break;
2926 case MouseEvent.MOUSE_EXITED:
2927 mouseListener.mouseExited(e);
2928 break;
2929 case MouseEvent.MOUSE_PRESSED:
2930 mouseListener.mousePressed(e);
2931 break;
2932 case MouseEvent.MOUSE_RELEASED:
2933 mouseListener.mouseReleased(e);
2934 break;
2939 * Called when a mouse motion event is dispatched and component events are
2940 * enabled. This method passes the event along to any listeners
2941 * that are attached.
2943 * @param event the <code>MouseMotionEvent</code> to process
2944 * @throws NullPointerException if e is null
2945 * @see MouseMotionListener
2946 * @see #addMouseMotionListener(MouseMotionListener)
2947 * @see #enableEvents(long)
2948 * @since 1.1
2950 protected void processMouseMotionEvent(MouseEvent e)
2952 if (mouseMotionListener == null)
2953 return;
2954 switch (e.id)
2956 case MouseEvent.MOUSE_DRAGGED:
2957 mouseMotionListener.mouseDragged(e);
2958 break;
2959 case MouseEvent.MOUSE_MOVED:
2960 mouseMotionListener.mouseMoved(e);
2961 break;
2966 * Called when a mouse wheel event is dispatched and component events are
2967 * enabled. This method passes the event along to any listeners that are
2968 * attached.
2970 * @param event the <code>MouseWheelEvent</code> to process
2971 * @throws NullPointerException if e is null
2972 * @see MouseWheelListener
2973 * @see #addMouseWheelListener(MouseWheelListener)
2974 * @see #enableEvents(long)
2975 * @since 1.4
2977 protected void processMouseWheelEvent(MouseWheelEvent e)
2979 if (mouseWheelListener != null
2980 && e.id == MouseEvent.MOUSE_WHEEL)
2981 mouseWheelListener.mouseWheelMoved(e);
2985 * Called when an input method event is dispatched and component events are
2986 * enabled. This method passes the event along to any listeners that are
2987 * attached.
2989 * @param event the <code>InputMethodEvent</code> to process
2990 * @throws NullPointerException if e is null
2991 * @see InputMethodListener
2992 * @see #addInputMethodListener(InputMethodListener)
2993 * @see #enableEvents(long)
2994 * @since 1.2
2996 protected void processInputMethodEvent(InputMethodEvent e)
2998 if (inputMethodListener == null)
2999 return;
3000 switch (e.id)
3002 case InputMethodEvent.CARET_POSITION_CHANGED:
3003 inputMethodListener.caretPositionChanged(e);
3004 break;
3005 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
3006 inputMethodListener.inputMethodTextChanged(e);
3007 break;
3012 * Called when a hierarchy change event is dispatched and component events
3013 * are enabled. This method passes the event along to any listeners that are
3014 * attached.
3016 * @param event the <code>HierarchyEvent</code> to process
3017 * @throws NullPointerException if e is null
3018 * @see HierarchyListener
3019 * @see #addHierarchyListener(HierarchyListener)
3020 * @see #enableEvents(long)
3021 * @since 1.3
3023 protected void processHierarchyEvent(HierarchyEvent e)
3025 if (hierarchyListener == null)
3026 return;
3027 if (e.id == HierarchyEvent.HIERARCHY_CHANGED)
3028 hierarchyListener.hierarchyChanged(e);
3032 * Called when a hierarchy bounds event is dispatched and component events
3033 * are enabled. This method passes the event along to any listeners that are
3034 * attached.
3036 * @param event the <code>HierarchyEvent</code> to process
3037 * @throws NullPointerException if e is null
3038 * @see HierarchyBoundsListener
3039 * @see #addHierarchyBoundsListener(HierarchyBoundsListener)
3040 * @see #enableEvents(long)
3041 * @since 1.3
3043 protected void processHierarchyBoundsEvent(HierarchyEvent e)
3045 if (hierarchyBoundsListener == null)
3046 return;
3047 switch (e.id)
3049 case HierarchyEvent.ANCESTOR_MOVED:
3050 hierarchyBoundsListener.ancestorMoved(e);
3051 break;
3052 case HierarchyEvent.ANCESTOR_RESIZED:
3053 hierarchyBoundsListener.ancestorResized(e);
3054 break;
3059 * AWT 1.0 event processor.
3061 * @param evt the event to handle
3062 * @return false: since the method was deprecated, the return has no meaning
3063 * @deprecated use {@link #processEvent(AWTEvent)} instead
3065 public boolean handleEvent(Event evt)
3067 // XXX Add backward compatibility handling.
3068 return false;
3072 * AWT 1.0 mouse event.
3074 * @param evt the event to handle
3075 * @param x the x coordinate, ignored
3076 * @param y the y coordinate, ignored
3077 * @return false: since the method was deprecated, the return has no meaning
3078 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3080 public boolean mouseDown(Event evt, int x, int y)
3082 // XXX Add backward compatibility handling.
3083 return false;
3087 * AWT 1.0 mouse event.
3089 * @param evt the event to handle
3090 * @param x the x coordinate, ignored
3091 * @param y the y coordinate, ignored
3092 * @return false: since the method was deprecated, the return has no meaning
3093 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3095 public boolean mouseDrag(Event evt, int x, int y)
3097 // XXX Add backward compatibility handling.
3098 return false;
3102 * AWT 1.0 mouse event.
3104 * @param evt the event to handle
3105 * @param x the x coordinate, ignored
3106 * @param y the y coordinate, ignored
3107 * @return false: since the method was deprecated, the return has no meaning
3108 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3110 public boolean mouseUp(Event evt, int x, int y)
3112 // XXX Add backward compatibility handling.
3113 return false;
3117 * AWT 1.0 mouse event.
3119 * @param evt the event to handle
3120 * @param x the x coordinate, ignored
3121 * @param y the y coordinate, ignored
3122 * @return false: since the method was deprecated, the return has no meaning
3123 * @deprecated use {@link #processMouseMotionEvent(MouseEvent)} instead
3125 public boolean mouseMove(Event evt, int x, int y)
3127 // XXX Add backward compatibility handling.
3128 return false;
3132 * AWT 1.0 mouse event.
3134 * @param evt the event to handle
3135 * @param x the x coordinate, ignored
3136 * @param y the y coordinate, ignored
3137 * @return false: since the method was deprecated, the return has no meaning
3138 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3140 public boolean mouseEnter(Event evt, int x, int y)
3142 // XXX Add backward compatibility handling.
3143 return false;
3147 * AWT 1.0 mouse event.
3149 * @param evt the event to handle
3150 * @param x the x coordinate, ignored
3151 * @param y the y coordinate, ignored
3152 * @return false: since the method was deprecated, the return has no meaning
3153 * @deprecated use {@link #processMouseEvent(MouseEvent)} instead
3155 public boolean mouseExit(Event evt, int x, int y)
3157 // XXX Add backward compatibility handling.
3158 return false;
3162 * AWT 1.0 key press event.
3164 * @param evt the event to handle
3165 * @param key the key pressed, ignored
3166 * @return false: since the method was deprecated, the return has no meaning
3167 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3169 public boolean keyDown(Event evt, int key)
3171 // XXX Add backward compatibility handling.
3172 return false;
3176 * AWT 1.0 key press event.
3178 * @param evt the event to handle
3179 * @param key the key pressed, ignored
3180 * @return false: since the method was deprecated, the return has no meaning
3181 * @deprecated use {@link #processKeyEvent(KeyEvent)} instead
3183 public boolean keyUp(Event evt, int key)
3185 // XXX Add backward compatibility handling.
3186 return false;
3190 * AWT 1.0 action event processor.
3192 * @param evt the event to handle
3193 * @param what the object acted on, ignored
3194 * @return false: since the method was deprecated, the return has no meaning
3195 * @deprecated in classes which support actions, use
3196 * <code>processActionEvent(ActionEvent)</code> instead
3198 public boolean action(Event evt, Object what)
3200 // XXX Add backward compatibility handling.
3201 return false;
3205 * Called to inform this component it has been added to a container.
3206 * A native peer - if any - is created at this time. This method is
3207 * called automatically by the AWT system and should not be called by
3208 * user level code.
3210 * @see #isDisplayable()
3211 * @see #removeNotify()
3213 public void addNotify()
3215 if (peer == null)
3216 peer = getToolkit().createComponent(this);
3217 /* Now that all the children has gotten their peers, we should
3218 have the event mask needed for this component and its
3219 lightweight subcomponents. */
3220 peer.setEventMask(eventMask);
3221 /* We do not invalidate here, but rather leave that job up to
3222 the peer. For efficiency, the peer can choose not to
3223 invalidate if it is happy with the current dimensions,
3224 etc. */
3228 * Called to inform this component is has been removed from its
3229 * container. Its native peer - if any - is destroyed at this time.
3230 * This method is called automatically by the AWT system and should
3231 * not be called by user level code.
3233 * @see #isDisplayable()
3234 * @see #addNotify()
3236 public void removeNotify()
3238 if (peer != null)
3239 peer.dispose();
3240 peer = null;
3244 * AWT 1.0 focus event.
3246 * @param evt the event to handle
3247 * @param what the Object focused, ignored
3248 * @return false: since the method was deprecated, the return has no meaning
3249 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3251 public boolean gotFocus(Event evt, Object what)
3253 // XXX Add backward compatibility handling.
3254 return false;
3258 * AWT 1.0 focus event.
3260 * @param evt the event to handle
3261 * @param what the Object focused, ignored
3262 * @return false: since the method was deprecated, the return has no meaning
3263 * @deprecated use {@link #processFocusEvent(FocusEvent)} instead
3265 public boolean lostFocus(Event evt, Object what)
3267 // XXX Add backward compatibility handling.
3268 return false;
3272 * Tests whether or not this component is in the group that can be
3273 * traversed using the keyboard traversal mechanism (such as the TAB key).
3275 * @return true if the component is traversed via the TAB key
3276 * @see #setFocusable(boolean)
3277 * @since 1.1
3278 * @deprecated use {@link #isFocusable()} instead
3280 public boolean isFocusTraversable()
3282 return enabled && visible && (peer == null || peer.isFocusTraversable());
3286 * Tests if this component can receive focus.
3288 * @return true if this component can receive focus
3289 * @since 1.4
3291 public boolean isFocusable()
3293 return focusable;
3297 * Specify whether this component can receive focus.
3299 * @param focusable the new focusable status
3300 * @since 1.4
3302 public void setFocusable(boolean focusable)
3304 firePropertyChange("focusable", this.focusable, focusable);
3305 this.focusable = focusable;
3309 * Sets the focus traversal keys for a given type of focus events. Normally,
3310 * the default values should match the operating system's native choices. To
3311 * disable a given traversal, use <code>Collections.EMPTY_SET</code>. The
3312 * event dispatcher will consume PRESSED, RELEASED, and TYPED events for the
3313 * specified key, although focus can only transfer on PRESSED or RELEASED.
3315 * <p>The defauts are:
3316 * <table>
3317 * <th><td>Identifier</td><td>Meaning</td><td>Default</td></th>
3318 * <tr><td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
3319 * <td>Normal forward traversal</td>
3320 * <td>TAB on KEY_PRESSED, Ctrl-TAB on KEY_PRESSED</td></tr>
3321 * <tr><td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
3322 * <td>Normal backward traversal</td>
3323 * <td>Shift-TAB on KEY_PRESSED, Ctrl-Shift-TAB on KEY_PRESSED</td></tr>
3324 * <tr><td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
3325 * <td>Go up a traversal cycle</td><td>None</td></tr>
3326 * </table>
3328 * <p>Specifying null allows inheritance from the parent, or from the current
3329 * KeyboardFocusManager default set. If not null, the set must contain only
3330 * AWTKeyStrokes that are not already focus keys and are not KEY_TYPED
3331 * events.
3333 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3334 * UP_CYCLE_TRAVERSAL_KEYS
3335 * @param keystrokes a set of keys, or null
3336 * @throws IllegalArgumentException if id or keystrokes is invalid
3337 * @see #getFocusTraversalKeys(int)
3338 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3339 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3340 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3341 * @since 1.4
3343 public void setFocusTraversalKeys(int id, Set keystrokes)
3345 if (keystrokes == null)
3346 throw new IllegalArgumentException();
3347 Set sa;
3348 Set sb;
3349 String name;
3350 switch (id)
3352 case KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS:
3353 sa = getFocusTraversalKeys
3354 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3355 sb = getFocusTraversalKeys
3356 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3357 name = "forwardFocusTraversalKeys";
3358 break;
3359 case KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS:
3360 sa = getFocusTraversalKeys
3361 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3362 sb = getFocusTraversalKeys
3363 (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
3364 name = "backwardFocusTraversalKeys";
3365 break;
3366 case KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS:
3367 sa = getFocusTraversalKeys
3368 (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
3369 sb = getFocusTraversalKeys
3370 (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
3371 name = "upCycleFocusTraversalKeys";
3372 break;
3373 default:
3374 throw new IllegalArgumentException();
3376 int i = keystrokes.size();
3377 Iterator iter = keystrokes.iterator();
3378 while (--i >= 0)
3380 Object o = iter.next();
3381 if (! (o instanceof AWTKeyStroke)
3382 || sa.contains(o) || sb.contains(o)
3383 || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED)
3384 throw new IllegalArgumentException();
3386 if (focusTraversalKeys == null)
3387 focusTraversalKeys = new Set[3];
3388 keystrokes = Collections.unmodifiableSet(new HashSet(keystrokes));
3389 firePropertyChange(name, focusTraversalKeys[id], keystrokes);
3390 focusTraversalKeys[id] = keystrokes;
3394 * Returns the set of keys for a given focus traversal action, as defined
3395 * in <code>setFocusTraversalKeys</code>. If not set, this is inherited from
3396 * the parent component, which may have gotten it from the
3397 * KeyboardFocusManager.
3399 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3400 * UP_CYCLE_TRAVERSAL_KEYS
3401 * @throws IllegalArgumentException if id is invalid
3402 * @see #setFocusTraversalKeys(int, Set)
3403 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3404 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3405 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3406 * @since 1.4
3408 public Set getFocusTraversalKeys(int id)
3410 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3411 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3412 throw new IllegalArgumentException();
3413 Set s = null;
3414 if (focusTraversalKeys != null)
3415 s = focusTraversalKeys[id];
3416 if (s == null && parent != null)
3417 s = parent.getFocusTraversalKeys(id);
3418 return s == null ? (KeyboardFocusManager.getCurrentKeyboardFocusManager()
3419 .getDefaultFocusTraversalKeys(id)) : s;
3423 * Tests whether the focus traversal keys for a given action are explicitly
3424 * set or inherited.
3426 * @param id one of FORWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, or
3427 * UP_CYCLE_TRAVERSAL_KEYS
3428 * @return true if that set is explicitly specified
3429 * @throws IllegalArgumentException if id is invalid
3430 * @see #getFocusTraversalKeys(int)
3431 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3432 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3433 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3434 * @since 1.4
3436 public boolean areFocusTraversalKeysSet(int id)
3438 if (id < KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
3439 || id > KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS)
3440 throw new IllegalArgumentException();
3441 return focusTraversalKeys != null && focusTraversalKeys[id] != null;
3445 * Sets whether focus traversal keys are enabled, which consumes traversal
3446 * keys and performs the focus event automatically.
3448 * @param focusTraversalKeysEnabled the new value of the flag
3449 * @see #getFocusTraversalKeysEnabled()
3450 * @see #setFocusTraversalKeys(int, Set)
3451 * @see #getFocusTraversalKeys(int)
3452 * @since 1.4
3454 public void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)
3456 firePropertyChange("focusTraversalKeysEnabled",
3457 this.focusTraversalKeysEnabled,
3458 focusTraversalKeysEnabled);
3459 this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
3463 * Tests whether focus traversal keys are enabled. If they are, then focus
3464 * traversal keys are consumed and focus events performed automatically,
3465 * without the component seeing the keystrokes.
3467 * @return true if focus traversal is enabled
3468 * @see #setFocusTraversalKeysEnabled(boolean)
3469 * @see #setFocusTraversalKeys(int, Set)
3470 * @see #getFocusTraversalKeys(int)
3471 * @since 1.4
3473 public boolean getFocusTraversalKeysEnabled()
3475 return focusTraversalKeysEnabled;
3479 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3480 * event will be fired if and only if this request is successful. To be
3481 * successful, the component must be displayable, visible, and focusable,
3482 * and the top-level Window must be able to receive focus. Thus, this
3483 * request may fail, or be delayed until the window receives focus. It is
3484 * recommended that <code>requestFocusInWindow</code> be used where
3485 * possible to be more platform-independent.
3487 * @see #requestFocusInWindow()
3488 * @see FocusEvent
3489 * @see #addFocusListener(FocusListener)
3490 * @see #isFocusable()
3491 * @see #isDisplayable()
3492 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3494 public void requestFocus()
3496 // If there's no peer then this component can't get the focus. We
3497 // treat it as a silent rejection of the request.
3498 if (peer != null)
3499 peer.requestFocus();
3503 * Requests that this component be given focus. A <code>FOCUS_GAINED</code>
3504 * event will be fired if and only if this request is successful. To be
3505 * successful, the component must be displayable, visible, and focusable,
3506 * and the top-level Window must be able to receive focus. Thus, this
3507 * request may fail, or be delayed until the window receives focus. It is
3508 * recommended that <code>requestFocusInWindow</code> be used where
3509 * possible to be more platform-independent.
3511 * <p>If the return value is false, the request is guaranteed to fail. If
3512 * it is true, it will likely succeed unless the action is vetoed or
3513 * something in the native windowing system intervenes. The temporary flag,
3514 * and thus this method in general, is not designed for public use; rather
3515 * it is a hook for lightweight components to notify their container in
3516 * an attempt to reduce the amount of repainting necessary.
3518 * @param temporary true if the focus request is temporary
3519 * @return true if the request has a chance of success
3520 * @see #requestFocusInWindow()
3521 * @see FocusEvent
3522 * @see #addFocusListener(FocusListener)
3523 * @see #isFocusable()
3524 * @see #isDisplayable()
3525 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3526 * @since 1.4
3528 protected boolean requestFocus(boolean temporary)
3530 // XXX Implement correctly.
3531 requestFocus();
3532 return true;
3536 * Requests that this component be given focus, if it resides in the
3537 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3538 * event will be fired if and only if this request is successful. To be
3539 * successful, the component must be displayable, visible, and focusable,
3540 * and the top-level Window must be focused.
3542 * <p>If the return value is false, the request is guaranteed to fail. If
3543 * it is true, it will likely succeed unless the action is vetoed or
3544 * something in the native windowing system intervenes. The temporary flag,
3545 * and thus this method in general, is not designed for public use; rather
3546 * it is a hook for lightweight components to notify their container in
3547 * an attempt to reduce the amount of repainting necessary.
3549 * @return true if the request has a chance of success
3550 * @see #requestFocus()
3551 * @see FocusEvent
3552 * @see #addFocusListener(FocusListener)
3553 * @see #isFocusable()
3554 * @see #isDisplayable()
3555 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3556 * @since 1.4
3558 public boolean requestFocusInWindow()
3560 // XXX Implement correctly.
3561 requestFocus();
3562 return true;
3566 * Requests that this component be given focus, if it resides in the
3567 * top-level window which already has focus. A <code>FOCUS_GAINED</code>
3568 * event will be fired if and only if this request is successful. To be
3569 * successful, the component must be displayable, visible, and focusable,
3570 * and the top-level Window must be focused.
3572 * <p>If the return value is false, the request is guaranteed to fail. If
3573 * it is true, it will likely succeed unless the action is vetoed or
3574 * something in the native windowing system intervenes. The temporary flag,
3575 * and thus this method in general, is not designed for public use; rather
3576 * it is a hook for lightweight components to notify their container in
3577 * an attempt to reduce the amount of repainting necessary.
3579 * @param temporary true if the focus request is temporary
3580 * @return true if the request has a chance of success
3581 * @see #requestFocus()
3582 * @see FocusEvent
3583 * @see #addFocusListener(FocusListener)
3584 * @see #isFocusable()
3585 * @see #isDisplayable()
3586 * @see KeyboardFocusManager#clearGlobalFocusOwner()
3587 * @since 1.4
3589 protected boolean requestFocusInWindow(boolean temporary)
3591 // XXX Implement correctly.
3592 requestFocus();
3593 return true;
3597 * Transfers focus to the next component in the focus traversal order, as
3598 * though this were the current focus owner.
3600 * @see #requestFocus()
3601 * @since 1.1
3603 public void transferFocus()
3605 Component next;
3606 if (parent == null)
3607 next = findNextFocusComponent(null);
3608 else
3609 next = parent.findNextFocusComponent(this);
3610 if (next != null && next != this)
3611 next.requestFocus();
3615 * Returns the root container that owns the focus cycle where this component
3616 * resides. A focus cycle root is in two cycles, one as the ancestor, and
3617 * one as the focusable element; this call always returns the ancestor.
3619 * @return the ancestor container that owns the focus cycle
3620 * @since 1.4
3622 public Container getFocusCycleRootAncestor()
3624 // XXX Implement.
3625 throw new Error("not implemented");
3629 * Tests if the container is the ancestor of the focus cycle that this
3630 * component belongs to.
3632 * @param c the container to test
3633 * @return true if c is the focus cycle root
3634 * @since 1.4
3636 public boolean isFocusCycleRoot(Container c)
3638 return c == getFocusCycleRootAncestor();
3642 * AWT 1.0 focus event processor.
3644 * @deprecated use {@link #transferFocus()} instead
3646 public void nextFocus()
3648 transferFocus();
3652 * Transfers focus to the previous component in the focus traversal order, as
3653 * though this were the current focus owner.
3655 * @see #requestFocus()
3656 * @since 1.4
3658 public void transferFocusBackward()
3660 // XXX Implement.
3661 throw new Error("not implemented");
3665 * Transfers focus to the focus cycle root of this component. However, if
3666 * this is a Window, the default focus owner in the window in the current
3667 * focus cycle is focused instead.
3669 * @see #requestFocus()
3670 * @see #isFocusCycleRoot()
3671 * @since 1.4
3673 public void transferFocusUpCycle()
3675 // XXX Implement.
3676 throw new Error("not implemented");
3680 * Tests if this component is the focus owner. Use {@link #isFocusOwner()}
3681 * instead.
3683 * @return true if this component owns focus
3684 * @since 1.2
3686 public boolean hasFocus()
3688 return isFocusOwner();
3692 * Tests if this component is the focus owner.
3694 * @return true if this component owns focus
3695 * @since 1.4
3697 public boolean isFocusOwner()
3699 // XXX Implement.
3700 throw new Error("not implemented");
3704 * Adds the specified popup menu to this component.
3706 * @param menu the popup menu to be added
3707 * @see #remove(MenuComponent)
3708 * @since 1.1
3710 public synchronized void add(PopupMenu popup)
3712 if (popups == null)
3713 popups = new Vector();
3714 popups.add(popup);
3718 * Removes the specified popup menu from this component.
3720 * @param menu the popup menu to remove
3721 * @see #add(PopupMenu)
3722 * @since 1.1
3724 public synchronized void remove(MenuComponent popup)
3726 if (popups != null)
3727 popups.remove(popup);
3731 * Returns a debugging string representing this component. The string may
3732 * be empty but not null.
3734 * @return a string representing this component
3736 protected String paramString()
3738 StringBuffer param = new StringBuffer();
3739 String name = getName();
3740 if (name != null)
3741 param.append(name).append(",");
3742 param.append(width).append("x").append(height).append("+").append(x)
3743 .append("+").append(y);
3744 if (! isValid())
3745 param.append(",invalid");
3746 if (! isVisible())
3747 param.append(",invisible");
3748 if (! isEnabled())
3749 param.append(",disabled");
3750 if (! isOpaque())
3751 param.append(",translucent");
3752 if (isDoubleBuffered())
3753 param.append(",doublebuffered");
3754 return param.toString();
3758 * Returns a string representation of this component. This is implemented
3759 * as <code>getClass().getName() + '[' + paramString() + ']'</code>.
3761 * @return a string representation of this component
3763 public String toString()
3765 return getClass().getName() + '[' + paramString() + ']';
3769 * Prints a listing of this component to <code>System.out</code>.
3771 * @see #list(PrintStream)
3773 public void list()
3775 list(System.out, 0);
3779 * Prints a listing of this component to the specified print stream.
3781 * @param stream the <code>PrintStream</code> to print to
3783 public void list(PrintStream out)
3785 list(out, 0);
3789 * Prints a listing of this component to the specified print stream,
3790 * starting at the specified indentation point.
3792 * @param stream the <code>PrintStream</code> to print to
3793 * @param indent the indentation point
3795 public void list(PrintStream out, int indent)
3797 for (int i = 0; i < indent; ++i)
3798 out.print(' ');
3799 out.println(toString());
3803 * Prints a listing of this component to the specified print writer.
3805 * @param writer the <code>PrintWrinter</code> to print to
3806 * @since 1.1
3808 public void list(PrintWriter out)
3810 list(out, 0);
3814 * Prints a listing of this component to the specified print writer,
3815 * starting at the specified indentation point.
3817 * @param writer the <code>PrintWriter</code> to print to
3818 * @param indent the indentation point
3819 * @since 1.1
3821 public void list(PrintWriter out, int indent)
3823 for (int i = 0; i < indent; ++i)
3824 out.print(' ');
3825 out.println(toString());
3829 * Adds the specified property listener to this component. This is harmless
3830 * if the listener is null, but if the listener has already been registered,
3831 * it will now be registered twice. The property listener ignores inherited
3832 * properties. Recognized properties include:<br>
3833 * <ul>
3834 * <li>the font (<code>"font"</code>)</li>
3835 * <li>the background color (<code>"background"</code>)</li>
3836 * <li>the foreground color (<code>"foreground"</code>)</li>
3837 * <li>the focusability (<code>"focusable"</code>)</li>
3838 * <li>the focus key traversal enabled state
3839 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3840 * <li>the set of forward traversal keys
3841 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3842 * <li>the set of backward traversal keys
3843 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3844 * <li>the set of up-cycle traversal keys
3845 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3846 * </ul>
3848 * @param listener the new listener to add
3849 * @see #removePropertyChangeListener(PropertyChangeListener)
3850 * @see #getPropertyChangeListeners()
3851 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3852 * @since 1.1
3854 public void addPropertyChangeListener(PropertyChangeListener listener)
3856 if (changeSupport == null)
3857 changeSupport = new PropertyChangeSupport(this);
3858 changeSupport.addPropertyChangeListener(listener);
3862 * Removes the specified property listener from the component. This is
3863 * harmless if the listener was not previously registered.
3865 * @param listener the listener to remove
3866 * @see #addPropertyChangeListener(PropertyChangeListener)
3867 * @see #getPropertyChangeListeners()
3868 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3869 * @since 1.1
3871 public void removePropertyChangeListener(PropertyChangeListener listener)
3873 if (changeSupport != null)
3874 changeSupport.removePropertyChangeListener(listener);
3878 * Returns an array of all specified listeners registered on this component.
3880 * @return an array of listeners
3881 * @see #addPropertyChangeListener(PropertyChangeListener)
3882 * @see #removePropertyChangeListener(PropertyChangeListener)
3883 * @see #getPropertyChangeListeners(String)
3884 * @since 1.4
3886 public PropertyChangeListener[] getPropertyChangeListeners()
3888 return changeSupport == null ? new PropertyChangeListener[0]
3889 : changeSupport.getPropertyChangeListeners();
3893 * Adds the specified property listener to this component. This is harmless
3894 * if the listener is null, but if the listener has already been registered,
3895 * it will now be registered twice. The property listener ignores inherited
3896 * properties. The listener is keyed to a single property. Recognized
3897 * properties include:<br>
3898 * <ul>
3899 * <li>the font (<code>"font"</code>)</li>
3900 * <li>the background color (<code>"background"</code>)</li>
3901 * <li>the foreground color (<code>"foreground"</code>)</li>
3902 * <li>the focusability (<code>"focusable"</code>)</li>
3903 * <li>the focus key traversal enabled state
3904 * (<code>"focusTraversalKeysEnabled"</code>)</li>
3905 * <li>the set of forward traversal keys
3906 * (<code>"forwardFocusTraversalKeys"</code>)</li>
3907 p * <li>the set of backward traversal keys
3908 * (<code>"backwardFocusTraversalKeys"</code>)</li>
3909 * <li>the set of up-cycle traversal keys
3910 * (<code>"upCycleFocusTraversalKeys"</code>)</li>
3911 * </ul>
3913 * @param propertyName the property name to filter on
3914 * @param listener the new listener to add
3915 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3916 * @see #getPropertyChangeListeners(String)
3917 * @see #addPropertyChangeListener(PropertyChangeListener)
3918 * @since 1.1
3920 public void addPropertyChangeListener(String propertyName,
3921 PropertyChangeListener listener)
3923 if (changeSupport == null)
3924 changeSupport = new PropertyChangeSupport(this);
3925 changeSupport.addPropertyChangeListener(propertyName, listener);
3929 * Removes the specified property listener on a particular property from
3930 * the component. This is harmless if the listener was not previously
3931 * registered.
3933 * @param propertyName the property name to filter on
3934 * @param listener the listener to remove
3935 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3936 * @see #getPropertyChangeListeners(String)
3937 * @see #removePropertyChangeListener(PropertyChangeListener)
3938 * @since 1.1
3940 public void removePropertyChangeListener(String propertyName,
3941 PropertyChangeListener listener)
3943 if (changeSupport != null)
3944 changeSupport.removePropertyChangeListener(propertyName, listener);
3948 * Returns an array of all specified listeners on the named property that
3949 * are registered on this component.
3951 * @return an array of listeners
3952 * @see #addPropertyChangeListener(String, PropertyChangeListener)
3953 * @see #removePropertyChangeListener(String, PropertyChangeListener)
3954 * @see #getPropertyChangeListeners()
3955 * @since 1.4
3957 public PropertyChangeListener[] getPropertyChangeListeners(String property)
3959 return changeSupport == null ? new PropertyChangeListener[0]
3960 : changeSupport.getPropertyChangeListeners(property);
3964 * Report a change in a bound property to any registered property listeners.
3966 * @param propertyName the property that changed
3967 * @param oldValue the old property value
3968 * @param newValue the new property value
3970 protected void firePropertyChange(String propertyName, Object oldValue,
3971 Object newValue)
3973 if (changeSupport != null)
3974 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3978 * Report a change in a bound property to any registered property listeners.
3980 * @param propertyName the property that changed
3981 * @param oldValue the old property value
3982 * @param newValue the new property value
3984 protected void firePropertyChange(String propertyName, boolean oldValue,
3985 boolean newValue)
3987 if (changeSupport != null)
3988 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
3992 * Report a change in a bound property to any registered property listeners.
3994 * @param propertyName the property that changed
3995 * @param oldValue the old property value
3996 * @param newValue the new property value
3998 protected void firePropertyChange(String propertyName, int oldValue,
3999 int newValue)
4001 if (changeSupport != null)
4002 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
4006 * Sets the text layout orientation of this component. New components default
4007 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects only
4008 * the current component, while
4009 * {@link #applyComponentOrientation(ComponentOrientation)} affects the
4010 * entire hierarchy.
4012 * @param o the new orientation
4013 * @throws NullPointerException if o is null
4014 * @see #getComponentOrientation()
4016 public void setComponentOrientation(ComponentOrientation o)
4018 if (o == null)
4019 throw new NullPointerException();
4020 orientation = o;
4024 * Determines the text layout orientation used by this component.
4026 * @return the component orientation
4027 * @see #setComponentOrientation(ComponentOrientation)
4029 public ComponentOrientation getComponentOrientation()
4031 return orientation;
4035 * Sets the text layout orientation of this component. New components default
4036 * to UNKNOWN (which behaves like LEFT_TO_RIGHT). This method affects the
4037 * entire hierarchy, while
4038 * {@link #setComponentOrientation(ComponentOrientation)} affects only the
4039 * current component.
4041 * @param o the new orientation
4042 * @throws NullPointerException if o is null
4043 * @see #getComponentOrientation()
4044 * @since 1.4
4046 public void applyComponentOrientation(ComponentOrientation o)
4048 setComponentOrientation(o);
4052 * Returns the accessibility framework context of this class. Component is
4053 * not accessible, so the default implementation returns null. Subclasses
4054 * must override this behavior, and return an appropriate subclass of
4055 * {@link AccessibleAWTComponent}.
4057 * @return the accessibility context
4059 public AccessibleContext getAccessibleContext()
4061 return null;
4065 // Helper methods; some are package visible for use by subclasses.
4068 * Subclasses should override this to return unique component names like
4069 * "menuitem0".
4071 * @return the generated name for this component
4073 String generateName()
4075 // Component is abstract.
4076 return null;
4080 * Sets the peer for this component.
4082 * @param peer the new peer
4084 final void setPeer(ComponentPeer peer)
4086 this.peer = peer;
4090 * Implementation method that allows classes such as Canvas and Window to
4091 * override the graphics configuration without violating the published API.
4093 * @return the graphics configuration
4095 GraphicsConfiguration getGraphicsConfigurationImpl()
4097 if (peer != null)
4099 GraphicsConfiguration config = peer.getGraphicsConfiguration();
4100 if (config != null)
4101 return config;
4104 if (parent != null)
4105 return parent.getGraphicsConfiguration();
4107 return null;
4111 * Implementation of dispatchEvent. Allows trusted package classes to
4112 * dispatch additional events first.
4114 * @param e the event to dispatch
4116 void dispatchEventImpl(AWTEvent e)
4118 if (eventTypeEnabled (e.id))
4119 processEvent(e);
4123 * Tells whether or not an event type is enabled.
4125 boolean eventTypeEnabled (int type)
4127 if (type > AWTEvent.RESERVED_ID_MAX)
4128 return true;
4130 switch (type)
4132 case ComponentEvent.COMPONENT_HIDDEN:
4133 case ComponentEvent.COMPONENT_MOVED:
4134 case ComponentEvent.COMPONENT_RESIZED:
4135 case ComponentEvent.COMPONENT_SHOWN:
4136 return (componentListener != null
4137 || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0);
4139 case KeyEvent.KEY_PRESSED:
4140 case KeyEvent.KEY_RELEASED:
4141 case KeyEvent.KEY_TYPED:
4142 return (keyListener != null
4143 || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0);
4145 case MouseEvent.MOUSE_CLICKED:
4146 case MouseEvent.MOUSE_ENTERED:
4147 case MouseEvent.MOUSE_EXITED:
4148 case MouseEvent.MOUSE_PRESSED:
4149 case MouseEvent.MOUSE_RELEASED:
4150 return (mouseListener != null
4151 || mouseMotionListener != null
4152 || (eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0);
4154 case FocusEvent.FOCUS_GAINED:
4155 case FocusEvent.FOCUS_LOST:
4156 return (focusListener != null
4157 || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0);
4159 case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
4160 case InputMethodEvent.CARET_POSITION_CHANGED:
4161 return (inputMethodListener != null
4162 || (eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0);
4164 case PaintEvent.PAINT:
4165 case PaintEvent.UPDATE:
4166 return (eventMask & AWTEvent.PAINT_EVENT_MASK) != 0;
4168 default:
4169 return false;
4174 * Coalesce paint events. Current heuristic is: Merge if the union of
4175 * areas is less than twice that of the sum of the areas. The X server
4176 * tend to create a lot of paint events that are adjacent but not
4177 * overlapping.
4179 * <pre>
4180 * +------+
4181 * | +-----+ ...will be merged
4182 * | | |
4183 * | | |
4184 * +------+ |
4185 * +-----+
4187 * +---------------+--+
4188 * | | | ...will not be merged
4189 * +---------------+ |
4190 * | |
4191 * | |
4192 * | |
4193 * | |
4194 * | |
4195 * +--+
4196 * </pre>
4198 * @param queuedEvent the first paint event
4199 * @param newEvent the second paint event
4200 * @return the combined paint event, or null
4202 private PaintEvent coalescePaintEvents(PaintEvent queuedEvent,
4203 PaintEvent newEvent)
4205 Rectangle r1 = queuedEvent.getUpdateRect();
4206 Rectangle r2 = newEvent.getUpdateRect();
4207 Rectangle union = r1.union(r2);
4209 int r1a = r1.width * r1.height;
4210 int r2a = r2.width * r2.height;
4211 int ua = union.width * union.height;
4213 if (ua > (r1a+r2a)*2)
4214 return null;
4215 /* The 2 factor should maybe be reconsidered. Perhaps 3/2
4216 would be better? */
4218 newEvent.setUpdateRect(union);
4219 return newEvent;
4223 * Does the work for a paint event.
4225 * @param event the event to process
4227 private void processPaintEvent(PaintEvent event)
4229 // Can't do graphics without peer
4230 if (peer == null)
4231 return;
4233 Graphics gfx = getGraphics();
4236 Shape clip = event.getUpdateRect();
4237 gfx.setClip(clip);
4239 switch (event.id)
4241 case PaintEvent.PAINT:
4242 paint(gfx);
4243 break;
4244 case PaintEvent.UPDATE:
4245 update(gfx);
4246 break;
4247 default:
4248 throw new IllegalArgumentException("unknown paint event");
4251 finally
4253 gfx.dispose();
4258 * This method is used to implement transferFocus(). CHILD is the child
4259 * making the request. This is overridden by Container; when called for an
4260 * ordinary component there is no child and so we always return null.
4262 * @param child the component making the request
4263 * @return the next component to focus on
4265 Component findNextFocusComponent(Component child)
4267 return null;
4271 * Deserializes this component. This regenerates all serializable listeners
4272 * which were registered originally.
4274 * @param s the stream to read from
4275 * @throws ClassNotFoundException if deserialization fails
4276 * @throws IOException if the stream fails
4278 private void readObject(ObjectInputStream s)
4279 throws ClassNotFoundException, IOException
4281 s.defaultReadObject();
4282 String key = (String) s.readObject();
4283 while (key != null)
4285 Object listener = s.readObject();
4286 if ("componentL".equals(key))
4287 addComponentListener((ComponentListener) listener);
4288 else if ("focusL".equals(key))
4289 addFocusListener((FocusListener) listener);
4290 else if ("keyL".equals(key))
4291 addKeyListener((KeyListener) listener);
4292 else if ("mouseL".equals(key))
4293 addMouseListener((MouseListener) listener);
4294 else if ("mouseMotionL".equals(key))
4295 addMouseMotionListener((MouseMotionListener) listener);
4296 else if ("inputMethodL".equals(key))
4297 addInputMethodListener((InputMethodListener) listener);
4298 else if ("hierarchyL".equals(key))
4299 addHierarchyListener((HierarchyListener) listener);
4300 else if ("hierarchyBoundsL".equals(key))
4301 addHierarchyBoundsListener((HierarchyBoundsListener) listener);
4302 else if ("mouseWheelL".equals(key))
4303 addMouseWheelListener((MouseWheelListener) listener);
4304 key = (String) s.readObject();
4309 * Serializes this component. This ignores all listeners which do not
4310 * implement Serializable, but includes those that do.
4312 * @param s the stream to write to
4313 * @throws IOException if the stream fails
4315 private void writeObject(ObjectOutputStream s) throws IOException
4317 s.defaultWriteObject();
4318 AWTEventMulticaster.save(s, "componentL", componentListener);
4319 AWTEventMulticaster.save(s, "focusL", focusListener);
4320 AWTEventMulticaster.save(s, "keyL", keyListener);
4321 AWTEventMulticaster.save(s, "mouseL", mouseListener);
4322 AWTEventMulticaster.save(s, "mouseMotionL", mouseMotionListener);
4323 AWTEventMulticaster.save(s, "inputMethodL", inputMethodListener);
4324 AWTEventMulticaster.save(s, "hierarchyL", hierarchyListener);
4325 AWTEventMulticaster.save(s, "hierarchyBoundsL", hierarchyBoundsListener);
4326 AWTEventMulticaster.save(s, "mouseWheelL", mouseWheelListener);
4327 s.writeObject(null);
4331 // Nested classes.
4334 * This class provides accessibility support for subclasses of container.
4336 * @author Eric Blake <ebb9@email.byu.edu>
4337 * @since 1.3
4338 * @status updated to 1.4
4340 protected abstract class AccessibleAWTComponent extends AccessibleContext
4341 implements Serializable, AccessibleComponent
4344 * Compatible with JDK 1.3+.
4346 private static final long serialVersionUID = 642321655757800191L;
4349 * Converts show/hide events to PropertyChange events, and is registered
4350 * as a component listener on this component.
4352 * @serial the component handler
4354 protected ComponentListener accessibleAWTComponentHandler
4355 = new AccessibleAWTComponentHandler();
4358 * Converts focus events to PropertyChange events, and is registered
4359 * as a focus listener on this component.
4361 * @serial the focus handler
4363 protected FocusListener accessibleAWTFocusHandler
4364 = new AccessibleAWTFocusHandler();
4367 * The default constructor.
4369 protected AccessibleAWTComponent()
4371 Component.this.addComponentListener(accessibleAWTComponentHandler);
4372 Component.this.addFocusListener(accessibleAWTFocusHandler);
4376 * Adds a global property change listener to the accessible component.
4378 * @param l the listener to add
4379 * @see #ACCESSIBLE_NAME_PROPERTY
4380 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
4381 * @see #ACCESSIBLE_STATE_PROPERTY
4382 * @see #ACCESSIBLE_VALUE_PROPERTY
4383 * @see #ACCESSIBLE_SELECTION_PROPERTY
4384 * @see #ACCESSIBLE_TEXT_PROPERTY
4385 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
4387 public void addPropertyChangeListener(PropertyChangeListener l)
4389 Component.this.addPropertyChangeListener(l);
4390 super.addPropertyChangeListener(l);
4394 * Removes a global property change listener from this accessible
4395 * component.
4397 * @param l the listener to remove
4399 public void removePropertyChangeListener(PropertyChangeListener l)
4401 Component.this.removePropertyChangeListener(l);
4402 super.removePropertyChangeListener(l);
4406 * Returns the accessible name of this component. It is almost always
4407 * wrong to return getName(), since it is not localized. In fact, for
4408 * things like buttons, this should be the text of the button, not the
4409 * name of the object. The tooltip text might also be appropriate.
4411 * @return the name
4412 * @see #setAccessibleName(String)
4414 public String getAccessibleName()
4416 return accessibleName == null ? getName() : accessibleName;
4420 * Returns a brief description of this accessible context. This should
4421 * be localized.
4423 * @return a description of this component
4424 * @see #setAccessibleDescription(String)
4426 public String getAccessibleDescription()
4428 return accessibleDescription;
4432 * Returns the role of this component.
4434 * @return the accessible role
4436 public AccessibleRole getAccessibleRole()
4438 return AccessibleRole.AWT_COMPONENT;
4442 * Returns a state set describing this component's state.
4444 * @return a new state set
4445 * @see AccessibleState
4447 public AccessibleStateSet getAccessibleStateSet()
4449 AccessibleStateSet s = new AccessibleStateSet();
4450 if (Component.this.isEnabled())
4451 s.add(AccessibleState.ENABLED);
4452 if (isFocusable())
4453 s.add(AccessibleState.FOCUSABLE);
4454 if (isFocusOwner())
4455 s.add(AccessibleState.FOCUSED);
4456 if (isOpaque())
4457 s.add(AccessibleState.OPAQUE);
4458 if (Component.this.isShowing())
4459 s.add(AccessibleState.SHOWING);
4460 if (Component.this.isVisible())
4461 s.add(AccessibleState.VISIBLE);
4462 return s;
4466 * Returns the parent of this component, if it is accessible.
4468 * @return the accessible parent
4470 public Accessible getAccessibleParent()
4472 if (accessibleParent == null)
4474 Container parent = getParent();
4475 accessibleParent = parent instanceof Accessible
4476 ? (Accessible) parent : null;
4478 return accessibleParent;
4482 * Returns the index of this component in its accessible parent.
4484 * @return the index, or -1 if the parent is not accessible
4485 * @see #getAccessibleParent()
4487 public int getAccessibleIndexInParent()
4489 if (getAccessibleParent() == null)
4490 return -1;
4491 AccessibleContext context
4492 = ((Component) accessibleParent).getAccessibleContext();
4493 if (context == null)
4494 return -1;
4495 for (int i = context.getAccessibleChildrenCount(); --i >= 0; )
4496 if (context.getAccessibleChild(i) == Component.this)
4497 return i;
4498 return -1;
4502 * Returns the number of children of this component which implement
4503 * Accessible. Subclasses must override this if they can have children.
4505 * @return the number of accessible children, default 0
4507 public int getAccessibleChildrenCount()
4509 return 0;
4513 * Returns the ith accessible child. Subclasses must override this if
4514 * they can have children.
4516 * @return the ith accessible child, or null
4517 * @see #getAccessibleChildrenCount()
4519 public Accessible getAccessibleChild(int i)
4521 return null;
4525 * Returns the locale of this component.
4527 * @return the locale
4528 * @throws IllegalComponentStateException if the locale is unknown
4530 public Locale getLocale()
4532 return Component.this.getLocale();
4536 * Returns this, since it is an accessible component.
4538 * @return the accessible component
4540 public AccessibleComponent getAccessibleComponent()
4542 return this;
4546 * Gets the background color.
4548 * @return the background color
4549 * @see #setBackground(Color)
4551 public Color getBackground()
4553 return Component.this.getBackground();
4557 * Sets the background color.
4559 * @param c the background color
4560 * @see #getBackground()
4561 * @see #isOpaque()
4563 public void setBackground(Color c)
4565 Component.this.setBackground(c);
4569 * Gets the foreground color.
4571 * @return the foreground color
4572 * @see #setForeground(Color)
4574 public Color getForeground()
4576 return Component.this.getForeground();
4580 * Sets the foreground color.
4582 * @param c the foreground color
4583 * @see #getForeground()
4585 public void setForeground(Color c)
4587 Component.this.setForeground(c);
4591 * Gets the cursor.
4593 * @return the cursor
4594 * @see #setCursor(Cursor)
4596 public Cursor getCursor()
4598 return Component.this.getCursor();
4602 * Sets the cursor.
4604 * @param cursor the cursor
4605 * @see #getCursor()
4607 public void setCursor(Cursor cursor)
4609 Component.this.setCursor(cursor);
4613 * Gets the font.
4615 * @return the font
4616 * @see #setFont(Font)
4618 public Font getFont()
4620 return Component.this.getFont();
4624 * Sets the font.
4626 * @param f the font
4627 * @see #getFont()
4629 public void setFont(Font f)
4631 Component.this.setFont(f);
4635 * Gets the font metrics for a font.
4637 * @param f the font to look up
4638 * @return its metrics
4639 * @throws NullPointerException if f is null
4640 * @see #getFont()
4642 public FontMetrics getFontMetrics(Font f)
4644 return Component.this.getFontMetrics(f);
4648 * Tests if the component is enabled.
4650 * @return true if the component is enabled
4651 * @see #setEnabled(boolean)
4652 * @see #getAccessibleStateSet()
4653 * @see AccessibleState#ENABLED
4655 public boolean isEnabled()
4657 return Component.this.isEnabled();
4661 * Set whether the component is enabled.
4663 * @param b the new enabled status
4664 * @see #isEnabled()
4666 public void setEnabled(boolean b)
4668 Component.this.setEnabled(b);
4672 * Test whether the component is visible (not necesarily showing).
4674 * @return true if it is visible
4675 * @see #setVisible(boolean)
4676 * @see #getAccessibleStateSet()
4677 * @see AccessibleState#VISIBLE
4679 public boolean isVisible()
4681 return Component.this.isVisible();
4685 * Sets the visibility of this component.
4687 * @param b the desired visibility
4688 * @see #isVisible()
4690 public void setVisible(boolean b)
4692 Component.this.setVisible(b);
4696 * Tests if the component is showing.
4698 * @return true if this is showing
4700 public boolean isShowing()
4702 return Component.this.isShowing();
4706 * Tests if the point is contained in this component.
4708 * @param p the point to check
4709 * @return true if it is contained
4710 * @throws NullPointerException if p is null
4712 public boolean contains(Point p)
4714 return Component.this.contains(p.x, p.y);
4718 * Returns the location of this object on the screen, or null if it is
4719 * not showing.
4721 * @return the location relative to screen coordinates, if showing
4722 * @see #getBounds()
4723 * @see #getLocation()
4725 public Point getLocationOnScreen()
4727 return Component.this.isShowing() ? Component.this.getLocationOnScreen()
4728 : null;
4732 * Returns the location of this object relative to its parent's coordinate
4733 * system, or null if it is not showing.
4735 * @return the location
4736 * @see #getBounds()
4737 * @see #getLocationOnScreen()
4739 public Point getLocation()
4741 return Component.this.isShowing() ? Component.this.getLocation() : null;
4745 * Sets the location of this relative to its parent's coordinate system.
4747 * @param p the location
4748 * @throws NullPointerException if p is null
4749 * @see #getLocation()
4751 public void setLocation(Point p)
4753 Component.this.setLocation(p.x, p.y);
4757 * Gets the bounds of this component, or null if it is not on screen.
4759 * @return the bounds
4760 * @see #contains(Point)
4761 * @see #setBounds(Rectangle)
4763 public Rectangle getBounds()
4765 return Component.this.isShowing() ? Component.this.getBounds() : null;
4769 * Sets the bounds of this component.
4771 * @param r the bounds
4772 * @throws NullPointerException if r is null
4773 * @see #getBounds()
4775 public void setBounds(Rectangle r)
4777 Component.this.setBounds(r.x, r.y, r.width, r.height);
4781 * Gets the size of this component, or null if it is not showing.
4783 * @return the size
4784 * @see #setSize(Dimension)
4786 public Dimension getSize()
4788 return Component.this.isShowing() ? Component.this.getSize() : null;
4792 * Sets the size of this component.
4794 * @param d the size
4795 * @throws NullPointerException if d is null
4796 * @see #getSize()
4798 public void setSize(Dimension d)
4800 Component.this.setSize(d.width, d.height);
4804 * Returns the Accessible child at a point relative to the coordinate
4805 * system of this component, if one exists, or null. Since components
4806 * have no children, subclasses must override this to get anything besides
4807 * null.
4809 * @param p the point to check
4810 * @return the accessible child at that point
4811 * @throws NullPointerException if p is null
4813 public Accessible getAccessibleAt(Point p)
4815 return null;
4819 * Tests whether this component can accept focus.
4821 * @return true if this is focus traversable
4822 * @see #getAccessibleStateSet()
4823 * @see AccessibleState#FOCUSABLE
4824 * @see AccessibleState#FOCUSED
4826 public boolean isFocusTraversable()
4828 return Component.this.isFocusTraversable();
4832 * Requests focus for this component.
4834 * @see #isFocusTraversable()
4836 public void requestFocus()
4838 Component.this.requestFocus();
4842 * Adds a focus listener.
4844 * @param l the listener to add
4846 public void addFocusListener(FocusListener l)
4848 Component.this.addFocusListener(l);
4852 * Removes a focus listener.
4854 * @param l the listener to remove
4856 public void removeFocusListener(FocusListener l)
4858 Component.this.removeFocusListener(l);
4862 * Converts component changes into property changes.
4864 * @author Eric Blake <ebb9@email.byu.edu>
4865 * @since 1.3
4866 * @status updated to 1.4
4868 protected class AccessibleAWTComponentHandler implements ComponentListener
4871 * Default constructor.
4873 protected AccessibleAWTComponentHandler()
4878 * Convert a component hidden to a property change.
4880 * @param e the event to convert
4882 public void componentHidden(ComponentEvent e)
4884 AccessibleAWTComponent.this.firePropertyChange
4885 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.VISIBLE, null);
4889 * Convert a component shown to a property change.
4891 * @param e the event to convert
4893 public void componentShown(ComponentEvent e)
4895 AccessibleAWTComponent.this.firePropertyChange
4896 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.VISIBLE);
4900 * Moving a component does not affect properties.
4902 * @param e ignored
4904 public void componentMoved(ComponentEvent e)
4909 * Resizing a component does not affect properties.
4911 * @param e ignored
4913 public void componentResized(ComponentEvent e)
4916 } // class AccessibleAWTComponentHandler
4919 * Converts focus changes into property changes.
4921 * @author Eric Blake <ebb9@email.byu.edu>
4922 * @since 1.3
4923 * @status updated to 1.4
4925 protected class AccessibleAWTFocusHandler implements FocusListener
4928 * Default constructor.
4930 protected AccessibleAWTFocusHandler()
4935 * Convert a focus gained to a property change.
4937 * @param e the event to convert
4939 public void focusGained(FocusEvent e)
4941 AccessibleAWTComponent.this.firePropertyChange
4942 (ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED);
4946 * Convert a focus lost to a property change.
4948 * @param e the event to convert
4950 public void focusLost(FocusEvent e)
4952 AccessibleAWTComponent.this.firePropertyChange
4953 (ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null);
4955 } // class AccessibleAWTComponentHandler
4956 } // class AccessibleAWTComponent
4959 * This class provides support for blitting offscreen surfaces.
4961 * @author Eric Blake <ebb9@email.byu.edu>
4962 * @since 1.4
4963 * @XXX Shell class, to allow compilation. This needs documentation and
4964 * correct implementation.
4966 protected class BltBufferStrategy extends BufferStrategy
4968 protected BufferCapabilities caps;
4969 protected VolatileImage[] backBuffers;
4970 protected boolean validatedContents;
4971 protected int width;
4972 protected int height;
4973 protected BltBufferStrategy(int num, BufferCapabilities caps)
4975 this.caps = caps;
4976 createBackBuffers(num);
4978 protected void createBackBuffers(int num)
4980 backBuffers = new VolatileImage[num];
4982 public BufferCapabilities getCapabilities()
4984 return caps;
4986 public Graphics getDrawGraphics() { return null; }
4987 public void show() {}
4988 protected void revalidate() {}
4989 public boolean contentsLost() { return false; }
4990 public boolean contentsRestored() { return false; }
4991 } // class BltBufferStrategy
4994 * This class provides support for flipping component buffers. It is only
4995 * designed for use by Canvas and Window.
4997 * @author Eric Blake <ebb9@email.byu.edu>
4998 * @since 1.4
4999 * @XXX Shell class, to allow compilation. This needs documentation and
5000 * correct implementation.
5002 protected class FlipBufferStrategy extends BufferStrategy
5004 protected int numBuffers;
5005 protected BufferCapabilities caps;
5006 protected Image drawBuffer;
5007 protected VolatileImage drawVBuffer;
5008 protected boolean validatedContents;
5009 protected FlipBufferStrategy(int num, BufferCapabilities caps)
5010 throws AWTException
5012 this.caps = caps;
5013 createBuffers(num, caps);
5015 protected void createBuffers(int num, BufferCapabilities caps)
5016 throws AWTException {}
5017 protected Image getBackBuffer()
5019 return drawBuffer;
5021 protected void flip(BufferCapabilities.FlipContents flipAction) {}
5022 protected void destroyBuffers() {}
5023 public BufferCapabilities getCapabilities()
5025 return caps;
5027 public Graphics getDrawGraphics() { return null; }
5028 protected void revalidate() {}
5029 public boolean contentsLost() { return false; }
5030 public boolean contentsRestored() { return false; }
5031 public void show() {}
5032 } // class FlipBufferStrategy
5033 } // class Component