Listeners Supported by Swing Components [zzThe JavaTM Tutorial ]

Listeners Supported by Swing Components

You can tell what kinds of events a component can fire by looking at the kinds of event listeners you can register on it. For example, the JComboBox class defines these listener registration methods:
  • addActionListener
  • addItemListener
  • addPopupMenuListener
Thus, a combo box supports action, item, and popup menu listeners in addition to the listener methods it inherits from JComponent.

A component fires only those events for which listeners have registered on it. For example, if an action listener is registered on a particular combo box, but the combo box has no other listeners, then the combo box will fire only action events — no item or popup menu events.

Listeners supported by Swing components fall into two categories:

Because all Swing components descend from the AWT Component class, you can register the following listeners on any Swing component: component listener Listens for changes in the component's size, position, or visibility. focus listener Listens for whether the component gained or lost the ability to receive keyboard input. key listener Listens for key presses; key events are fired only by the component that has the current keyboard focus. mouse listener Listens for mouse clicks and mouse movement into or out of the component's drawing area. mouse-motion listener Listens for changes in the cursor's position over the component. mouse-wheel listener (introduced in 1.4) Listens for mouse wheel movement over the component.

Two listener types introduced in release 1.3, HierarchyListener (in the API reference documentation) and HierarchyBoundsListener (in the API reference documentation), listen to changes to a component's containment hierarchy. These listener types aren't useful to most programs and can generally be ignored.

All Swing components descend from the AWT Container class, but many of them aren't used as containers. So, technically speaking, any Swing component can fire container events, which notify listeners that a component has been added to or removed from the container. Realistically speaking, however, only containers (such as panels and frames) and compound components (such as combo boxes) typically fire container events.

JComponent provides support for three more listener types. You can register an ancestor listener (in the API reference documentation) to be notified when a component's containment ancestors are added to or removed from a container, hidden, made visible, or moved. This listener type is an implementation detail which predated hierarchy listeners and can generally be ignored.

The other two listener types are part of the Swing components' conformance to the JavaBeansTM specification. Among other things, this means that Swing components support bound and constrained properties and notify listeners of changes to the properties. Property change listeners listen for changes to bound properties and are used by several Swing components, such as formatted text fields (in the Creating a GUI with JFC/Swing trail), to track changes on a component's bound properties. Also, property change listeners, as well as vetoable change listeners (in the API reference documentation) are used by builder tools to listen for changes on constrained properties. For more information refer to the Properties (in the Creating a GUI with JFC/Swing trail) lesson in the JavaBeans (in the Creating a GUI with JFC/Swing trail) trail.

Other Listeners that Swing Components Support
The following table lists Swing components and the specialized listeners they support, not including listeners supported by all Components, Containers, or JComponents. In many cases, the events are fired directly from the component. In other cases, the events are fired from the component's data or selection model. To find out the details for the particular component and listener you're interested in, go first to the component how-to section, and then if necessary to the listener how-to section.

ComponentListener
actioncaretchangedocument,
undoable edit
itemlist
selection
windowother
button

 

checked

 

 

 

checked

 

 

 

checked

 

   
check box

 

checked

 

 

 

checked

 

 

 

checked

 

   
color chooser  

 

checked

 

     
combo box

 

checked

 

   

 

checked

 

   
dialog      

 

checked

 

 
editor pane 

 

checked

 

 

 

checked

 

   hyperlink (in the API reference documentation)
file chooser

 

checked

 

       
formatted text field

 

checked

 

 

checked

 

 

 

checked

 

    
frame      

 

checked

 

 
internal frame       internal frame
list     

 

checked

 

 list data
menu       menu (in the API reference documentation)
menu item

 

checked

 

 

 

checked

 

 

 

checked

 

  menu key (in the API reference documentation)
menu drag mouse (in the API reference documentation)
option pane        
password field

 

checked

 

 

checked

 

 

 

checked

 

    
popup menu       popup menu (in the API reference documentation)
progress bar  

 

checked

 

     
radio button

 

checked

 

 

 

checked

 

 

 

checked

 

   
slider  

 

checked

 

     
spinner  

 

checked

 

     
tabbed pane  

 

checked

 

     
table     

 

checked

 

   table model
table column model (in the API reference documentation)
cell editor (in the API reference documentation)
text area 

 

checked

 

 

 

checked

 

    
text field

 

checked

 

 

checked

 

 

 

checked

 

    
text pane 

 

checked

 

 

 

checked

 

     hyperlink (in the API reference documentation)
toggle button

 

checked

 

 

 

checked

 

 

 

checked

 

   
tree         tree expansion
tree will expand
tree model
tree selection
viewport
(used by scrollpane)
  

 

checked

 

     

Listeners that All Swing Components Support

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值