United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6237146 Add ability to get baseline
6237146 : Add ability to get baseline

Details
Type:
Enhancement
Submit Date:
2005-03-07
Status:
Resolved
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-05-11
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:
6

Related Reports
Relates:
Relates:

Sub Tasks

Description
In order to create professional layouts we need the ability to get the baseline for components.  At a minimum baseline should be supported for: spinner, buttons, combobox, textfield (and it subclasses) and label.  We should also consider baseline for scrollpanes, tables, lists, trees and textpane/textarea/editorpane.

###@###.### 2005-03-07 23:31:59 GMT

                                    

Comments
EVALUATION

This resulted in adding API to Component to get the baseline and baseline resize behavior:

    /**
     * Enumeration of the common ways the baseline of a component can
     * change as the size changes.  The baseline resize behavior is
     * primarily for layout managers that need to know how the
     * position of the baseline changes as the component size changes.
     * In general the baseline resize behavior will be valid for sizes
     * greater than or equal to the minimum size (the actual minimum
     * size; not a developer specified minimum size).  For sizes
     * smaller than the minimum size the baseline may change in a way
     * other than the baseline resize behavior indicates.  Similarly,
     * as the size approaches <code>Integer.MAX_VALUE</code> and/or
     * <code>Short.MAX_VALUE</code> the baseline may change in a way
     * other than the baseline resize behavior indicates.
     *
     * @see #getBaselineResizeBehavior
     * @see #getBaseline(int,int)
     * @since 1.6
     */
    public enum BaselineResizeBehavior {
        /**
         * Indicates the baseline remains fixed relative to the
         * y-origin.  That is, <code>getBaseline</code> returns
         * the same value regardless of the height or width.  For example, a
         * <code>JLabel</code> containing non-empty text with a
         * vertical alignment of <code>TOP</code> should have a
         * baseline type of <code>CONSTANT_ASCENT</code>.
         */
        CONSTANT_ASCENT,
 
        /**
         * Indicates the baseline remains fixed relative to the height
         * and does not change as the width is varied.  That is, for
         * any height H the difference between H and
         * <code>getBaseline(w, H)</code> is the same.  For example, a
         * <code>JLabel</code> containing non-empty text with a
         * vertical alignment of <code>BOTTOM</code> should have a
         * baseline type of <code>CONSTANT_DESCENT</code>.
         */
        CONSTANT_DESCENT,
 
        /**
         * Indicates the baseline remains a fixed distance from
         * the center of the component.  That is, for any height H the
         * difference between <code>getBaseline(w, H)</code> and
         * <code>H / 2</code> is the same (plus or minus one depending upon
         * rounding error).
         * <p>
         * Because of possible rounding errors it is recommended
         * you ask for the baseline with two consecutive heights and use
         * the return value to determine if you need to pad calculations
         * by 1.  The following shows how to calculate the baseline for
         * any height:
         * <pre>
         *   Dimension preferredSize = component.getPreferredSize();
         *   int baseline = getBaseline(preferredSize.width,
         *                              preferredSize.height);
         *   int nextBaseline = getBaseline(preferredSize.width,
         *                                  preferredSize.height + 1);
         *   // Amount to add to height when calculating where baseline
         *   // lands for a particular height:
         *   int padding = 0;
         *   // Where the baseline is relative to the mid point
         *   int baselineOffset = baseline - height / 2;
         *   if (preferredSize.height % 2 == 0 &amp;&amp;
         *       baseline != nextBaseline) {
         *       padding = 1;
         *   }
         *   else if (preferredSize.height % 2 == 1 &amp;&amp;
         *            baseline == nextBaseline) {
         *       baselineOffset--;
         *       padding = 1;
         *   }
         *   // The following calculates where the baseline lands for
         *   // the height z:
         *   int calculatedBaseline = (z + padding) / 2 + baselineOffset;
         * </pre>
         */
        CENTER_OFFSET,
 
        /**
         * Indicates the baseline resize behavior can not be expressed using
         * any of the other constants.  This may also indicate the baseline
         * varies with the width of the component.  This is also returned
         * by components that do not have a baseline.
         */
        OTHER
    }
