Example usage for javax.swing JLabel getHorizontalAlignment

List of usage examples for javax.swing JLabel getHorizontalAlignment

Introduction

In this page you can find the example usage for javax.swing JLabel getHorizontalAlignment.

Prototype

public int getHorizontalAlignment() 

Source Link

Document

Returns the alignment of the label's contents along the X axis.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel userPanel = new JPanel(new BorderLayout());
    JLabel userLabel = new JLabel("Username: ");
    userLabel.setDisplayedMnemonic(KeyEvent.VK_U);
    JTextField userTextField = new JTextField();
    userLabel.setLabelFor(userTextField);
    userPanel.add(userLabel, BorderLayout.WEST);
    userPanel.add(userTextField, BorderLayout.CENTER);

    System.out.println(userLabel.getHorizontalAlignment());

    JPanel passPanel = new JPanel(new BorderLayout());
    JLabel passLabel = new JLabel("Password: ");
    passLabel.setDisplayedMnemonic('p');
    JPasswordField passTextField = new JPasswordField();
    passLabel.setLabelFor(passTextField);
    passPanel.add(passLabel, BorderLayout.WEST);
    passPanel.add(passTextField, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(userPanel, BorderLayout.NORTH);
    panel.add(passPanel, BorderLayout.SOUTH);
    frame.add(panel, BorderLayout.NORTH);

    frame.setSize(250, 150);/*from  w w w  .j a v  a  2 s .c om*/
    frame.setVisible(true);
}

From source file:Main.java

public static String layoutLabel(JLabel label, Icon icon, Rectangle viewRect, Rectangle iconRect,
        Rectangle textRect) {//w ww  . j a  va  2s .  co m
    setViewBounds(label, viewRect);

    return layoutComponent(label, label.getText(), icon, label.getIconTextGap(), label.getVerticalAlignment(),
            label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(),
            viewRect, iconRect, textRect);
}

From source file:PaintUtils.java

/**
 * Returns the bounds that the text of a label will be drawn into.
 * Takes into account the current font metrics.
 *//* www.  j  a v a 2  s  .c om*/
public static Rectangle getTextBounds(Graphics g, JLabel label) {
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D r2d = fm.getStringBounds(label.getText(), g);
    Rectangle rect = r2d.getBounds();
    int xOffset = 0;
    switch (label.getHorizontalAlignment()) {
    case SwingConstants.RIGHT:
    case SwingConstants.TRAILING:
        xOffset = label.getBounds().width - rect.width;
        break;
    case SwingConstants.CENTER:
        xOffset = (label.getBounds().width - rect.width) / 2;
        break;
    default:
    case SwingConstants.LEFT:
    case SwingConstants.LEADING:
        xOffset = 0;
        break;
    }
    int yOffset = 0;
    switch (label.getVerticalAlignment()) {
    case SwingConstants.TOP:
        yOffset = 0;
        break;
    case SwingConstants.CENTER:
        yOffset = (label.getBounds().height - rect.height) / 2;
        break;
    case SwingConstants.BOTTOM:
        yOffset = label.getBounds().height - rect.height;
        break;
    }
    return new Rectangle(xOffset, yOffset, rect.width, rect.height);
}

From source file:Main.java

public static Rectangle getTextRectangle(JLabel label) {

    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return null;
    }//  w  w  w. j  av a 2s . c o m

    Rectangle paintIconR = new Rectangle();
    Rectangle paintTextR = new Rectangle();
    Rectangle paintViewR = new Rectangle();
    Insets paintViewInsets = new Insets(0, 0, 0, 0);

    paintViewInsets = label.getInsets(paintViewInsets);
    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;
    paintViewR.width = label.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.height = label.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    Graphics g = label.getGraphics();
    if (g == null) {
        return null;
    }
    String clippedText = SwingUtilities.layoutCompoundLabel(label, g.getFontMetrics(), text, icon,
            label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(),
            label.getHorizontalTextPosition(), paintViewR, paintIconR, paintTextR, label.getIconTextGap());

    return paintTextR;
}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

/**
 * //w ww .  j av a  2 s  .co m
 * <i>Description:</i> simple method that creates single/multi line label
 * with specified label width based on a source label. For more advanced
 * label attributes, it's the developer's responsibility to set them back.
 * 
 * @param source
 * @param wrap
 * @param maxWidth
 * @return
 */
public static JLabel deriveLabel(JLabel source, final boolean wrap, final int maxWidth) {
    JXLabel label = new JXLabel(source.getText(), source.getIcon(), source.getHorizontalAlignment()) {
        private static final long serialVersionUID = -4816144910055350011L;

        private Font cachedFont;

        private String chahedRawText, chahedText;

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.JLabel#getText()
         */
        @Override
        public String getText() {
            String text = super.getText();
            if (wrap || maxWidth <= 0 || text == null || text.isEmpty()) {
                return text;
            }

            if (getFont().equals(cachedFont) && text.equals(chahedRawText)) {
                return chahedText;
            }

            chahedRawText = text;
            cachedFont = getFont();
            FontMetrics fm = getFontMetrics(cachedFont);
            char[] chars = text.toCharArray();
            int width = fm.charsWidth(chars, 0, chars.length);
            if (width < maxWidth) {
                chahedText = text;
            } else {
                width += fm.charWidth('.') * 3;
                int pos = chars.length - 1;
                for (; pos >= 0 && width > maxWidth; pos--) {
                    width -= fm.charWidth(chars[pos]);
                }
                chahedText = new String(chars, 0, pos) + "...";
                if (getToolTipText() == null) {
                    setToolTipText(text);
                }
            }

            return chahedText;
        }

    };
    if (wrap) {
        label.setLineWrap(true);
    }
    if (maxWidth > 0) {
        label.setMaxLineSpan(maxWidth);
    }
    label.setEnabled(source.isEnabled());
    label.setForeground(source.getForeground());
    label.setOpaque(source.isOpaque());
    label.setBackground(source.getBackground());
    label.setFont(source.getFont());
    label.setBorder(source.getBorder());
    label.setToolTipText(source.getToolTipText());
    return label;
}