< prev index next >

src/java.desktop/share/classes/sun/swing/SwingUtilities2.java

Print this page




 341     }
 342 
 343 
 344     /**
 345      * Returns the FontMetrics for the specified Font.
 346      * This method is used when a Graphics is available, typically when
 347      * painting.  If a Graphics is not available the JComponent method of
 348      * the same name should be used.
 349      * <p>
 350      * Callers should pass in a non-null JComonent, the exception
 351      * to this is if a JComponent is not readily available at the time of
 352      * painting.
 353      * <p>
 354      * This does not necessarily return the FontMetrics from the
 355      * Graphics.
 356      *
 357      * @param c JComponent requesting FontMetrics, may be null
 358      * @param c Graphics Graphics
 359      * @param font Font to get FontMetrics for
 360      */

 361     public static FontMetrics getFontMetrics(JComponent c, Graphics g,
 362                                              Font font) {
 363         if (c != null) {
 364             // Note: We assume that we're using the FontMetrics
 365             // from the widget to layout out text, otherwise we can get
 366             // mismatches when printing.
 367             return c.getFontMetrics(font);
 368         }
 369         return Toolkit.getDefaultToolkit().getFontMetrics(font);
 370     }
 371 
 372 
 373     /**
 374      * Returns the width of the passed in String.
 375      * If the passed String is <code>null</code>, returns zero.
 376      *
 377      * @param c JComponent that will display the string, may be null
 378      * @param fm FontMetrics used to measure the String width
 379      * @param string String to get the width of
 380      */


1689                 Component comp = policy.getComponentAfter(rootAncestor, container);
1690 
1691                 if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) {
1692                     comp.requestFocus();
1693                     return comp;
1694                 }
1695             }
1696         }
1697         if (component.isFocusable()) {
1698             component.requestFocus();
1699             return component;
1700         }
1701         return null;
1702     }
1703 
1704     /**
1705      * Change focus to the visible component in {@code JTabbedPane}.
1706      * This is not a general-purpose method and is here only to permit
1707      * sharing code.
1708      */

1709     public static boolean tabbedPaneChangeFocusTo(Component comp) {
1710         if (comp != null) {
1711             if (comp.isFocusTraversable()) {
1712                 SwingUtilities2.compositeRequestFocus(comp);
1713                 return true;
1714             } else if (comp instanceof JComponent
1715                        && ((JComponent)comp).requestDefaultFocus()) {
1716 
1717                  return true;
1718             }
1719         }
1720 
1721         return false;
1722     }
1723 
1724     /**
1725      * Submits a value-returning task for execution on the EDT and
1726      * returns a Future representing the pending results of the task.
1727      *
1728      * @param task the task to submit




 341     }
 342 
 343 
 344     /**
 345      * Returns the FontMetrics for the specified Font.
 346      * This method is used when a Graphics is available, typically when
 347      * painting.  If a Graphics is not available the JComponent method of
 348      * the same name should be used.
 349      * <p>
 350      * Callers should pass in a non-null JComonent, the exception
 351      * to this is if a JComponent is not readily available at the time of
 352      * painting.
 353      * <p>
 354      * This does not necessarily return the FontMetrics from the
 355      * Graphics.
 356      *
 357      * @param c JComponent requesting FontMetrics, may be null
 358      * @param c Graphics Graphics
 359      * @param font Font to get FontMetrics for
 360      */
 361     @SuppressWarnings("deprecation")
 362     public static FontMetrics getFontMetrics(JComponent c, Graphics g,
 363                                              Font font) {
 364         if (c != null) {
 365             // Note: We assume that we're using the FontMetrics
 366             // from the widget to layout out text, otherwise we can get
 367             // mismatches when printing.
 368             return c.getFontMetrics(font);
 369         }
 370         return Toolkit.getDefaultToolkit().getFontMetrics(font);
 371     }
 372 
 373 
 374     /**
 375      * Returns the width of the passed in String.
 376      * If the passed String is <code>null</code>, returns zero.
 377      *
 378      * @param c JComponent that will display the string, may be null
 379      * @param fm FontMetrics used to measure the String width
 380      * @param string String to get the width of
 381      */


1690                 Component comp = policy.getComponentAfter(rootAncestor, container);
1691 
1692                 if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) {
1693                     comp.requestFocus();
1694                     return comp;
1695                 }
1696             }
1697         }
1698         if (component.isFocusable()) {
1699             component.requestFocus();
1700             return component;
1701         }
1702         return null;
1703     }
1704 
1705     /**
1706      * Change focus to the visible component in {@code JTabbedPane}.
1707      * This is not a general-purpose method and is here only to permit
1708      * sharing code.
1709      */
1710     @SuppressWarnings("deprecation")
1711     public static boolean tabbedPaneChangeFocusTo(Component comp) {
1712         if (comp != null) {
1713             if (comp.isFocusTraversable()) {
1714                 SwingUtilities2.compositeRequestFocus(comp);
1715                 return true;
1716             } else if (comp instanceof JComponent
1717                        && ((JComponent)comp).requestDefaultFocus()) {
1718 
1719                  return true;
1720             }
1721         }
1722 
1723         return false;
1724     }
1725 
1726     /**
1727      * Submits a value-returning task for execution on the EDT and
1728      * returns a Future representing the pending results of the task.
1729      *
1730      * @param task the task to submit


< prev index next >