Components
Contents:
Component
Labels
Buttons
Simple Calculator
Canvas
Creating Your Own Component
Cursor
This chapter introduces the generic graphical widget used within the AWT package, Component
, along with a trio of specific components: Label
, Button
, and Canvas
. It also covers the Cursor
class, new to Java 1.1. (Cursor support was previously part of the Frame
class.) Although many objects within AWT don't subclass Component
, and though you will never create an instance of Component
, anything that provides screen-based user interaction and relies on the system for its layout will be a child of Component
. As a subclass of Component
, each child inherits a common set of methods and an API for dealing with the different events (i.e., mouse click, keyboard input) that occur within your Java programs.
After discussing the methods in Component
classes, this chapter goes into detail about two specific components, Label
and Button
. A Label
is a widget that contains descriptive text, usually next to an input field. A Button
is a basic mechanism that lets the user signal the desire to perform an action. You will learn about the Canvas
object and how to use a Canvas
to create your own component. Finally, we cover the Cursor
class, which lets you change the cursor over a Component
.
Before going into the mechanics of the Component
class, it's necessary to say a little about the relationship between components and containers. A Container
is also a component with the ability to contain other components. There are several different kinds of containers; they are discussed in Containers. To display a component, you have to put it in a container by calling the container's add()
method. We often call the container that holds a component the component's parent ; likewise, we call the components a container holds its children. Certain operations are legal only if a component has a parent--that is, the component is in a container. Of course, since containers are components, containers can contain other containers, ad infinitum.
NOTE:
If you think some component is missing a method that should obviously be there, check the methods it inherits. For example, the Label
class appears to lack a setFont()
method. Obviously, labels ought to be able to change their fonts. The setFont()
method really is there; it is inherited from the Component
class, and therefore, not documented as part of the Label
class. Even if you're familiar with object-oriented techniques, the need to work up a class hierarchy to find all of the class's methods can lead to confusion and frustration. While all Java objects inherit methods from other classes, the potential for confusion is worst with components, which inherit over a hundred methods from Component
and may only have a few methods of their own.
Component
Every GUI-based program consists of a screen with a set of objects. With Java, these objects are called components. Some of the more frequently used components are buttons, text fields, and containers.
A container is a special component that allows you to group different components together within it. You will learn more about containers in the next chapter, but they are in fact just another kind of component. Also, some of the parameters and return types for the methods of Component
have not been explained yet and have their own sections in future chapters.
Component Methods
ConstantsPrior to Java 1.1, you could not subclass Component
or Container
. With the introduction of the LightweightPeer
, you can now subclass either Component
or Container
. However, since you no longer have a native peer, you must rely on your container to provide a display area and other services that are normally provided by a full-fledged peer. Because you cannot rely on your peer to determine your alignment, the Component
class now has five constants to indicate six possible alignment settings (one constant is used twice). The alignment constants designate where to position a lightweight component; their values range from 0.0 to 1.0. The lower the number, the closer the component will be placed to the origin (top left corner) of the space allotted to it.[1]
[1] As of Beta 3, these constants appear to be seldom used. The
getAlignmentX()
andgetAlignmentY()
methods return these values, but there are nosetAlignment
methods.
- public static final float BOTTOM_ALIGNMENT
- The
BOTTOM_ALIGNMENT
constant indicates that the component should align itself to the bottom of its available space. It is a return value from the methodgetAlignmentY()
. - public static final float CENTER_ALIGNMENT
- The
CENTER_ALIGNMENT
constant indicates that the component should align itself to the middle of its available space. It is a return value from either thegetAlignmentX()
orgetAlignmentY()
method. This constant represents both the horizontal and vertical center. - public static final float LEFT_ALIGNMENT
- The
LEFT_ALIGNMENT
constant indicates that the component should align itself to the left side of its available space. It is a return value fromgetAlignmentX()
. - public static final float RIGHT_ALIGNMENT
- The
RIGHT_ALIGNMENT
constant indicates that the component should align itself to the right side of its available space. It is a return value from the methodgetAlignmentX()
. - public static final float TOP_ALIGNMENT
- The
TOP_ALIGNMENT
constant indicates that the component should align itself to the top of its available space. It is a return value fromgetAlignmentY()
.
- protected Locale locale
- The protected
locale
variable can be accessed by calling thegetLocale()
method.
Prior to Java 1.1, there was no public or protected constructor for Component
. Only package members were able to subclass Component
directly. With the introduction of lightweight peers, components can exist without a native peer, so the constructor was made protected
, allowing you to create your own Component
subclasses.
- protected Component()
- The constructor for
Component
creates a new component without a native peer. Since you no longer have a native peer, you must rely on your container to provide a display area. This allows you to create components that require fewer system resources than components that subclassCanvas
. The example in the "Using an event multicaster" section of the previous chapter is of a lightweight component. Use theSystemColor
class to help you colorize the new component appropriately or make it transparent.
- public Toolkit getToolkit ()
- The
getToolkit()
method returns the currentToolkit
of theComponent
. This returns the parent'sToolkit
(from agetParent()
call) when theComponent
has not been added to the screen yet or is lightweight. If there is no parent,getToolkit()
returns the defaultToolkit
. Through theToolkit
, you have access to the details of the current platform (like screen resolution, screen size, and available fonts), which you can use to adjust screen real estate requirements or check on the availability of a font. - public Color getForeground ()
- The
getForeground()
method returns the foreground color of the component. If no foreground color is set for the component, you get its parent's foreground color. If none of the component's parents have a foreground color set,null
is returned. - public void setForeground (Color c)
- The
setForeground()
method changes the current foreground color of the area of the screen occupied by the component toc
. After changing the color, it is necessary for the screen to refresh before the change has any effect. To refresh the screen, callrepaint()
. - public Color getBackground ()
- The
getBackground()
method returns the background color of the component. If no background color is set for the component, its parent's background color is retrieved. If none of the component's parents have a background color set,null
is returned. - public void setBackground (Color c)
- The
setBackground()
method changes the current background color of the area of the screen occupied by the component toc
. After changing the color, it is necessary for the screen to refresh before the change has any affect. To refresh the screen, callrepaint()
. - public Font getFont ()
- The
getFont()
method returns the font of the component. If no font is set for the component, its parent's font is retrieved. If none of the component's parents have a font set,null
is returned. - public synchronized void setFont (Font f)
- The
setFont()
method changes the component's font tof
. If the font family (such as TimesRoman) provided withinf
is not available on the current platform, the system uses a default font family, along with the supplied size and style (plain, bold, italic). Depending upon the platform, it may be necessary to refresh the component/screen before seeing any changes.Changing the font of a component could have an affect on the layout of the component.
- public synchronized ColorModel getColorModel ()
- The
getColorModel()
method returns theColorModel
used to display the current component. If the component is not displayed, theColorModel
from the component'sToolkit
is used. The normalColorModel
for a Java program is 8 bits each for red, green, and blue. - public Graphics getGraphics ()
- The
getGraphics()
method gets the component's graphics context. Most noncontainer components do not manage them correctly and therefore throw anInternalError
exception when you call this method. TheCanvas
component is one that does since you can draw on that directly. If the component is not visible,null
is returned. - public FontMetrics getFontMetrics (Font f)
- The
getFontMetrics()
method retrieves the component's view of theFontMetrics
for the requested fontf
. Through theFontMetrics
, you have access to the platform-specific sizing for the appearance of a character or string. - public Locale getLocale ()
- The
getLocale()
method retrieves the currentLocale
of the component, if it has one. Using aLocale
allows you to write programs that can adapt themselves to different languages and different regional variants. If noLocale
has been set,getLocale()
returns the parent'sLocale
.[2] If the component has no locale of its own and no parent (i.e., it isn't in a container),getLocale()
throws the run-time exceptionIllegalComponentStateException
.[2] For more on the
Locale
class, see the Java Fundamental Classes Reference from Anonymous. - public void setLocale (Locale l)
- The
setLocale()
method changes the currentLocale
of the component tol
. In order for this change to have any effect, you must localize your components so that they have different labels or list values for different environments. Localization is part of the broad topic of internationalization and is beyond the scope of this tutorial. - public Cursor getCursor ()
- The
getCursor()
method retrieves the component's currentCursor
. If one hasn't been set, the default isCursor.DEFAULT_CURSOR
. TheCursor
class is described fully in Cursor. Prior to Java 1.1, the ability to associate cursors with components was restricted to frames. - public synchronized void setCursor (Cursor c)
- The
setCursor()
method changes the currentCursor
of the component toc
. The change takes effect as soon as the cursor is moved. Lightweight components cannot change their cursors.
Component
provides a handful of methods for positioning and sizing objects. Most of these are used behind the scenes by the system. You will also need them if you create your own LayoutManager
or need to move or size an object. All of these depend on support for the functionality from the true component's peer.
- public Point getLocation ()
public Point location () - The
getLocation()
method returns the current position of theComponent
in its parent's coordinate space. ThePoint
is the top left corner of the bounding box around theComponent
.location()
is the Java 1.0 name for this method. - public Point getLocationOnScreen ()
- The
getLocationOnScreen()
method returns the current position of theComponent
in the screen's coordinate space. ThePoint
is the top left corner of the bounding box around theComponent
. If the component is not showing, thegetLocationOnScreen()
method throws theIllegalComponentStateException
run-time exception. - public void setLocation (int x, int y)
public void move (int x, int y) - The
setLocation()
method moves theComponent
to the new position (x
,y
). The coordinates provided are in the parent container's coordinate space. This method callssetBounds()
to move the component. TheLayoutManager
of the container may make it impossible to change a component's location.Calling this method with a new position for the component generates a
ComponentEvent
with the IDCOMPONENT_MOVED
.move()
is the Java 1.0 name for this method. - public void setLocation (Point p)
- This
setLocation()
method moves the component to the position specified by the givenPoint
. It is the same as callingsetLocation
(p.x
,p.y)
.Calling this method with a new position for the component generates a
ComponentEvent
with the IDCOMPONENT_MOVED
. - public Dimension getSize ()
public Dimension size () - The
getSize()
method returns the width and height of the component as aDimension
object.size()
is the Java 1.0 name for this method. - public void setSize (int width, int height)
public void resize (int width, int height) - The
setSize()
method changes the component's width and height to thewidth
andheight
provided.width
andheight
are specified in pixels. The component is resized by a call tosetBounds()
. TheLayoutManager
of theContainer
that contains the component may make it impossible to change a component's size.Calling this method with a new size for the component generates a
ComponentEvent
with the IDCOMPONENT_RESIZED
.resize()
is the Java 1.0 name for this method. - public void setSize (Dimension d)
public void resize (Dimension d) - This
setSize()
method changes the component's width and height to theDimension
d
provided. TheDimension
object includes the width and height attributes in one object. The component is resized by a call to thesetBounds()
method. TheLayoutManager
of theContainer
that contains the component may make it impossible to change a component's size.Calling this method with a new size for the component generates a
ComponentEvent
with the IDCOMPONENT_RESIZED
.resize()
is the Java 1.0 name for this method. - public Rectangle getBounds ()
public Rectangle bounds () - The
getBounds()
method returns the bounding rectangle of the object. The fields of theRectangle
that you get back contain the component's position and dimensions.bounds()
is the Java 1.0 name for this method. - public void setBounds (int x, int y, int width, int height)
public void reshape (int x, int y, int width, int height) - The
setBounds()
method moves and resizes the component to the bounding rectangle with coordinates of (x
,y
) (top left corner) andwidth
xheight
. If the size and shape have not changed, no reshaping is done. If the component is resized, it is invalidated, along with its parent container. TheLayoutManager
of theContainer
that contains the component may make it impossible to change the component's size or position. CallingsetBounds()
invalidates the container, which results in a call to theLayoutManager
to rearrange the container's contents. In turn, theLayoutManager
callssetBounds()
to give the component its new size and position, which will probably be the same size and position it had originally. In short, if a layout manager is in effect, it will probably undo your attempts to change the component's size and position.Calling this method with a new size for the component generates a
ComponentEvent
with the IDCOMPONENT_RESIZED
. Calling this method with a new position generates aComponentEvent
with the IDCOMPONENT_MOVED
.reshape()
is the Java 1.0 name for this method. - public void setBounds (Rectangle r)
- This
setBounds()
method calls the previous method with parameters ofr.x
,r.y
,r.width
, andr.height
.Calling this method with a new size for the component generates a
ComponentEvent
with the IDCOMPONENT_RESIZED
. Calling this method with a new position generates aComponentEvent
with the IDCOMPONENT_MOVED
. - public Dimension getPreferredSize ()
public Dimension preferredSize () - The
getPreferredSize()
method returns theDimension
(width and height) for the preferred size of the component. Each component's peer knows its preferred size. Lightweight objects returngetSize()
.preferredSize()
is the Java 1.0 name for this method. - public Dimension getMinimumSize ()
public Dimension minimumSize () - The
getMinimumSize()
method returns theDimension
(width and height) for the minimum size of the component. Each component's peer knows its minimum size. Lightweight objects returngetSize()
. It is possible that the methodsgetMinimumSize()
andgetPreferredSize()
will return the same dimensions.minimumSize()
is the Java 1.0 name for this method. - public Dimension getMaximumSize ()
- The
getMaximumSize()
method returns theDimension
(width and height) for the maximum size of the component. This may be used by a layout manager to prevent a component from growing beyond a predetermined size. None of thejava.awt
layout managers call this method. By default, the value returned isShort.MAX_VALUE
for both dimensions. - public float getAlignmentX ()
- The
getAlignmentX()
method returns the alignment of the component along the x axis. The alignment could be used by a layout manager to position this component relative to others. The return value is between 0.0 and 1.0. Values nearer 0 indicate that the component should be placed closer to the left edge of the area available. Values nearer 1 indicate that the component should be placed closer to the right. The value 0.5 means the component should be centered. The default setting isComponent.CENTER_ALIGNMENT
. - public float getAlignmentY ()
- The
getAlignmentY()
method returns the alignment of the component along the y axis. The alignment could be used by a layout manager to position this component relative to others. The return value is between 0.0 and 1.0. Values nearer 0 indicate that the component should be placed closer to the top of the area available. Values nearer 1 indicate that the component should be placed closer to the bottom. The value 0.5 means the component should be centered. The default setting isComponent.CENTER_ALIGNMENT
. - public void doLayout ()
public void layout () - The
doLayout()
method ofComponent
does absolutely nothing. It is called when theComponent
is validated (through thevalidate()
method). TheContainer
class overrides this method.layout()
is the Java 1.0 name for this method. - public boolean contains (int x, int y)
public boolean inside (int x, int y) - The
contains()
method checks if thex
andy
coordinates are within the bounding box of the component. If theComponent
is not rectangular, the method acts as if there is a rectangle around theComponent
.contains()
returnstrue
if thex
andy
coordinates are within the component,false
otherwise.inside()
is the Java 1.0 name for this method. - public boolean contains (Point p)
- This
contains()
method calls the previous method with parameters ofp.x
andp.y
. - public Component getComponentAt (int x, int y)
public Component locate (int x, int y) - The
getComponentAt()
method usescontains()
to see if thex
andy
coordinates are within the component. If they are, this method returns theComponent
. If they aren't, it returnsnull
.getComponentAt()
is overridden byContainer
to provide enhanced functionality.locate()
is the Java 1.0 name for this method. - public Component getComponentAt (Point p)
- This
getComponentAt()
method calls the previous method with parameters ofp.x
andp.y
.
The only methods in this section that you call directly are the versions of repaint()
. The paint()
and update()
methods are called by the system when the display area requires refreshing, such as when a user resizes a window. When your program changes the display you should call repaint()
to trigger a call to update()
and paint()
. Otherwise, the system is responsible for updating the display.
- public void paint (Graphics g)
- The
paint()
method is offered so the system can display whatever you want in aComponent
. In the baseComponent
class, this method does absolutely nothing. Ordinarily, it would be overridden in an applet to do something other than the default, which is display a box in the current background color.g
is the graphics context of the component being drawn on. - public void update (Graphics g)
- The
update()
method is automatically called when you ask to repaint theComponent
. If the component is not lightweight, the default implementation ofupdate()
clears graphics contextg
by drawing a filled rectangle in the background color, resetting the color to the current foreground color, and callingpaint()
. If you do not overrideupdate()
when you do animation, you will see some flickering becauseComponent
clears the screen. Animation is discussed in Simple Graphics. - public void paintAll (Graphics g)
- The
paintAll()
method validates the component and paints its peer if it is visible.g
represents the graphics context of the component. This method is called when thepaintComponents()
method ofContainer
is called. - public void repaint ()
- The
repaint()
method requests the scheduler to redraw the component as soon as possible. This will result inupdate()
getting called soon thereafter. There is not a one-to-one correlation betweenrepaint()
andupdate()
calls. It is possible that multiplerepaint()
calls can result in a singleupdate()
. - public void repaint (long tm)
- This version of
repaint()
allows for a delay oftm
milliseconds. It says, please update this component withintm
milliseconds, which may happen immediately. - public void repaint (int x, int y, int width, int height)
- This version of
repaint()
allows you to select the region of theComponent
you desire to be updated. (x
,y
) are the coordinates of the upper left corner of the bounding box of the component with dimensions ofwidth
xheight
. This is similar to creating a clipping area and results in a quicker repaint. - public void repaint (long tm, int x, int y, int width, int height)
- This final version of
repaint()
is what the other threerepaint()
methods call.tm
is the maximum delay in milliseconds before update should be called. (x
,y
) are the coordinates of the upper left corner of the clipping area of the component with dimensions ofwidth
xheight
. - public void print (Graphics g)
- The default implementation of the
print()
method callspaint()
.In Java 1.0, there was no way to print; in Java 1.1, if the
graphics
parameter implementsPrintGraphics
, anything drawn ong
will be printed. Printing is covered in Printing. - public void printAll (Graphics g)
- The
printAll()
method validates the component and paints its peer if it is visible.g
represents the graphics context of the component. This method is called when theprintComponents()
method ofContainer
is called or when you call it with aPrintGraphics
parameter.The default implementation of
printAll()
is identical topaintAll()
. As withpaintAll()
,g
represents the graphics context of the component; ifg
implementsPrintGraphics
, it can be printed.
Background information about using images is discussed in Simple Graphics and Image Processing. The imageUpdate()
method of Component
is the sole method of the ImageObserver
interface. Since images are loaded in a separate thread, this method is called whenever additional information about the image becomes available.
- public boolean imageUpdate (Image image, int infoflags, int x, int y, int width, int height)
imageUpdate()
is thejava.awt.image.ImageObserver
method implemented byComponent
. It is an asynchronous update interface for receiving notifications aboutImage
information asimage
is loaded and is automatically called when additional information becomes available. This method is necessary because image loading is done in a separate thread from thegetImage()
call. Ordinarily,x
andy
would be the coordinates of the upper left corner of the image loaded so far, usually (0, 0). However, the methodimageUpdate()
of the component ignores these parameters.width
andheight
are theimage
's dimensions, so far, in the loading process.The
infoflags
parameter is a bit-mask of information available to you aboutimage
. Please see the text aboutImageObserver
in Image Processing for a complete description of the different flags that can be set. When overriding this method, you can wait for some condition to be true by checking a flag in your program and then taking the desired action. To check for a particular flag, perform an AND (&
) ofinfoflags
and the constant. For example, to check if theFRAMEBITS
flag is set:if ((infoflags & ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS) System.out.println ("The Flag is set");
The return value from a call to
imageUpdate()
istrue
ifimage
has changed andfalse
otherwise.Two system properties let the user control the behavior of updates:
awt.image.incrementaldraw
allows the user to control whether or not partial images are displayed. Initially, the value ofincrementaldraw
is unset and defaults totrue
, which means that partial images are drawn. Ifincrementaldraw
is set tofalse
, the image will be drawn only when it is complete or when the screen is resized or refreshed.awt.image.redrawrate
allows the user to change the delay between successive repaints. If not set, the default redraw rate is 100 milliseconds.
- public Image createImage (int width, int height)
- The
createImage()
method creates an emptyImage
of sizewidth
xheight
. The returnedImage
is an in-memory image that can be drawn on for double buffering to manipulate an image in the background. If an image of sizewidth
xheight
cannot be created, the call returnsnull
. In order forcreateImage()
to succeed, the peer of theComponent
must exist; if the component is lightweight, the peer of the component's container must exist. - public Image createImage (ImageProducer producer)
- This
createImage()
method allows you to take an existing image and modify it in some way to produce a newImage
. This can be done throughImageFilter
andFilteredImageSource
or aMemoryImageSource
, which accepts an array of pixel information. You can learn more about these classes and this method in Image Processing. - public boolean prepareImage (Image image, ImageObserver observer)
- The
prepareImage()
method forcesimage
to start loading, asynchronously, in another thread.observer
is theComponent
thatimage
will be rendered on and is notified (viaimageUpdate()
) asimage
is being loaded. In the case of anApplet
,this
would be passed as theImageObserver
. Ifimage
has already been fully loaded,prepareImage()
returnstrue
. Otherwise,false
is returned. Sinceimage
is loaded asynchronously,prepareImage()
returns immediately. Ordinarily,prepareImage()
would be called by the system whenimage
is first needed to be displayed (indrawImage()
withinpaint()
). As more information about the image gets loaded,imageUpdate()
is called periodically.If you do not want to go through the trouble of creating a
MediaTracker
instance to start the loading of the image objects, you can callprepareImage()
to trigger the start of image loading prior to a call todrawImage()
.If
image
has already started loading when this is called or if this is an in-memory image, there is no effect. - public boolean prepareImage (Image image, int width, int height, ImageObserver observer)
- This version of
prepareImage()
is identical to the previous one, with the addition of a scaling factor ofwidth
xheight
. As with otherwidth
andheight
parameters, the units for these parameters are pixels. Also, ifwidth
andheight
are -1, no scaling factor is assumed. This method is called by one of the internalMediaTracker
methods. - public int checkImage (Image image, ImageObserver observer)
- The
checkImage()
method returns the status of the construction of a screen representation ofimage
, being watched byobserver
. Ifimage
has not started loading yet, this will not start it. The return value is theImageObserver
flags ORed together for the data that is now available. The availableImageObserver
flags are:WIDTH
,HEIGHT
,PROPERTIES
,SOMEBITS
,FRAMEBITS
,ALLBITS
,ERROR
, andABORT
. See Image Processing for a complete description ofImageObserver
. - public int checkImage (Image image, int width, int height, ImageObserver observer)
- This version of
checkImage()
is identical to the previous one, with the addition of a scaling factor ofwidth
xheight
. If you are using thedrawImage()
version withwidth
andheight
parameters, you should use this version ofcheckImage()
with the samewidth
andheight
.
- public ComponentPeer getPeer ()
- The
getPeer()
method returns a reference to the component's peer as aComponentPeer
object. For example, if you issue this method from aButton
object,getPeer()
returns an instance of theComponentPeer
subclassButtonPeer
.This method is flagged as deprecated in comments but not with
@deprecated
. There is no replacement method for Java 1.1. - public void addNotify ()
- The
addNotify()
method is overridden by each individual component type. WhenaddNotify()
is called, the peer of the component gets created, and theComponent
is invalidated. TheaddNotify()
method is called by the system when it needs to create the peer. The peer needs to be created when aComponent
is first shown, or when a newComponent
is added to aContainer
and theContainer
is already being shown (in which case it already has a peer, but a new one must be created to take account of the newComponent
). If you override this method for a specificComponent
, callsuper.addNotify()
first, then do what you need for theComponent
. You will then have information available about the newly created peer.Certain tasks cannot succeed unless the peer has been created. An incomplete list includes finding the size of a component, laying out a container (because it needs the component's size), and creating an
Image
object. Peers are discussed in more depth in Toolkit and Peers. - public synchronized void removeNotify ()
- The
removeNotify()
method destroys the peer of the component and removes it from the screen. The state information about theComponent
is retained by the specific subtype. TheremoveNotify()
method is called by the system when it determines the peer is no longer needed. Such times would be when theComponent
is removed from aContainer
, when its container changes, or when theComponent
is disposed. If you override this method for a specificComponent
, issue the particular commands for you need for thisComponent
, then callsuper.removeNotify()
last.
These methods determine whether the component is ready to be displayed and can be seen by the user. The first requirement is that it be valid--that is, whether the system knows its size, and (in the case of a container) whether the layout manager is aware of all its parts and has placed them as requested. A component becomes invalid if the size has changed since it was last displayed. If the component is a container, it becomes invalid when one of the components contained within it becomes invalid.
Next, the component must be visible--a possibly confusing term, because components can be considered "visible" without being seen by the user. Frames (because they have their own top-level windows) are not visible until you request that they be shown, but other components are visible as soon as you create them.
Finally, to be seen, a component must be showing. You show a component by adding it to its container. For something to be showing, it must be visible and be in a container that is visible and showing.
A subsidiary aspect of state is the enabled quality, which determines whether a component can accept input.
- public boolean isValid ()
- The
isValid()
method tells you whether or not the component needs to be laid out. - public void validate ()
- The
validate()
method sets the component's valid state totrue
. Ordinarily, this is done for you when theComponent
is laid out by itsContainer
. Since objects are invalid when they are first drawn on the screen, you should callvalidate()
to tell the system you are finished adding objects so that it can validate the screen and components. One reason you can overridevalidate()
is to find out when the container that the component exists in has been resized. The only requirement when overriding is that the originalvalidate()
be called. With Java 1.1, instead of overriding, you can listen for resize events. - public void invalidate ()
- The
invalidate()
method sets the component's valid state tofalse
and propagates the invalidation to its parent. Ordinarily, this is done for you, or should be, whenever anything that affects the layout is changed. - public boolean isVisible ()
- The
isVisible()
methods tells you if the component is currently visible. Most components are initially visible, except for top-level objects like frames. Any component that is visible will be shown on the screen when the screen is painted. - public boolean isShowing ()
- The
isShowing()
method tells you if the component is currently shown on the screen. It is possible forisVisible()
to returntrue
andisShowing()
to returnfalse
if the screen has not been painted yet.
Table 5.1 compares possible return values from isVisible()
and isShowing()
. The first two entries are for objects that have their own Window
. These will always return the same values for isVisible()
and isShowing()
. The next three are for Component
objects that exist within a Window
, Panel
, or Applet
. The visible setting is always initially true
. However, the showing setting is not true until the object is actually drawn. The last case shows another possibility. If the component exists within an invisible Container
, the component will be visible but will not be shown.
Happenings | isVisible | isShowing |
---|---|---|
Frame created
| false
| false
|
Frame f = new Frame ()
| ||
Frame showing
| true
| true
|
f.show ()
| ||
Component created
| true
| false
|
Button b= new Button ("Help")
| ||
Button added to screen in init()
| true
| false
|
add (b)
| ||
Container laid out with Button in it
| true
| true
|
Button within Panel that is not visible
| true
| false |
- public void show ()
- The
show()
method displays a component by making it visible and showing its peer. The parentContainer
becomes invalid because the set of children to display has changed. You would callshow()
directly to display aFrame
orDialog
.In Java 1.1, you should use
setVisible()
instead. - public void hide ()
- The
hide()
method hides a component by making it invisible and hiding its peer. The parentContainer
becomes invalid because the set of children to display has changed. If you callhide()
for aComponent
that does not subclassWindow
, the component'sContainer
reserves space for the hidden object.In Java 1.1, you should use
setVisible()
instead. - public void setVisible(boolean condition)
public void show (boolean condition) - The
setVisible()
method calls eithershow()
orhide()
based on the value ofcondition
. Ifcondition
istrue
,show()
is called. Whencondition
isfalse
,hide()
is called.show()
is the Java 1.0 name for this method. - public boolean isEnabled ()
- The
isEnabled()
method checks to see if the component is currently enabled. An enabledComponent
can be selected and trigger events. A disabledComponent
usually has a slightly lighter font and doesn't permit the user to select or interact with it. Initially, everyComponent
is enabled. - public synchronized void enable ()
- The
enable()
method allows the user to interact with the component. Components are enabled by default but can be disabled by a call todisabled()
orsetEnabled(false)
.In Java 1.1, you should use
setEnabled()
instead. - public synchronized void disable ()
- The
disable()
method disables the component so that it is unresponsive to user interactions.In Java 1.1, you should use
setEnabled()
instead. - public void setEnabled (boolean condition)
public void enable (boolean condition) - The
setEnabled()
method calls eitherenable()
ordisable()
based on the value ofcondition
. Ifcondition
istrue
,enable()
is called. Whencondition
isfalse
,disable()
is called. Enabling and disabling lets you create components that can be operated only under certain conditions--for example, aButton
that can be pressed only after the user has typed into aTextArea
.enable()
is the Java 1.0 name for this method.
Although there was some support for managing input focus in version 1.0, 1.1 improved on this greatly by including support for Tab and Shift+Tab to move input focus to the next or previous component, and by being more consistent across different platforms. This support is provided by the package-private class FocusManager
.
- public boolean isFocusTraversable()
- The
isFocusTraversable()
method is the support method that tells you whether or not a component is capable of receiving the input focus. Every component asks its peer whether or not it is traversable. If there is no peer, this method returnsfalse
.If you are creating a component by subclassing
Component
orCanvas
and you want it to be traversable, you should override this method; aCanvas
is not traversable by default. - public void requestFocus ()
- The
requestFocus()
method allows you to request that a component get the input focus. If it can't (isFocusTraversable()
returnsfalse
), it won't. - public void transferFocus ()
public void nextFocus () - The
transferFocus()
method moves the focus from the current component to the next one.nextFocus()
is the Java 1.0 name for this method.
- public final Object getTreeLock ()
- The
getTreeLock()
method retrieves the synchronization lock for all AWT components. Instead of usingsynchronized
methods in Java 1.1, previously synchronized methods lock the tree within asynchronized (component.getTreeLock()) {}
code block. This results in a more efficient locking mechanism to improve performance. - public String getName ()
- The
getName()
method retrieves the current name of the component. The component's name is useful for object serialization. Components are given a name by default; you can change the name by callingsetName()
. - public void setName (String name)
- The
setName()
method changes the name of the component toname
. - public Container getParent ()
- The
getParent()
method returns the component'sContainer
. The container for anything added to an applet is the applet itself, since it subclassesPanel
. The container for the applet is the browser. In the case of Netscape Navigator versions 2.0 and 3.0, the return value would be a specific instance of thenetscape.applet.EmbeddedAppletFrame
class. If the applet is running within the appletviewer, the return value would be an instance ofsun.applet.AppletViewerPanel
. - public synchronized void add(PopupMenu popup)
- The
add()
method introduced in Java 1.1 provides the ability to associate aPopupMenu
with aComponent
. The pop-up menu can be used to provide context-sensitive menus for specific components. (On some platforms for some components, pop-up menus exist already and cannot be overridden.) Interaction with the menu is discussed in Would You Like to Choose from the Menu?Multiple pop-up menus can be associated with a component. To display the appropriate pop-up menu, call the pop-up menu's
show()
method. - public synchronized void remove(MenuComponent popup)
- The
remove()
method is theMenuContainer
interface method to disassociate thepopup
from the component. (PopupMenu
is a subclass ofMenuComponent
.) Ifpopup
is not associated with theComponent
, nothing happens. - protected String paramString ()
- The
paramString()
method is a protected method that helps build aString
listing the different parameters of theComponent
. When thetoString()
method is called for a specificComponent
,paramString()
is called for the lowest level and works its way up the inheritance hierarchy to build a complete parameter string to display. At theComponent
level, potentially seven ( Java1.0) or eight (1.1) items are added. The first five items added are the component's name (if non-null and using Java 1.1), x and y coordinates (as returned bygetLocation()
), along with its width and height (as returned bygetSize()
). If the component is not valid, "invalid" is added next. If the component is not visible, "hidden" is added next. Finally, if the component is not enabled, "disabled" is added. - public String toString ()
- The
toString()
method returns aString
representation of the object's values. At theComponent
level, the class's name is placed before the results ofparamString()
. This method is called automatically by the system if you try to print an object usingSystem.out.println()
. - public void list ()
- The
list()
method prints the contents of theComponent
(as returned bytoString()
) toSystem.out
. Ifc
is a type ofComponent
, the two statementsSystem.out.println(c)
andc.list()
are equivalent. This method is more useful at theContainer
level, because it prints all the components within the container. - public void list (PrintWriter out)
public void list (PrintStream out) - This version of
list()
prints the contents of theComponent
(as returned bytoString()
) to a differentPrintStream
,out
. - public void list (PrintWriter out, int indentation)
public void list (PrintStream out, int indentation) - These versions of
list()
are called by the other two. They print the component's contents (as returned bytoString()
) with the given indentation. This allows you to prepare nicely formatted lists of a container's contents for debugging; you could use the indentation to reflect how deeply the component is nested within the container.
Component Events
Events covers event handling in detail. This section summarizes what Component
does for the different event-related methods.
With the Java 1.0 event model, many methods return true
to indicate that the program has handled the event and false
to indicate that the event was not handled (or only partially handled); when false
is returned, the system passes the event up to the parent container. Thus, it is good form to return true
only when you have fully handled the event, and no further processing is necessary.
With the Java 1.1 event model, you register a listener for a specific event type. When that type of event happens, the listener is notified. Unlike the 1.0 model, you do not need to override any methods of Component
to handle the event. Controllers
The Java 1.0 event model controllers are deliverEvent()
, postEvent()
, and handleEvent()
. With 1.1, the controller is a method named dispatchEvent()
.
- public void deliverEvent (Event e)
- The
deliverEvent()
method delivers the 1.0Event
e
to theComponent
in which an event occurred. Internally, this method callspostEvent()
. ThedeliverEvent()
method is an important enhancement topostEvent()
forContainer
objects since they have to determine which component in theContainer
gets the event. - public boolean postEvent (Event e)
- The
postEvent()
method tells theComponent
to deal with 1.0Event
e
. It callshandleEvent()
, which returnstrue
if some other object handlede
andfalse
if no one handles it. IfhandleEvent()
returnsfalse
,postEvent()
posts theEvent
to the component's parent. You can usepostEvent()
to hand any events you generate yourself to some other component for processing. (Creating your own events is a useful technique that few developers take advantage of.) You can also usepostEvent()
to reflect an event from one component into another. - public boolean handleEvent (Event e)
- The
handleEvent()
method determines the type of evente
and passes it along to an appropriate method to deal with it. For example, when a mouse motion event is delivered topostEvent()
, it is passed off tohandleEvent()
, which callsmouseMove()
. As shown in the following listing,handleEvent()
can be implemented as one big switch statement. Since not all event types have default event handlers, you may need to override this method. If you do, remember to call the overridden method to ensure that the default behavior still takes place. To do so, callsuper.handleEvent(event)
for any event your method does not deal with.
public boolean handleEvent(Event event) { switch (event.id) { case Event.MOUSE_ENTER: return mouseEnter (event, event.x, event.y); case Event.MOUSE_EXIT: return mouseExit (event, event.x, event.y); case Event.MOUSE_MOVE: return mouseMove (event, event.x, event.y); case Event.MOUSE_DOWN: return mouseDown (event, event.x, event.y); case Event.MOUSE_DRAG: return mouseDrag (event, event.x, event.y); case Event.MOUSE_UP: return mouseUp (event, event.x, event.y); case Event.KEY_PRESS: case Event.KEY_ACTION: return keyDown (event, event.key); case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: return keyUp (event, event.key); case Event.ACTION_EVENT: return action (event, event.arg); case Event.GOT_FOCUS: return gotFocus (event, event.arg); case Event.LOST_FOCUS: return lostFocus (event, event.arg); } return false; }
- public final void dispatchEvent(AWTEvent e)
- The
dispatchEvent()
method allows you to post new AWT events to this component's listeners.dispatchEvent()
tells theComponent
to deal with theAWTEvent e
by calling itsprocessEvent()
method. This method is similar to Java 1.0'spostEvent()
method. Events delivered in this way bypass the system's event queue. It's not clear why you would want to bypass the event queue, except possibly to deliver some kind of high priority event.
- public boolean action (Event e, Object o)
- The
action()
method is called when the user performs some action in theComponent
.e
is the 1.0Event
instance for the specific event, while the content ofo
varies depending upon the specificComponent
. The particular action that triggers a call toaction()
depends on theComponent
. For example, with aTextField
,action()
is called when the user presses the carriage return. This method should not be called directly; to deliver any event you generate, callpostEvent()
, and let it decide how the event should propagate.The default implementation of the
action()
method does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.action(e, o)
to ensure that the event propagates to the component's container or component's superclass, respectively.
- public boolean keyDown (Event e, int key)
- The
keyDown()
method is called whenever the user presses a key.e
is the 1.0Event
instance for the specific event, whilekey
is the integer representation of the character pressed. The identifier for the event (e.id
) could be eitherEvent.KEY_PRESS
for a regular key orEvent.KEY_ACTION
for an action-oriented key (e.g., arrow or function key). The defaultkeyDown()
method does nothing and returnsfalse
. If you are doing input validation, returntrue
if the character is invalid; this keeps the event from propagating to a higher component. If you wish to alter the input (i.e., convert to uppercase), returnfalse
, but changee.key
to the new character. - public boolean keyUp (Event e, int key)
- The
keyUp()
method is called whenever the user releases a key.e
is theEvent
instance for the specific event, whilekey
is the integer representation of the character pressed. The identifier for the event (e.id
) could be eitherEvent.KEY_RELEASE
for a regular key orEvent.KEY_ACTION_RELEASE
for an action-oriented key (e.g., arrow or function key).keyUp()
may be used to determine how longkey
has been pressed. The defaultkeyUp()
method does nothing and returnsfalse
.
NOTE:
Early releases of Java (1.0.2 and earlier) propagated only mouse events from Canvas
and Container
objects. However, Netscape Navigator seems to have jumped the gun and corrected the situation with their 3.0 release, which is based on Java release 1.0.2.1. Until other Java releases catch up, use these events with care. For more information on platform dependencies, see Appendix C, Platform-Specific Event Handling.
- public boolean mouseDown (Event e, int x, int y)
- The
mouseDown()
method is called when the user presses a mouse button over theComponent
.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated. It is necessary to examine the modifiers field ofe
to determine which mouse button the user pressed. The defaultmouseDown()
method does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseDown(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively. - public boolean mouseDrag (Event e, int x, int y)
- The
mouseDrag()
method is called when the user is pressing a mouse button and moves the mouse.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated.mouseDrag()
could be called multiple times as the mouse is moved. The defaultmouseDrag()
method does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseDrag(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively. - public boolean mouseEnter (Event e, int x, int y)
- The
mouseEnter()
method is called when the mouse enters theComponent
.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated. The defaultmouseEnter()
method does nothing and returnsfalse
.mouseEnter()
can be used for implementing balloon help. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseEnter(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively. - public boolean mouseExit (Event e, int x, int y)
- The
mouseExit()
method is called when the mouse exits theComponent
.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated. The default methodmouseExit()
does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseExit(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively. - public boolean mouseMove (Event e, int x, int y)
- The
mouseMove()
method is called when the user moves the mouse without pressing a mouse button.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated.mouseMove()
will be called numerous times as the mouse is moved. The defaultmouseMove()
method does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseMove(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively. - public boolean mouseUp (Event e, int x, int y)
- The
mouseUp()
method is called when the user releases a mouse button over theComponent
.e
is theEvent
instance for the specific event, whilex
andy
are the coordinates where the cursor was located when the event was initiated. The defaultmouseUp()
method does nothing and returnsfalse
. When you override this method, returntrue
only if you fully handle the event. Your method should always have a default case that returnsfalse
or callssuper.mouseUp(e, x, y)
to ensure that the event propagates to the component's container or component's superclass, respectively.
Focus events indicate whether a component can get keyboard input. Not all components can get focus (e.g., Label
cannot). Precisely which components can get the focus is platform specific.
Ordinarily, the item with the focus has a light gray rectangle around it, though the actual display depends on the platform and the component. Figure 5.1 displays the effect of focus for buttons in Windows.
Figure 5.1: Focused and UnFocused buttons
NOTE:
Early releases of Java (1.0.2 and earlier) do not propagate all focus events on all platforms. Java 1.1 seems to propagate them properly. For more information on platform dependencies, see Appendix C, Platform-Specific Event Handling.
- public boolean gotFocus (Event e, Object o)
- The
gotFocus()
method is triggered when theComponent
gets the input focus.e
is the 1.0Event
instance for the specific event, while the content ofo
varies depending upon the specificComponent
. The defaultgotFocus()
method does nothing and returnsfalse
. For aTextField
, when the cursor becomes active, it has the focus. When you override this method, returntrue
to indicate that you have handled the event completely orfalse
if you want the event to propagate to the component's container. - public boolean lostFocus (Event e, Object o)
- The
lostFocus()
method is triggered when the input focus leaves theComponent
.e
is theEvent
instance for the specific event, while the content ofo
varies depending upon the specificComponent
. The defaultlostFocus()
method does nothing and returnsfalse
. When you override this method, returntrue
to indicate that you have handled the event completely orfalse
if you want the event to propagate to the component's container.
With the 1.1 event model, you receive events by registering event listeners, which are told when the event happens. Components don't have to receive and handle their own events; you can cleanly separate the event-handling code from the user interface itself. This section covers the methods used to add and remove event listeners, which are part of the Component
class. There is a pair of methods to add and remove listeners for each event type that is appropriate for a Component
: ComponentEvent
, FocusEvent
, KeyEvent
, MouseEvent
, and MouseMotionEvent
. Subclasses of Component
may have additional event types and therefore will have additional methods for adding and removing listeners. For example, Button
, List
, MenuItem
, and TextField
each generate action events and therefore have methods to add and remove action listeners. These additional listeners are covered with their respective components.
- public void addComponentListener(ComponentListener listener)
- The
addComponentListener()
method registerslistener
as an object interested in being notified when aComponentEvent
passes through theEventQueue
with thisComponent
as its target. When such an event occurs, a method in theComponentListener
interface is called. Multiple listeners can be registered. - public void removeComponentListener(ComponentListener listener)
- The
removeComponentListener()
method removeslistener
as a interested listener. Iflistener
is not registered, nothing happens. - public void addFocusListener(FocusListener listener)
- The
addFocusListener()
method registerslistener
as an object interested in being notified when aFocusEvent
passes through theEventQueue
with thisComponent
as its target. When such an event occurs, a method in theFocusListener
interface is called. Multiple listeners can be registered. - public void removeFocusListener(FocusListener listener)
- The
removeFocusListener()
method removeslistener
as a interested listener. Iflistener
is not registered, nothing happens. - public void addKeyListener(KeyListener listener)
- The
addKeyListener()
method registerslistener
as an object interested in being notified when aKeyEvent
passes through theEventQueue
with thisComponent
as its target. When such an event occurs, a method in theKeyListener
interface is called. Multiple listeners can be registered. - public void removeKeyListener(KeyListener listener)
- The
removeKeyListener()
method removeslistener
as a interested listener. If listener is not registered, nothing happens. - public void addMouseListener(MouseListener listener)
- The
addMouseListener()
method registerslistener
as an object interested in being notified when a nonmotion-orientedMouseEvent
passes through theEventQueue
with thisComponent
as its target. When such an event occurs, a method in theMouseListener
interface is called. Multiple listeners can be registered. - public void removeMouseListener(MouseListener listener)
- The
removeMouseListener()
method removeslistener
as a interested listener. If listener is not registered, nothing happens. - public void addMouseMotionListener(MouseMotionListener listener)
- The
addMouseMotionListener()
method registerslistener
as an object interested in being notified when a motion-orientedMouseEvent
passes through theEventQueue
with thisComponent
as its target. When such an event occurs, a method in theMouseMotionListener
interface is called. Multiple listeners can be registered.The mouse motion-oriented events are separate from the other mouse events because of their frequency of generation. If they do not have to propagate around, resources can be saved.
- public void removeMouseMotionListener(MouseMotionListener listener)
- The
removeMouseMotionListener()
method removeslistener
as a interested listener. Iflistener
is not registered, nothing happens.
Under the 1.1 event model, it is still possible for components to receive their own events, simulating the old event mechanism. If you want to write components that process their own events but are also compatible with the new model, you can override processEvent()
or one of its related methods. processEvent()
is logically similar to handleEvent()
in the old model; it receives all the component's events and sees that they are forwarded to the appropriate listeners. Therefore, by overriding processEvent()
, you get access to every event the component generates. If you want only a specific type of event, you can override processComponentEvent()
, processKeyEvent()
, or one of the other event-specific methods.
However, there is one problem. In Java 1.1, events aren't normally generated if there are no listeners. Therefore, if you want to receive your own events without registering a listener, you should first enable event processing (by a call to enableEvent()
) to make sure that the events you are interested in are generated.
- protected final void enableEvents(long eventsToEnable)
- The
enableEvents()
method allows you to configure a component to listen for events without having any active listeners. Under normal circumstances (i.e., if you are not subclassing a component), it is not necessary to call this method.The
eventsToEnable
parameter contains a mask specifying which event types you want to enable. TheAWTEvent
class (covered in Events) contains constants for the following types of events:COMPONENT_EVENT_MASK
CONTAINER_EVENT_MASK
FOCUS_EVENT_MASK
KEY_EVENT_MASK
MOUSE_EVENT_MASK
MOUSE_MOTION_EVENT_MASK
WINDOW_EVENT_MASK
ACTION_EVENT_MASK
ADJUSTMENT_EVENT_MASK
ITEM_EVENT_MASK
TEXT_EVENT_MASKOR the masks for the events you want; for example, call
enableEvents(MOUSE_EVENT_MASK | MOUSE_MOTION_EVENT_MASK)
to enable all mouse events. Any previous event mask settings are retained. - protected final void disableEvents(long eventsToDisable)
- The
disableEvents()
method allows you to stop the delivery of events when they are no longer needed.eventsToDisable
is similar to theeventsToEnable
parameter but instead contains a mask specifying which event types to stop. A disabled event would still be delivered if someone were listening. - protected void processEvent(AWTEvent e)
- The
processEvent()
method receives allAWTEvent
with thisComponent
as its target.processEvent()
then passes them along to one of the event-specific processing methods (e.g.,processKeyEvent()
). When you subclassComponent
, overridingprocessEvent()
allows you to process all events without providing listeners. Remember to callsuper.processEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. OverridingprocessEvent()
is like overriding thehandleEvent()
method using the 1.0 event model. - protected void processComponentEvent(ComponentEvent e)
- The
processComponentEvent()
method receivesComponentEvent
with thisComponent
as its target. If any listeners are registered, they are then notified. When you subclassComponent
, overridingprocessComponentEvent()
allows you to process component events without providing listeners. Remember to callsuper.processComponentEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. OverridingprocessComponentEvent()
is roughly similar to overridingresize()
,move()
,show()
, andhide()
to add additional functionality when those methods are called. - protected void processFocusEvent(FocusEvent e)
- The
processFocusEvent()
method receivesFocusEvent
with thisComponent
as its target. If any listeners are registered, they are then notified. When you subclassComponent
, overridingprocessFocusEvent()
allows you to process the focus event without providing listeners. Remember to callsuper.processFocusEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. OverridingprocessFocusEvent()
is like overriding the methodsgotFocus()
andlostFocus()
using the 1.0 event model. - protected void processKeyEvent(KeyEvent e)
- The
processKeyEvent()
method receivesKeyEvent
with thisComponent
as its target. If any listeners are registered, they are then notified. When you subclassComponent
, overridingprocessKeyEvent()
allows you to process key events without providing listeners. Be sure to remember to callsuper.processKeyEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. OverridingprocessKeyEvent()
is roughly similar to overridingkeyDown()
andkeyUp()
with one method using the 1.0 event model. - protected void processMouseEvent(MouseEvent e)
- This
processMouseEvent()
method receives all nonmotion-orientedMouseEvent
s with thisComponent
as its target. If any listeners are registered, they are then notified. When you subclassComponent
, overriding the methodprocessMouseEvent()
allows you to process mouse events without providing listeners. Remember to callsuper.processMouseEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. Overriding the methodprocessMouseEvent()
is roughly similar to overridingmouseDown()
,mouseUp()
,mouseEnter()
, andmouseExit()
with one method using the 1.0 event model. - protected void processMouseMotionEvent(MouseEvent e)
- The
processMouseMotionEvent()
method receives all motion-orientedMouseEvents
with thisComponent
as its target. If there are any listeners registered, they are then notified. When you subclassComponent
, overridingprocessMouseMotionEvent()
allows you to process mouse motion events without providing listeners. Remember to callsuper.processMouseMotionEvent(e)
last to ensure that normal event processing still occurs; if you don't, events won't get distributed to any registered listeners. Overriding the methodprocessMouseMotionEvent()
is roughly similar to overridingmouseMove()
andmouseDrag()
with one method using the 1.0 event model.