Example usage for javax.swing JComponent getInsets

List of usage examples for javax.swing JComponent getInsets

Introduction

In this page you can find the example usage for javax.swing JComponent getInsets.

Prototype

@BeanProperty(expert = true)
public Insets getInsets() 

Source Link

Document

If a border has been set on this component, returns the border's insets; otherwise calls super.getInsets.

Usage

From source file:Main.java

public static final void shortenHorizontalInsets(JComponent c) {
    Insets i = c.getInsets();
    int top = i.top, bottom = i.bottom, left = i.left, right = i.right;
    int min = top > bottom ? bottom : top;
    if (left > min)
        i.left = min;//from  w ww .  j  a v a 2  s.  co  m
    if (right > min)
        i.right = min;
    //c.setInsets(i);
}

From source file:Main.java

public static void copyFontAndMargins(final JTextArea target, final JComponent source) {
    final Insets insets = source.getInsets();
    target.setFont(source.getFont());/*from w  w w.j  a v  a2s  .  co m*/
    target.setMargin(insets);
}

From source file:Main.java

/**
 * Sets where a component should be painted. It uses the 
 * {@code JComponent}'s width and height and excludes the {@code Insets}.
 * //from  w w  w .j  ava 2  s . c om
 * @param c the component
 * @param viewRect the bounds for painting the component
 */
public static void setViewBounds(JComponent c, Rectangle viewRect) {
    Insets i = c.getInsets();

    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = c.getWidth() - (i.right + viewRect.x);
    viewRect.height = c.getHeight() - (i.bottom + viewRect.y);
}

From source file:Main.java

/**
 * If icon is an instance of {@link ImageIcon}, 
 * returns an icon scaled to the height of the component.
 * Else return the icon given./*ww w . j ava  2s .  c  o  m*/
 */
public static Icon scaleToHeight(JComponent c, Icon icon) {

    Icon sizedIcon = icon;
    if (icon instanceof ImageIcon) {
        int h = c.getPreferredSize().height - c.getInsets().top - c.getInsets().bottom;
        sizedIcon = new ImageIcon(((ImageIcon) icon).getImage().getScaledInstance(-1, h, Image.SCALE_SMOOTH));
    }
    return sizedIcon;
}

From source file:Main.java

public static void centerHorizontally(JComponent c, int from, int to, boolean withInsets) {
    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;/*from ww  w  .  j  a  v  a 2 s.  c  om*/
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    Rectangle visible = c.getVisibleRect();

    visible.x = from - (visible.width + from - to) / 2;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void centerVertically(JComponent c, int from, int to, boolean withInsets) {
    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;/*  w  ww . ja v  a 2  s . co m*/
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    Rectangle visible = c.getVisibleRect();

    visible.y = from - (visible.height + from - to) / 2;

    if (visible.y < bounds.y)
        visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
        visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static Rectangle calculatePaintingArea(JComponent c) {
    Dimension d = c.getSize();/*  w ww.  ja v  a2 s  .co  m*/
    Rectangle result = new Rectangle();
    result.x = 0;
    result.y = 0;
    result.width = d.width;
    result.height = d.height;
    Insets insets = c.getInsets();
    if (insets != null) {
        result.width = d.width - (insets.left + insets.right);
        result.height = d.height - (insets.top + insets.bottom);
        result.x = insets.left;
        result.y = insets.top;
    }
    return result;
}

From source file:Main.java

public static void center(JComponent c, Rectangle r, boolean withInsets) {
    Rectangle visible = c.getVisibleRect();

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;/*  w ww  . ja v  a 2 s  . c  o  m*/
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    if (visible.y < bounds.y)
        visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
        visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static Dimension getPreferredSize(JComponent c, String text, Icon icon, int iconTextGap,
        int verticalAlignment, int horizontalAlignment, int verticalTextAlignment, int horizontalTextAlignment,
        Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
    viewRect.x = viewRect.y = 0;/*from w w w .j  av a  2 s  .c  o m*/
    viewRect.width = viewRect.height = Short.MAX_VALUE;

    layoutComponent(c, text, icon, iconTextGap, verticalAlignment, horizontalAlignment, verticalTextAlignment,
            horizontalTextAlignment, viewRect, iconRect, textRect);

    Rectangle size = iconRect.union(textRect);
    if (c.getInsets() != null) {
        addInsets(size, c.getInsets());
    }

    return new Dimension(size.width, size.height);
}

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

public void paint(Graphics g, JComponent c) {

    UIUtils.antialias(g);//  www . j  a v  a 2  s .c om

    Font font = c.getFont();
    FontMetrics metrics = c.getFontMetrics(font);

    Dimension size = c.getSize();
    if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, size.width + 20, size.height);
    }

    JToolTip toolTip = (JToolTip) c;
    String tipText = getTipText(toolTip);

    if (!MiscUtils.isNull(tipText)) {

        Insets insets = c.getInsets();
        Rectangle paintTextR = new Rectangle(insets.left, insets.top, size.width - (insets.left + insets.right),
                size.height - (insets.top + insets.bottom));

        Color foreground = c.getForeground();
        g.setColor(foreground);
        g.setFont(font);

        g.drawString(tipText, paintTextR.x + 3, paintTextR.y + metrics.getAscent());

        String acceleratorString = getAcceleratorStringForRender(toolTip);
        if (StringUtils.isNotBlank(acceleratorString)) {

            Font acceleratorFont = font.deriveFont(font.getSize() - 1f);
            g.setFont(acceleratorFont);
            g.setColor(GUIUtils.getSlightlyBrighter(foreground, 2.0f));

            g.drawString(acceleratorString, paintTextR.x + 6 + metrics.stringWidth(tipText),
                    paintTextR.y + metrics.getAscent());
        }

    }

}