Example usage for java.awt Container getInsets

List of usage examples for java.awt Container getInsets

Introduction

In this page you can find the example usage for java.awt Container getInsets.

Prototype

public Insets getInsets() 

Source Link

Document

Determines the insets of this container, which indicate the size of the container's border.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTextArea cmp = new JTextArea();
    String str = "a";
    for (int i = 0; i < 20; i++) {
        cmp.append(str + str + "\n");
    }/*from w  w w.j  av a 2  s .c o m*/
    JScrollPane scrollPane = new JScrollPane(cmp);
    scrollPane.setComponentZOrder(scrollPane.getVerticalScrollBar(), 0);
    scrollPane.setComponentZOrder(scrollPane.getViewport(), 1);
    scrollPane.getVerticalScrollBar().setOpaque(false);

    scrollPane.setLayout(new ScrollPaneLayout() {
        @Override
        public void layoutContainer(Container parent) {
            JScrollPane scrollPane = (JScrollPane) parent;

            Rectangle availR = scrollPane.getBounds();
            availR.x = availR.y = 0;

            Insets parentInsets = parent.getInsets();
            availR.x = parentInsets.left;
            availR.y = parentInsets.top;
            availR.width -= parentInsets.left + parentInsets.right;
            availR.height -= parentInsets.top + parentInsets.bottom;

            Rectangle vsbR = new Rectangle();
            vsbR.width = 12;
            vsbR.height = availR.height;
            vsbR.x = availR.x + availR.width - vsbR.width;
            vsbR.y = availR.y;

            if (viewport != null) {
                viewport.setBounds(availR);
            }
            if (vsb != null) {
                vsb.setVisible(true);
                vsb.setBounds(vsbR);
            }
        }
    });
    scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollPane);
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame.//from  ww  w .  j a  va2 s  . c om
 * <p>
 * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
 *
 * @param e
 *          the event that caused the display of the tool tip
 * @param c
 *          the parent component of the tool tip
 * @param frame
 *          a component in which the tool tip has to fit (usually the surrounding window of "c")
 * @return
 */
public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
    JToolTip tip = new JToolTip();
    tip.setTipText(c.getToolTipText(e));
    Dimension tipSize = tip.getPreferredSize();
    // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
    Rectangle frameR = frame.getBounds();
    if (frame instanceof Container) {
        Container container = (Container) frame;
        Insets insets = container.getInsets();
        frameR.x += insets.left;
        frameR.y += insets.top;
        frameR.width -= (insets.left + insets.right);
        frameR.height -= (insets.top + insets.bottom);
    }
    frameR.x += 5;
    frameR.y += 5;
    frameR.width -= 10;
    frameR.height -= 10;
    // Initial try for the tool tip's position
    Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
            tipSize.width, tipSize.height);
    // Check if it fits within the frame
    Rectangle intersection = frameR.intersection(r);
    if (r.equals(intersection)) {
        // Tool tip is fully visible within the frame --> use default behaviour
        //
        // Note: The implementation of ToolTipManager.showTipWindow() is not always
        // correct in dual screen mode. The tool tip is _always_ put on that screen,
        // where the most part of the frame lies upon, even if we return coordinates
        // that clearly belong to the other screen. Unfortunately we cannot change
        // that behavior... (bsh 2010-11-24)
        return null;
    }
    // Otherwise, move the tool tip
    int correction = 0;
    if (r.height == intersection.height) {
        // Height is okay, just move left. To make it look better, position the
        // tip 5px below the component.
        r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                tipSize.height);
        correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
    } else {
        // The height does not fit. Position the tool tip above the component.
        r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                tipSize.width, tipSize.height);
    }
    // Adjust to frame bounds
    intersection = frameR.intersection(r);
    intersection.x -= (r.width - intersection.width);
    intersection.y -= (r.height - intersection.height);
    // Return value is expected to be relative to the component's position
    return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
            (-c.getLocationOnScreen().y) + intersection.y);
}

From source file:AbsoluteLayoutDemo.java

public static void addComponentsToPane(Container pane) {
    pane.setLayout(null);/*from w  ww .j a  v a2  s.c  om*/

    JButton b1 = new JButton("one");
    JButton b2 = new JButton("two");
    JButton b3 = new JButton("three");

    pane.add(b1);
    pane.add(b2);
    pane.add(b3);

    Insets insets = pane.getInsets();
    Dimension size = b1.getPreferredSize();
    b1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height);
    size = b2.getPreferredSize();
    b2.setBounds(55 + insets.left, 40 + insets.top, size.width, size.height);
    size = b3.getPreferredSize();
    b3.setBounds(150 + insets.left, 15 + insets.top, size.width + 50, size.height + 20);
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;
    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;

    setSizes(parent);/*from ww  w. j av a2  s .c o m*/
    int centerX = insets.left + containerWidth / 2;
    int centerY = insets.top + containerHeight / 2;

    int comCount = parent.getComponentCount();
    for (int i = 0; i < comCount; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension size = c.getPreferredSize();
            double angle = 2 * Math.PI * i / comCount;
            int x = centerX + (int) (Math.cos(angle) * xradius);
            int y = centerY + (int) (Math.sin(angle) * yradius);

            c.setBounds(x - size.width / 2, y - size.height / 2, size.width, size.height);
        }
    }
}

From source file:WrapperLayout.java

public Dimension preferredLayoutSize(Container arg0) {
    java.awt.Insets insets = arg0.getInsets();
    int count = arg0.getComponentCount();
    if (count > 0) {
        Dimension d = arg0.getComponent(0).getPreferredSize();
        return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
    } else {/*from ww w.j a va  2 s  .c om*/
        return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
    }
}

From source file:WrapperLayout.java

public Dimension minimumLayoutSize(Container arg0) {
    java.awt.Insets insets = arg0.getInsets();
    int count = arg0.getComponentCount();
    if (count > 0) {
        Dimension d = arg0.getComponent(0).getMinimumSize();
        return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
    } else {/*from w  w  w. ja  v a 2 s. co  m*/
        return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
    }
}

From source file:CircleLayoutTest.java

public Dimension preferredLayoutSize(Container parent) {
    setSizes(parent);//from ww  w. j a va2s  .co m
    Insets insets = parent.getInsets();
    int width = preferredWidth + insets.left + insets.right;
    int height = preferredHeight + insets.top + insets.bottom;
    return new Dimension(width, height);
}

From source file:CircleLayoutTest.java

public Dimension minimumLayoutSize(Container parent) {
    setSizes(parent);//www .ja  v a2  s .  c  o m
    Insets insets = parent.getInsets();
    int width = minWidth + insets.left + insets.right;
    int height = minHeight + insets.top + insets.bottom;
    return new Dimension(width, height);
}

From source file:CircleLayoutTest.java

public Dimension preferredLayoutSize(Container parent) {
    Dimension dim = new Dimension(0, 0);
    setSizes(parent);/*from  w  w w .  j av a  2 s . c o m*/
    Insets insets = parent.getInsets();
    dim.width = preferredWidth + insets.left + insets.right;
    dim.height = preferredHeight + insets.top + insets.bottom;
    return dim;
}

From source file:com.equitysoft.cellspark.ProportionalLayout.java

/**
 * Lays out the container./*from ww w  . j  av a2 s .  c om*/
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        //do layout
        int cnt = 0;
        int totalwid = pd.width - insets.left - insets.right;
        int x = insets.left;
        int cumwid = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int wid = proportions[i] * totalwid / total;
            c.setBounds(x, insets.top, wid, pd.height - insets.bottom - insets.top);
            x += wid;
            cnt++;
            if (cnt == num)
                break;
        }
    }
}