New methods in java.awt.Component:
    /**
     * Returns the baseline.  The baseline is measured from the top of
     * the component.  This method is primarily meant for
     * <code>LayoutManager</code>s to align components along their
     * baseline.  A return value less than 0 indicates this component
     * does not have a reasonable baseline and that
     * <code>LayoutManager</code>s should not align this component on
     * its baseline.
     * <p>
     * The default implementation returns -1.  Subclasses that support
     * baseline should override appropriately.  If a value &gt;= 0 is
     * returned, than the component has a valid baseline for any
     * size &gt;= the minimum size and <code>getBaselineResizeBehavior</code>
     * can be used to determine how the baseline changes with size.
     *
     * @param width the width to get the baseline for
     * @param height the height to get the baseline for
     * @return the baseline or &lt; 0 indicating there is no reasonable
     *         baseline
     * @throws IllegalArgumentException if width or height is &lt; 0
     * @see #getBaselineResizeBehavior
     * @see java.awt.FontMetrics
     * @since 1.6
     */
    public int getBaseline(int width, int height);
 
    /**
     * Returns an enum indicating how the baseline of the component
     * changes as the size changes.  This method is primarily meant for
     * layout managers and GUI builders.
     * <p>
     * The default implementation returns
     * <code>BaselineResizeBehavior.OTHER</code>.  Subclasses that have a
     * baseline should override appropriately.  Subclasses should
     * never return <code>null</code>; if the baseline can not be
     * calculated return <code>BaselineResizeBehavior.OTHER</code>.  Callers
     * should first ask for the baseline using
     * <code>getBaseline</code> and if a value &gt;= 0 is returned use
     * this method.  It is acceptable for this method to return a
     * value other than <code>BaselineResizeBehavior.OTHER</code> even if
     * <code>getBaseline</code> returns a value less than 0.
     *
     * @return an enum indicating how the baseline changes as the component
     *         size changes
     * @see #getBaseline(int, int)
     * @since 1.6
     */
    public BaselineResizeBehavior getBaselineResizeBehavior();
 
JComponent will override this and forward to the ComponentUI.
Border will also get similar methods.

The following will override getBaseline:
javax.swing.plaf.basic.BasicButtonUI
javax.swing.plaf.basic.BasicComboBoxUI
javax.swing.plaf.basic.BasicLabelUI
javax.swing.plaf.basic.BasicListUI
javax.swing.plaf.basic.BasicPanelUI
javax.swing.plaf.basic.BasicProgressBarUI
javax.swing.plaf.basic.BasicScrollPaneUI
javax.swing.plaf.basic.BasicSliderUI
javax.swing.plaf.basic.BasicSpinnerUI
javax.swing.plaf.basic.BasicTabbedPaneUI
javax.swing.plaf.basic.BasicTableUI
javax.swing.plaf.basic.BasicTableHeaderUI
javax.swing.plaf.basic.BasicTextAreaUI
javax.swing.plaf.basic.BasicTextFieldUI
javax.swing.plaf.basic.BasicTreeUI
javax.swing.plaf.metal.MetalComboBoxUI
com.sun.java.swing.plaf.windows.WindowsProgressBarUI

and getBaselineResizeBehavior:
javax.swing.plaf.basic.BasicButtonUI
javax.swing.plaf.basic.BasicComboBoxUI
javax.swing.plaf.basic.BasicLabelUI
javax.swing.plaf.basic.BasicListUI
javax.swing.plaf.basic.BasicPanelUI
javax.swing.plaf.basic.BasicProgressBarUI
javax.swing.plaf.basic.BasicScrollPaneUI
javax.swing.plaf.basic.BasicSliderUI
javax.swing.plaf.basic.BasicSpinnerUI
javax.swing.plaf.basic.BasicTabbedPaneUI
javax.swing.plaf.basic.BasicTableUI
javax.swing.plaf.basic.BasicTableHeaderUI
javax.swing.plaf.basic.BasicTextAreaUI
javax.swing.plaf.basic.BasicTextFieldUI
javax.swing.plaf.basic.BasicTreeUI

###@###.### 2005-05-06 17:47:25 GMT
                                     
2005-05-06



Hardware and Software, Engineered to Work Together