Example usage for javax.swing JComponent getBounds

List of usage examples for javax.swing JComponent getBounds

Introduction

In this page you can find the example usage for javax.swing JComponent 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:Main.java

public static void main(String[] args) {
    int ROWS = 100;
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JLabel("Thanks for helping out. Use tab to move around."));
    for (int i = 0; i < ROWS; i++) {
        JTextField field = new JTextField("" + i);
        field.setName("field#" + i);
        content.add(field);/*w  w  w. ja  va  2 s.  c  om*/
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
            new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (!(evt.getNewValue() instanceof JComponent)) {
                        return;
                    }
                    JViewport viewport = (JViewport) content.getParent();
                    JComponent focused = (JComponent) evt.getNewValue();
                    if (content.isAncestorOf(focused)) {
                        Rectangle rect = focused.getBounds();
                        Rectangle r2 = viewport.getVisibleRect();
                        content.scrollRectToVisible(
                                new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                    }
                }
            });
    JFrame window = new JFrame();
    window.setContentPane(new JScrollPane(content));
    window.setSize(200, 200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

From source file:OverlaySample.java

public static void main(String args[]) {

    ActionListener generalActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JComponent comp = (JComponent) actionEvent.getSource();
            System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds());
        }/* w  ww.j  a  va2  s .  co m*/
    };

    ActionListener sizingActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setupButtons(actionEvent.getActionCommand());
        }
    };

    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    Object settings[][] = { { "Small", new Dimension(25, 25), Color.white },
            { "Medium", new Dimension(50, 50), Color.gray },
            { "Large", new Dimension(100, 100), Color.black } };
    JButton buttons[] = { smallButton, mediumButton, largeButton };

    for (int i = 0, n = settings.length; i < n; i++) {
        JButton button = buttons[i];
        button.addActionListener(generalActionListener);
        button.setActionCommand((String) settings[i][0]);
        button.setMaximumSize((Dimension) settings[i][1]);
        button.setBackground((Color) settings[i][2]);
        panel.add(button);
    }

    setupButtons(SET_CENTRAL);

    JPanel actionPanel = new JPanel();
    actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment"));
    String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED };
    for (int i = 0, n = actionSettings.length; i < n; i++) {
        JButton button = new JButton(actionSettings[i]);
        button.addActionListener(sizingActionListener);
        actionPanel.add(button);
    }

    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(actionPanel, BorderLayout.SOUTH);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Forces an immediate repaint of a component and all of its children.
 *
 * @param component/*from  w  w w.  j av  a2s .  c o  m*/
 */
public static void paintImmediately(Component component) {

    // Paint the component
    if (component instanceof JComponent) {
        JComponent jcomponent = (JComponent) component;
        jcomponent.paintImmediately(jcomponent.getBounds());
    }

    // Recursively paint children
    if (component instanceof Container) {
        Container container = (Container) component;
        int numberOfChildren = container.getComponentCount();
        for (int i = 0; i < numberOfChildren; i++) {
            Component child = container.getComponent(i);
            paintImmediately(child);
        }
    }
}

From source file:Main.java

private static Rectangle calculateInnerArea(JComponent comp, int pixels) {
    Rectangle result = new Rectangle();
    Rectangle compRect = comp.getBounds();
    result.x = pixels;//from w w  w. ja va2  s .co  m
    result.y = pixels;
    result.height = compRect.height - (pixels * 2);
    result.width = compRect.width - (pixels * 2);
    return result;
}

From source file:Main.java

/**
 * Centers JComponent on screen/*  ww w  .ja va2  s .  c o  m*/
 *
 * @param target
 */
public static void centerComponent(final JComponent target) {
    final Rectangle screen = getScreenRect();
    final Rectangle frameSize = target.getBounds();
    final int x = (screen.width - frameSize.width) / 2;
    final int y = (screen.height - frameSize.height) / 2;
    target.setLocation(x, y);
}

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  w w w.  j a  v a2 s  . c o m*/
    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;//from  ww  w  .  j  a  v  a2s  . c o  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 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  w w  .  j ava 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:es.emergya.ui.base.plugins.PluggableJTabbedPane.java

@Override
public void paint(Graphics g) {
    super.paint(g);
    for (JComponent bf : botones_flotantes) {
        try {//from   w w w . j a v  a 2  s  . c o m
            g.translate(bf.getBounds().x, bf.getBounds().y);
            bf.paint(g);
            g.translate(-bf.getBounds().x, -bf.getBounds().y);
        } catch (Throwable t) {
        }
    }
}

From source file:com.haulmont.cuba.desktop.sys.DesktopToolTipManager.java

protected boolean isPointInComponent(Point point, JComponent component) {
    if (!component.isShowing())
        return false;

    Point componentLocation = component.getLocationOnScreen();
    Rectangle bounds = component.getBounds();
    return (((point.x >= componentLocation.x) && (point.x <= componentLocation.x + bounds.width))
            && (point.y >= componentLocation.y) && (point.y <= componentLocation.y + bounds.height));
}