Example usage for javax.swing SwingUtilities convertPointToScreen

List of usage examples for javax.swing SwingUtilities convertPointToScreen

Introduction

In this page you can find the example usage for javax.swing SwingUtilities convertPointToScreen.

Prototype

@SuppressWarnings("deprecation")
public static void convertPointToScreen(Point p, Component c) 

Source Link

Document

Convert a point from a component's coordinate system to screen coordinates.

Usage

From source file:pcgen.gui2.tools.Utility.java

/**
 * Centres the dialog over the component ensuring that the dialog will be
 * within the usable area of the screen (i.e. excluding native task bars,
 * menus bars etc)./*from   w w w.j  a  v a 2 s.c  om*/
 *
 * @param parent The component over which the dialog should be centred.
 * @param dialog The dialog to be positioned.
 */
public static void setComponentRelativeLocation(Component parent, Component dialog) {
    // First make sure it is not too big
    resizeComponentToScreen(dialog);

    // Get the maximum window size to account for taskbars etc
    Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Point centreOfParent = new Point(parent.getWidth() / 2, parent.getHeight() / 2);
    SwingUtilities.convertPointToScreen(centreOfParent, parent);
    // Default to centre of parent
    Point location = new Point(centreOfParent.x - (dialog.getWidth() / 2),
            centreOfParent.y - (dialog.getHeight() / 2));
    // Adjust so it fits on the screen
    if ((location.x + dialog.getWidth()) > (screenBounds.width + screenBounds.x)) {
        location.x -= (location.x + dialog.getWidth()) - (screenBounds.width + screenBounds.x);
    }
    if (location.x < screenBounds.x) {
        location.x = screenBounds.x;
    }
    if ((location.y + dialog.getHeight()) > (screenBounds.height + screenBounds.y)) {
        location.y -= (location.y + dialog.getHeight()) - (screenBounds.height + screenBounds.y);
    }
    if (location.y < screenBounds.y) {
        location.y = screenBounds.y;
    }
    dialog.setLocation(location);
}

From source file:picocash.Picocash.java

private JPanel initMainFrame() {
    this.accountPanel = new StandardAccountPanel();
    mainPanel = new JXPanel(new MigLayout("fill, insets 0 0 0 0", "[grow]", "[40!] 0 [31!] 0 [grow] 0 [20!]"));
    headerPanel = new HeaderPanel();
    modePanel = new ModePanel();
    accountContainer = new JXPanel(new MigLayout("fill, insets 0 0 0 0"));
    statisticPanel = new StatisticPanel();
    footerPanel = new FooterPanel();
    headerPanel.addMouseListener(new MouseAdapter() {

        @Override//  w  ww.j a  v a2s. c  o m
        public void mousePressed(MouseEvent arg0) {
            Point clickPoint = new Point(arg0.getPoint());
            SwingUtilities.convertPointToScreen(clickPoint, getMainFrame());
            dX = clickPoint.x - getMainFrame().getX();
            dY = clickPoint.y - getMainFrame().getY();
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (System.currentTimeMillis() - lastClickOn < DOUBLE_CLICK_TIME) {
                maximize();
            }
            lastClickOn = System.currentTimeMillis();
        }
    });

    headerPanel.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseDragged(MouseEvent arg0) {
            Point dragPoint = new Point(arg0.getPoint());
            SwingUtilities.convertPointToScreen(dragPoint, getMainFrame());
            getMainFrame().setLocation(dragPoint.x - dX, dragPoint.y - dY);
        }
    });

    modePanel.addModePanelSelectionListener(this);

    mainPanel.add(headerPanel, "aligny top, growx, h 40!, wrap");
    mainPanel.add(modePanel, "aligny top, growx, h 31!, wrap");

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, accountContainer, statisticPanel);
    splitPane.setDividerLocation(0.7d);
    JXPanel tmp = new JXPanel(new MigLayout("fill, insets 0 0 0 0"));
    tmp.add(splitPane, "grow");
    mainPanel.add(tmp, "growx, growy, wrap");

    mainPanel.add(footerPanel, "aligny bottom, growx, h 20!");

    return mainPanel;
}

From source file:savant.view.swing.GraphPane.java

