caretlistener.html

来自「jsf、swing的官方指南」· HTML 代码 · 共 538 行 · 第 1/2 页

HTML
538
字号
    <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl>                         <div id=TopBar_right>                             <a target="_blank"                                href="http://java.sun.com/javase/6/download.jsp">Download                                the JDK</a>                            <br>                            <a href="../../search.html" target="_blank">Search the                                Tutorials</a>                            <br>                            <a href="javascript:toggleLeft()"                                id="ToggleLeft">Hide the TOC</a>                        </div>                    </div> </div> </div> </div> </div>    <div class=PrintHeaders>        <b>Trail:</b> Creating a GUI with JFC/Swing        <br><b>Lesson:</b> Writing Event Listeners        <br><b>Section:</b> Implementing Listeners for Commonly Handled Events    </div>    <div id=LeftBar class=LeftBar_shown>        <div id=Contents>            <div class="linkLESSON"><a href="index.html">Writing Event Listeners</a></div><div class="linkAHEAD"><a href="intro.html">Introduction to Event Listeners</a></div><div class="linkAHEAD"><a href="generalrules.html">General Information about Writing Event Listeners</a></div><div class="linkAHEAD"><a href="eventsandcomponents.html">Listeners Supported by Swing Components</a></div><div class="linkAHEAD"><a href="handling.html">Implementing Listeners for Commonly Handled Events</a></div><div class="linkBHEAD"><a href="actionlistener.html">How to Write an Action Listener</a></div><div class="nolinkBHEAD">How to Write a Caret Listener</div><div class="linkBHEAD"><a href="changelistener.html">How to Write a Change Listener</a></div><div class="linkBHEAD"><a href="componentlistener.html">How to Write a Component Listener</a></div><div class="linkBHEAD"><a href="containerlistener.html">How to Write a Container Listener</a></div><div class="linkBHEAD"><a href="documentlistener.html">How to Write a Document Listener</a></div><div class="linkBHEAD"><a href="focuslistener.html">How to Write a Focus Listener</a></div><div class="linkBHEAD"><a href="internalframelistener.html">How to Write an Internal Frame Listener</a></div><div class="linkBHEAD"><a href="itemlistener.html">How to Write an Item Listener</a></div><div class="linkBHEAD"><a href="keylistener.html">How to Write a Key Listener</a></div><div class="linkBHEAD"><a href="listdatalistener.html">How to Write a List Data Listener</a></div><div class="linkBHEAD"><a href="listselectionlistener.html">How to Write a List Selection Listener</a></div><div class="linkBHEAD"><a href="mouselistener.html">How to Write a Mouse Listener</a></div><div class="linkBHEAD"><a href="mousemotionlistener.html">How to Write a Mouse-Motion Listener</a></div><div class="linkBHEAD"><a href="mousewheellistener.html">How to Write a Mouse-Wheel Listener</a></div><div class="linkBHEAD"><a href="propertychangelistener.html">How to Write a Property Change Listener</a></div><div class="linkBHEAD"><a href="tablemodellistener.html">How to Write a Table Model Listener</a></div><div class="linkBHEAD"><a href="treeexpansionlistener.html">How to Write a Tree Expansion Listener</a></div><div class="linkBHEAD"><a href="treemodellistener.html">How to Write a Tree Model Listener</a></div><div class="linkBHEAD"><a href="treeselectionlistener.html">How to Write a Tree Selection Listener</a></div><div class="linkBHEAD"><a href="treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a></div><div class="linkBHEAD"><a href="undoableeditlistener.html">How to Write an Undoable Edit Listener</a></div><div class="linkBHEAD"><a href="windowlistener.html">How to Write Window Listeners</a></div><div class="linkAHEAD"><a href="api.html">Listener API Table</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Event-Handling Problems</a></div></div>    </div>    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Writing Event Listeners</a>            </span>            <div class=NavBit>                <a target=_top href=actionlistener.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=changelistener.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>How to Write a Caret Listener</div>            <blockquote>Caret events occur when the <em>caret</em> &#151; the cursorindicating the insertionpoint &#151; in a text component movesor when the selection in a text component changes.The text component's document can initiate caret eventswhen it inserts or removes text, for example.<h4 align=center><font color=red>[PENDING: Image showing sample caret     forthcoming.]</font></h4>You can attach a caret listener to an instance of any<code>JTextComponent</code> subclass with the<code>addCaretListener</code> method. <blockquote><hr><strong>Note:</strong>&nbsp;An alternate way of detecting caret changes is to attach alistener directly to the caret object itself rather than tothe text component that manages the caret. A caret fires change events (<em>not</em>caret events), so you would need to write a<a href="changelistener.html">change listener</a>rather than a caret listener.<hr></blockquote><p>Here is the caret event handling code from an applicationcalled <code>TextComponentDemo</code>:<blockquote><pre>...<em>//where initialization occurs</em>CaretListenerLabel caretListenerLabel =    new CaretListenerLabel("Caret Status");...textPane.addCaretListener(caretListenerLabel);...protected class CaretListenerLabel extends JLabel                                   implements CaretListener{    ...    //Might not be invoked from the event dispatching thread.    public void caretUpdate(CaretEvent e) {        displaySelectionInfo(e.getDot(), e.getMark());    }    //This method can be invoked from any thread.  It     //invokes the setText and modelToView methods, which     //must run in the event dispatching thread. We use    //invokeLater to schedule the code for execution    //in the event dispatching thread.    protected void displaySelectionInfo(final int dot,                                        final int mark) {        SwingUtilities.invokeLater(new Runnable() {            public void run() {                if (dot == mark) {  // no selection                    ...                }            });        }    }}</pre></blockquote><blockquote><hr><strong>Note:</strong>&nbsp;The <code>caretUpdate</code> method is not guaranteedto be called in the event-dispatching thread.To use any methods inside of <code>caretUpdate</code>that update the GUI specialhandling is required to ensure they are executed onthe event-dispatching thread.  You can do this by wrappingthe code inside a <code>Runnable</code> and calling<code>SwingUtilities.invokeLater</code> on that<code>Runnable</code>.<hr></blockquote>You can find a link to the source file for<code>TextComponentDemo</code> in the<a class="TutorialLink" target="_top" href="../components/examples/index.html#TextComponentDemo">example index for Using Swing Components</a>.  For a discussion about the caret listener aspectof the program see<a class="TutorialLink" target="_top" href="../components/generaltext.html#caret">Listening for Caret and Selection Changes</a> in<a class="TutorialLink" target="_top" href="../components/generaltext.html">Text Component Features</a>.</blockquote><h3><a name="api">The Caret Listener API</a></h3><blockquote><p align=center><a name="caretlistener">The CaretListener   Interface</a><p><em>Because <code>CaretListener</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/javax/swing/event/CaretListener.html#caretUpdate(javax.swing.event.CaretEvent)">caretUpdate(CaretEvent)</a></td><td>Called when the caret in the listened-to component moves    or when the selection in the listened-to component changes.</td></tr></table><p align=center><a name="caretevent">The CaretEvent 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/javax/swing/event/CaretEvent.html#getDot()">int getDot()</a></td><td>Returns the current location of the caret.    If text is selected, the caret marks one end of the selection.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/CaretEvent.html#getMark()">int getMark()</a></td><td>Returns the other end of the selection.    If nothing is selected,    the value returned by this method is equal to the    value returned by <code>getDot</code>.    Note that the dot is not guaranteed to be less than the mark.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/util/EventObject.html#getSource()">Object getSource()</a><br>(<em>in <code>java.util.EventObject</code></em>)</td><td>Returns the object that fired the event.</td></tr></table></blockquote><a name="eg"><h3>Examples that Use Caret Listeners</h3></a><blockquote>The following table lists theexamples that use caret 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#TextComponentDemo"><code>TextComponentDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/generaltext.html#caret">Listening for Caret and Selection Changes</a></td><td> Uses a listener label to display caret and selection status.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=actionlistener.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=changelistener.html>Next &raquo;</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 an Action Listener        <br><b>Next page:</b> How to Write a Change Listener    </div>    </body></html> 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?