The following Java code shows an example program that implements the
KeyListener interface. When executed, the Java code will show a very simple Swing Graphical User Interface.
Background
The GUI is made up of a
JFrame which contains two JTextAreas . The first, feedbackText JTextArea , is placed inside a JScrollPane and is used to display text generated by the KeyListener events. The JScrollPane allows the user to see all the lines of text generated by the KeyListener events.
The second is the
inputText JTextArea . This JTextArea has the focus and will generate KeyListener events as the user types into it. By default, the inputArea JTextArea will have the focus when the JFrame appears.
The
KeyListener interface could have been implemented as a separate class, or extending the JFrame , but in this instance using an anonymous inner class makes the most sense.
Key events occur when a key is pressed on the keyboard. Any component can generate these events, and a class must implement KeyListener interface to support them. The following example shows how to use key events in Swing.
The
keyPressed method is called when a user presses down on a key and the keyReleased method is called when a key is released. The keyTyped method is called when a character key is typed into the inputText JTextArea .
Java Code Listing
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:
Photodex proshow producer crack. Thus, a combo box supports action, item, and popup menu listeners in addition to the listener methods it inherits from
JComponent .
![]()
Listeners supported by Swing components fall into two categories:
Listeners that All Swing Components Support
Because all Swing components descend from the AWT
Component class, you can register the following listeners on any Swing component:
All Swing components descend from the AWT
Container class, but many of them are not 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. Parallels 12 activation key generator torrent.
JComponent provides support for three more listener types. You can register an ancestor listener 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.
The other two listener types are part of the Swing components' conformance to the JavaBeans 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, to track changes on a component's bound properties. Also, property change listeners, as well as vetoable change listeners are used by builder tools to listen for changes on constrained properties. For more information refer to the Properties lesson in the JavaBeans trail.
Other Listeners that Swing Components SupportJpanel Doesn't Generate Key Events
The following table lists Swing components and the specialized listeners they support, not including listeners supported by all
Component s, Container s, or JComponent s. 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 are interested in, go first to the component how-to section, and then if necessary to the listener how-to section.
Jpanel Doesn't Generate Key Event Calendar
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |