src/share/classes/javax/swing/text/JTextComponent.java

Print this page




  65 
  66 import javax.print.attribute.*;
  67 
  68 import sun.awt.AppContext;
  69 
  70 
  71 import sun.swing.PrintingStatus;
  72 import sun.swing.SwingUtilities2;
  73 import sun.swing.text.TextComponentPrintable;
  74 import sun.swing.SwingAccessor;
  75 
  76 /**
  77  * <code>JTextComponent</code> is the base class for swing text
  78  * components.  It tries to be compatible with the
  79  * <code>java.awt.TextComponent</code> class
  80  * where it can reasonably do so.  Also provided are other services
  81  * for additional flexibility (beyond the pluggable UI and bean
  82  * support).
  83  * You can find information on how to use the functionality
  84  * this class provides in
  85  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html">General Rules for Using Text Components</a>,
  86  * a section in <em>The Java Tutorial.</em>
  87  *
  88  * <dl>
  89  * <dt><b><font size=+1>Caret Changes</font></b>
  90  * <dd>
  91  * The caret is a pluggable object in swing text components.
  92  * Notification of changes to the caret position and the selection
  93  * are sent to implementations of the <code>CaretListener</code>
  94  * interface that have been registered with the text component.
  95  * The UI will install a default caret unless a customized caret
  96  * has been set. <br>
  97  * By default the caret tracks all the document changes
  98  * performed on the Event Dispatching Thread and updates it's position
  99  * accordingly if an insertion occurs before or at the caret position
 100  * or a removal occurs before the caret position. <code>DefaultCaret</code>
 101  * tries to make itself visible which may lead to scrolling
 102  * of a text component within <code>JScrollPane</code>. The default caret
 103  * behavior can be changed by the {@link DefaultCaret#setUpdatePolicy} method.
 104  * <br>
 105  * <b>Note</b>: Non-editable text components also have a caret though


2147      * being processed until printing is complete. It is only
2148      * recommended when printing from an application with no
2149      * visible GUI.
2150      *
2151      * <p>
2152      * Note: In <i>headless</i> mode, {@code showPrintDialog} and
2153      * {@code interactive} parameters are ignored and no dialogs are
2154      * shown.
2155      *
2156      * <p>
2157      * This method ensures the {@code document} is not mutated during printing.
2158      * To indicate it visually, {@code setEnabled(false)} is set for the
2159      * duration of printing.
2160      *
2161      * <p>
2162      * This method uses {@link #getPrintable} to render document content.
2163      *
2164      * <p>
2165      * This method is thread-safe, although most Swing methods are not. Please
2166      * see <A
2167      * HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">
2168      * Concurrency in Swing</A> for more information.
2169      *
2170      * <p>
2171      * <b>Sample Usage</b>. This code snippet shows a cross-platform print
2172      * dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode
2173      * unless the user cancels the dialog:
2174      *
2175      * <pre>
2176      * textComponent.print(new MessageFormat(&quot;My text component header&quot;),
2177      *     new MessageFormat(&quot;Footer. Page - {0}&quot;), true, null, null, true);
2178      * </pre>
2179      * <p>
2180      * Executing this code off the <i>Event Dispatch Thread</i>
2181      * performs printing on the <i>background</i>.
2182      * The following pattern might be used for <i>background</i>
2183      * printing:
2184      * <pre>
2185      *     FutureTask&lt;Boolean&gt; future =
2186      *         new FutureTask&lt;Boolean&gt;(
2187      *             new Callable&lt;Boolean&gt;() {


2405      * ensure that the {@code document} is not mutated while this {@code Printable}
2406      * is used. Printing behavior is undefined when the {@code document} is
2407      * mutated during printing.
2408      *
2409      * <p>
2410      * Page header and footer text can be added to the output by providing
2411      * {@code MessageFormat} arguments. The printing code requests
2412      * {@code Strings} from the formats, providing a single item which may be
2413      * included in the formatted string: an {@code Integer} representing the
2414      * current page number.
2415      *
2416      * <p>
2417      * The returned {@code Printable} when printed, formats the
2418      * document content appropriately for the page size. For correct
2419      * line wrapping the {@code imageable width} of all pages must be the
2420      * same. See {@link java.awt.print.PageFormat#getImageableWidth}.
2421      *
2422      * <p>
2423      * This method is thread-safe, although most Swing methods are not. Please
2424      * see <A
2425      * HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">
2426      * Concurrency in Swing</A> for more information.
2427      *
2428      * <p>
2429      * The returned {@code Printable} can be printed on any thread.
2430      *
2431      * <p>
2432      * This implementation returned {@code Printable} performs all painting on
2433      * the <i>Event Dispatch Thread</i>, regardless of what thread it is
2434      * used on.
2435      *
2436      * @param headerFormat the text, in {@code MessageFormat}, to be
2437      *        used as the header, or {@code null} for no header
2438      * @param footerFormat the text, in {@code MessageFormat}, to be
2439      *        used as the footer, or {@code null} for no footer
2440      * @return a {@code Printable} for use in printing content of this
2441      *         {@code JTextComponent}
2442      *
2443      *
2444      * @see java.awt.print.Printable
2445      * @see java.awt.print.PageFormat




  65 
  66 import javax.print.attribute.*;
  67 
  68 import sun.awt.AppContext;
  69 
  70 
  71 import sun.swing.PrintingStatus;
  72 import sun.swing.SwingUtilities2;
  73 import sun.swing.text.TextComponentPrintable;
  74 import sun.swing.SwingAccessor;
  75 
  76 /**
  77  * <code>JTextComponent</code> is the base class for swing text
  78  * components.  It tries to be compatible with the
  79  * <code>java.awt.TextComponent</code> class
  80  * where it can reasonably do so.  Also provided are other services
  81  * for additional flexibility (beyond the pluggable UI and bean
  82  * support).
  83  * You can find information on how to use the functionality
  84  * this class provides in
  85  * <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html">General Rules for Using Text Components</a>,
  86  * a section in <em>The Java Tutorial.</em>
  87  *
  88  * <dl>
  89  * <dt><b><font size=+1>Caret Changes</font></b>
  90  * <dd>
  91  * The caret is a pluggable object in swing text components.
  92  * Notification of changes to the caret position and the selection
  93  * are sent to implementations of the <code>CaretListener</code>
  94  * interface that have been registered with the text component.
  95  * The UI will install a default caret unless a customized caret
  96  * has been set. <br>
  97  * By default the caret tracks all the document changes
  98  * performed on the Event Dispatching Thread and updates it's position
  99  * accordingly if an insertion occurs before or at the caret position
 100  * or a removal occurs before the caret position. <code>DefaultCaret</code>
 101  * tries to make itself visible which may lead to scrolling
 102  * of a text component within <code>JScrollPane</code>. The default caret
 103  * behavior can be changed by the {@link DefaultCaret#setUpdatePolicy} method.
 104  * <br>
 105  * <b>Note</b>: Non-editable text components also have a caret though


2147      * being processed until printing is complete. It is only
2148      * recommended when printing from an application with no
2149      * visible GUI.
2150      *
2151      * <p>
2152      * Note: In <i>headless</i> mode, {@code showPrintDialog} and
2153      * {@code interactive} parameters are ignored and no dialogs are
2154      * shown.
2155      *
2156      * <p>
2157      * This method ensures the {@code document} is not mutated during printing.
2158      * To indicate it visually, {@code setEnabled(false)} is set for the
2159      * duration of printing.
2160      *
2161      * <p>
2162      * This method uses {@link #getPrintable} to render document content.
2163      *
2164      * <p>
2165      * This method is thread-safe, although most Swing methods are not. Please
2166      * see <A
2167      * HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">
2168      * Concurrency in Swing</A> for more information.
2169      *
2170      * <p>
2171      * <b>Sample Usage</b>. This code snippet shows a cross-platform print
2172      * dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode
2173      * unless the user cancels the dialog:
2174      *
2175      * <pre>
2176      * textComponent.print(new MessageFormat(&quot;My text component header&quot;),
2177      *     new MessageFormat(&quot;Footer. Page - {0}&quot;), true, null, null, true);
2178      * </pre>
2179      * <p>
2180      * Executing this code off the <i>Event Dispatch Thread</i>
2181      * performs printing on the <i>background</i>.
2182      * The following pattern might be used for <i>background</i>
2183      * printing:
2184      * <pre>
2185      *     FutureTask&lt;Boolean&gt; future =
2186      *         new FutureTask&lt;Boolean&gt;(
2187      *             new Callable&lt;Boolean&gt;() {


2405      * ensure that the {@code document} is not mutated while this {@code Printable}
2406      * is used. Printing behavior is undefined when the {@code document} is
2407      * mutated during printing.
2408      *
2409      * <p>
2410      * Page header and footer text can be added to the output by providing
2411      * {@code MessageFormat} arguments. The printing code requests
2412      * {@code Strings} from the formats, providing a single item which may be
2413      * included in the formatted string: an {@code Integer} representing the
2414      * current page number.
2415      *
2416      * <p>
2417      * The returned {@code Printable} when printed, formats the
2418      * document content appropriately for the page size. For correct
2419      * line wrapping the {@code imageable width} of all pages must be the
2420      * same. See {@link java.awt.print.PageFormat#getImageableWidth}.
2421      *
2422      * <p>
2423      * This method is thread-safe, although most Swing methods are not. Please
2424      * see <A
2425      * HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">
2426      * Concurrency in Swing</A> for more information.
2427      *
2428      * <p>
2429      * The returned {@code Printable} can be printed on any thread.
2430      *
2431      * <p>
2432      * This implementation returned {@code Printable} performs all painting on
2433      * the <i>Event Dispatch Thread</i>, regardless of what thread it is
2434      * used on.
2435      *
2436      * @param headerFormat the text, in {@code MessageFormat}, to be
2437      *        used as the header, or {@code null} for no header
2438      * @param footerFormat the text, in {@code MessageFormat}, to be
2439      *        used as the footer, or {@code null} for no footer
2440      * @return a {@code Printable} for use in printing content of this
2441      *         {@code JTextComponent}
2442      *
2443      *
2444      * @see java.awt.print.Printable
2445      * @see java.awt.print.PageFormat