public void tryPopup(Point p) {

    if (tracks == null) {
        return;/* w  w w  .j  a va  2 s.c o m*/
    }

    Point pt = new Point(p.x, p.y - getOffset());

    for (Track t : tracks) {
        Map<Record, Shape> map = t.getRenderer().searchPoint(pt);
        if (map != null) {
            /**
             * XXX: This line is here to get around what looks like a bug in
             * the 1.6.0_20 JVM for Snow Leopard which causes the
             * mouseExited events not to be triggered sometimes. We hide the
             * popup before showing another. This needs to be done exactly
             * here: after we know we have a new popup to show and before we
             * set currentOverRecord. Otherwise, it won't work.
             */
            PopupPanel.hidePopup();

            // Arbitrarily pick the first record in the map.  Most of the time, there will be only one.
            Record overRecord = map.keySet().iterator().next();

            Point p1 = (Point) p.clone();
            SwingUtilities.convertPointToScreen(p1, this);
            PopupPanel.showPopup(this, p1, t, overRecord);

            currentOverRecord = overRecord;
            currentOverShape = map.get(currentOverRecord);
            if (currentOverRecord instanceof ContinuousRecord) {
                currentOverShape = ContinuousTrackRenderer.continuousRecordToEllipse(this, currentOverRecord);
            }

            repaint();
            return;
        }
    }
    // Didn't get a hit on any track.
    currentOverShape = null;
    currentOverRecord = null;
}

From source file:unikn.dbis.univis.navigation.tree.VTree.java

/**
 * TODO: document me!!!// ww w .  j  a v a 2  s  .  c om
 *
 * @param p
 */
public void showPopupMenu(Point p) {

    // Remove all items from popup menu.
    popupMenu.removeAll();

    Object o = getLastSelectedPathComponent();

    if (o instanceof DefaultMutableTreeNode) {

        DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;

        Object userObject = node.getUserObject();

        if (userObject instanceof VDimension) {

            VDimension dimension = (VDimension) userObject;

            try {
                Point p2 = new Point(p);
                SwingUtilities.convertPointToScreen(p2, this);

                final FilterItemContainer container = createFilterContainer(dimension, p2);

                if (!container.isEmpty()) {
                    /*
                    JLabel header = new JLabel(MessageResolver.getMessage("data_reference." + dimension.getI18nKey()));
                    Font font = header.getFont();
                    header.setFont(new Font(font.getFontName(), Font.BOLD, font.getSize() + 2));
                    popupMenu.add(header);
                    popupMenu.add(new JPopupMenu.Separator());
                    */

                    popupMenu = new FilterPopupMenu(
                            MessageResolver.getMessage("data_reference." + dimension.getI18nKey()));

                    final JCheckBox button = new JCheckBox("Check/Uncheck all");

                    button.setSelected(container.isAllChecked());

                    button.addActionListener(new ActionListener() {
                        /**
                         * Invoked when an action occurs.
                         */
                        public void actionPerformed(ActionEvent e) {

                            for (Component c : container.getComponents()) {
                                if (c instanceof VBidirectionalBrowsingItem) {
                                    ((VBidirectionalBrowsingItem) c).getCheckBox()
                                            .setChecked(button.isSelected());
                                }
                            }
                        }
                    });

                    popupMenu.add(button);

                    popupMenu.add(new JPopupMenu.Separator());

                    popupMenu.add(container);

                    JButton view = new JButton(MessageResolver.getMessage("filtering"), VIcons.FILTER);
                    view.addActionListener(new ActionListener() {

                        /**
                         * Invoked when an action occurs.
                         */
                        public void actionPerformed(ActionEvent e) {
                            popupMenu.setVisible(false);
                        }
                    });

                    popupMenu.add(new JPopupMenu.Separator());

                    popupMenu.add(view);

                    popupMenu.show(VTree.this, (int) p.getX(), (int) p.getY());
                } else {
                    JOptionPane.showMessageDialog(VTree.this.getParent().getParent().getParent(),
                            MessageResolver.getMessage("no_items_found"),
                            MessageResolver.getMessage("error_message"), JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException sqle) {
                VExplorer.publishException(sqle);

                if (LOG.isErrorEnabled()) {
                    LOG.error(sqle.getMessage(), sqle);
                }
            }
        }
    }
}