Example usage for javax.swing JLabel getBounds

List of usage examples for javax.swing JLabel getBounds

Introduction

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

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

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.
 *//* w  w  w  .ja  va2 s.co m*/
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:es.emergya.ui.base.plugins.PluggableJTabbedPane.java

private void addFloatingButtons() {
    JButton salir = new JButton();
    salir.addActionListener(new ExitHandler());

    Icon icon = LogicConstants.getIcon("header_button_exit");
    salir.setIcon(icon);/*  w  w  w  . j a  v a 2 s .com*/
    if (icon != null)
        if (min_height < icon.getIconHeight())
            min_height = icon.getIconHeight();

    // Aadimos el botn de Salir
    salir.setBounds(this.getWidth() - icon.getIconWidth() - 2, 2, icon.getIconWidth(), icon.getIconHeight());
    salir.setBorderPainted(false);
    PluggableJTabbedPane.this.salir = salir.getBounds();

    // Logo de la empresa
    JLabel logo = new JLabel();
    icon = LogicConstants.getIcon("header_logo_cliente");
    if (min_height < icon.getIconHeight())
        min_height = icon.getIconHeight();

    logo.setIcon(icon);
    logo.setBounds(salir.getBounds().x - icon.getIconWidth() - 2, 2, icon.getIconWidth(), icon.getIconHeight());

    JLabel companyLogo = new JLabel();
    icon = LogicConstants.getIcon("header_logo");
    if (icon != null)
        if (min_height < icon.getIconHeight())
            min_height = icon.getIconHeight();
    companyLogo.setIcon(icon);
    companyLogo.setBounds(logo.getBounds().x - icon.getIconWidth(), 2, icon.getIconWidth(),
            icon.getIconHeight());

    botones_flotantes = new ArrayList<JComponent>();
    addFloatingButton(companyLogo);
    addFloatingButton(logo);
    addFloatingButton(salir);

    repaint();
}