propertychangelistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 655 行 · 第 1/2 页
HTML
655 行
<a href=index.html target=_top>Writing Event Listeners</a> </span> <div class=NavBit> <a target=_top href=mousewheellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=tablemodellistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Property Change Listener</div> <blockquote>Property-change events occur whenever the value of a<em>bound property</em> changesfor a <em>bean</em> —a component that conforms to theJavaBeans<font size="-2"><sup>TM</sup></font> specification.You can find out more about beans from the <a class="TutorialLink" target="_top" href="../../javabeans/">JavaBeans</a> trail of the<a class="TutorialLink" target="_top" href="../../java/">Java Tutorial</a>. All Swing components are also beans.<p>A JavaBeans property is accessed through its<em>get</em> and <em>set</em> methods.For example, <code>JComponent</code> has the property <em>font</em> which is accessiblethrough <code>getFont</code> and<code>setFont</code> methods.<p>A <em>bound</em> property fulfills the specialrequirement that, besides the <em>get</em> and <em>set</em>methods, it fires a property-change eventwhen its value changes.If you are interested in more information, see the<a class="TutorialLink" target="_top" href="../../javabeans/properties/bound.html">Bound Properties</a> page in the JavaBeans trail.<p>Some scenarios that commonly require property-change listenersinclude:<ul><li>You have implemented a formatted text field and need a way to detect when the user has entered a new value. You can register a property-change listener on the formatted text field to listen to changes on the <em>value</em> property. See <code>FormattedTextFieldDemo</code> in<a class="TutorialLink" target="_top" href="../components/formattedtextfield.html#value">How to Use Formatted Text Fields</a> for an example of this.<p><li>You have implemented a dialog and need to know when a user has clicked one of the dialog's buttons or changed a selection in the dialog. See <code>DialogDemo</code> in<a class="TutorialLink" target="_top" href="../components/dialog.html#stayup">How to Make Dialogs</a> for an example of registering a property-change listener on an option pane to listen to changes to the <em>value</em> property. You can also check out <code>FileChooserDemo2</code> in<a class="TutorialLink" target="_top" href="../components/filechooser.html#advancedexample">How to Use File Choosers</a> for an example of how to register a property-change listener to listen to changes to the <em>directoryChanged</em> and <em>selectedFileChanged</em> properties.<p><li>You need to be notified when the component that has the focus changes. You can register a property-change listener on the keyboard focus manager to listen to changes to the <em>focusOwner</em> property. See <code>TrackFocusDemo</code> and <code>DragPictureDemo</code> in<a class="TutorialLink" target="_top" href="../misc/focus.html#trackingFocus">How to Use the Focus Subsystem</a> for examples of this.</ul><p>Although these are some of the more common uses forproperty-change listeners, you can register a property-changelistener on the bound property of any component that conformsto the JavaBeans specification.<p>You can register a property change listener in two ways.The first uses the method<code>addPropertyChangeListener(PropertyChangeListener)</code>.When you register a listener this way, you are notifiedof every change to every bound property for that object.In the <code>propertyChange</code> method, you can get the nameof the property that has changed using the<code>PropertyChangeEvent</code> <code>getPropertyName</code>method, as in the following code snippet:<blockquote><pre>KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();focusManager.addPropertyChangeListener(new FocusManagerListener());...public FocusManagerListener() implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("focusOwner".equals(propertyName) { ... } else if ("focusedWindow".equals(propertyName) { ... } } ...}</pre></blockquote><p>The second way to register a property change listener uses the method<code>addPropertyChangeListener(String, PropertyChangeListener)</code>.The <code>String</code> argument is the name of a property. Using thismethod means that you only receive notification whena change occurs to that particular property. So, for example,if you registered a property change listener like this:<blockquote><pre>aComponent.addPropertyChangeListener("font", new FontListener());</pre></blockquote><p><code>FontListener</code> only receives notification when thevalue of the component's<em>font</em> property changes. It does <em>not</em>receive notification when the value changes for <em>transferHandler</em>,<em>opaque</em>, <em>border</em>, or any other property.<p>The following example shows how to register a propertychange listener on the <em>value</em> property of aformatted text field using the two-argument version of<code>addPropertyChangeListener</code>:<blockquote><pre><em>//...where initialization occurs:</em>double amount;JFormattedTextField amountField;...amountField.addPropertyChangeListener("value", new FormattedTextFieldListener());...class FormattedTextFieldListener implements PropertyChangeListener { public void propertyChanged(PropertyChangeEvent e) { Object source = e.getSource(); if (source == amountField) { amount = ((Number)amountField.getValue()).doubleValue(); ... } <em>...//re-compute payment and update field...</em> }}</pre></blockquote></blockquote><h3><a name="api">The Property Change Listener API</a></h3><blockquote><p align=center><a name="addpropertychangelistener">Registering a PropertyChangeListener</a><p><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#addPropertyChangeListener(java.beans.PropertyChangeListener)">addPropertyChangeListener(PropertyChangeListener)</a></td><td>Add a property-change listener to the listener list.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)">addPropertyChangeListener(String, PropertyChangeListener)</a></td><td>Add a property-change listener for a specific property. The listener is called only when there is a change to the specified property.</td></tr></table><p align=center><a name="propertychangelistener">The PropertyChangeListener Interface</a><p><em>Because <code>PropertyChangeListener</code> has only one method, it has no corresponding adapter class.</em><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/beans/PropertyChangeListener.html#propertyChange(java.beans.PropertyChangeEvent)">propertyChange(PropertyChangeEvent)</a></td><td>Called when the listened-to bean changes a bound property.</td></tr></table><p align=center><a name="propertychangeevent">The PropertyChangeEvent Class</a><p><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/beans/PropertyChangeEvent.html#getNewValue()">Object getNewValue()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/beans/PropertyChangeEvent.html#getOldValue()">Object getOldValue()</a></td><td>Return the new, or old, value of the property, respectively.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/beans/PropertyChangeEvent.html#getPropertyName()">String getPropertyName()</a></td><td>Return the name of the property that was changed.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Property Change Listeners</h3></a><blockquote>The following table lists theexamples that use property-change listeners.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr valign=top><td><a href="../components/examples/index.html#FormattedTextFieldDemo"><code>FormattedTextFieldDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/formattedtextfield.html#value">How to Use Formatted Text Fields</a></td><td> A property-change listener is registered on several formatted text fields to track changes to the <em>value</em> property.</td></tr><tr valign=top><td><a href="../components/examples/index.html#DialogDemo"><code>DialogDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/dialog.html#stayup">How to Make Dialogs</a></td><td> The<a class="TutorialLink" target="_top" href="../components/examples/CustomDialog.java"><code>CustomDialog</code></a> class registers a property-change listener onan option pane to listen to the <em>value</em>and <em>inputValue</em> properties.</td></tr><tr valign=top><td><a href="../components/examples/index.html#FileChooserDemo2"><code>FileChooserDemo2</code></a></td><td><a class="TutorialLink" target="_top" href="../components/filechooser.html#advancedexample">How to Use File Choosers</a></td><td> The<a class="TutorialLink" target="_top" href="../components/examples/ImagePreview.java"><code>ImagePreview</code></a> class registers a property-change listener onthe file chooser to listen to the <em>directoryChanged</em> and<em>selectedFileChanged</em> properties.</td></tr><tr valign=top><td><a href="../misc/examples/index.html#TrackFocusDemo"><code>TrackFocusDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../misc/focus.html#trackingFocus">How to Use the Focus Subsystem</a></td><td> A property-change listener is registeredon the keyboard focus manager to track changesto the <em>focusOwner</em> property.</td></tr></table></blockquote> </blockquote> <div class=NavBit> <a target=_top href=mousewheellistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=tablemodellistener.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Write a Mouse-Wheel Listener <br><b>Next page:</b> How to Write a Table Model Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?