Example usage for javax.swing JComponent getSize

List of usage examples for javax.swing JComponent getSize

Introduction

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

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:Main.java

public static void printNow(int time, JComponent obj) {
    Dimension dim = obj.getSize();
    obj.validate();//ww w  .  j  a  v a  2  s  .c o m
    obj.paintImmediately(0, 0, dim.width, dim.height);
    try {
        Thread.sleep(time);
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static Rectangle calculatePaintingArea(JComponent c) {
    Dimension d = c.getSize();
    Rectangle result = new Rectangle();
    result.x = 0;//ww w  .  j a  va2  s  . c  om
    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

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame./*from  w w w  .  j  a  v a  2s . co m*/
 * <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:com.croer.javaorange.diviner.SimpleOrangeDiviner.java

@Override
public void show(JComponent jComponent) {
    this.jComponent = jComponent;
    System.out.println(configuration.getString(jComponent.getName()));
    Point location = jComponent.getLocationOnScreen();
    Dimension size = jComponent.getSize();
    Point xy = new Point(location.x, location.y + size.height);

    setLocation(xy);//www  . j av a2 s  .c om
    //        EventQueue.invokeLater(new Runnable() {
    //
    //            public void run() {
    //                jTextField3.requestFocusInWindow();
    //            }
    //        });
    pack();
    setVisible(true);
}

From source file:org.drugis.addis.gui.builder.SMAAView.java

protected JButton createSaveImageButton(final JComponent chart) {
    JButton button = new JButton("Save Image");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ImageExporter.writeImage(d_mainWindow, chart, (int) chart.getSize().getWidth(),
                    (int) chart.getSize().getHeight());
        }//from   w  w  w  . j  a v  a  2 s  .co m
    });
    return button;
}

From source file:eu.delving.sip.frames.AllFrames.java

public Swing prepareForMapping(final JComponent component) {
    return new Swing() {
        @Override/*from  ww  w.  j av  a  2 s.c om*/
        public void run() {
            viewSelector.selectView(component.getSize().width > 1600 ? DECADENT_DISPLAY : QUICK_MAPPING);
        }
    };
}

From source file:eu.delving.sip.frames.AllFrames.java

public Swing prepareForInvestigation(final JComponent component) {
    return new Swing() {
        @Override/*from w w w  .ja v a  2 s.c  om*/
        public void run() {
            viewSelector.selectView(component.getSize().width > 1600 ? DECADENT_DISPLAY : BIG_PICTURE);
        }
    };
}

From source file:net.lmxm.ute.gui.validation.AbstractInputValidator.java

/**
 * Display messages dialog./*  w ww.  j  a  va2s .  co  m*/
 * 
 * @param component the component
 * @param messages the messages
 */
private void displayMessagesDialog(final JComponent component, final List<String> messages) {
    final JDialog dialog = getMessagesDialog();

    // Load dialog with messages.
    getMessagesLabel().setText(StringUtils.join(messages, "\n"));

    // Relocate dialog relative to the input component
    dialog.setSize(0, 0);
    dialog.setLocationRelativeTo(component);
    final Point location = dialog.getLocation();
    final Dimension componentSize = component.getSize();
    dialog.setLocation(location.x - (int) componentSize.getWidth() / 2,
            location.y + (int) componentSize.getHeight() / 2);
    dialog.pack();
    dialog.setVisible(true);
}

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

public void paint(Graphics g, JComponent c) {

    UIUtils.antialias(g);/*from   w w  w. j  a  v a  2  s .  c  o  m*/

    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());
        }

    }

}