private void setMiddle() {
   Point point = owner.getLocationOnScreen();
   int x = point.x;
   int y = point.y;
   int width = (int) ((owner.getWidth() - this.getWidth()) / 2);
   int height = (int) ((owner.getHeight() - this.getHeight()) / 2);
   this.setLocation(x + width, y + height);
 }
 private static GraphicsDevice getScreen(JFrame window) {
   for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
     if (gd.getDefaultConfiguration().getBounds().contains(window.getLocationOnScreen()))
       return gd;
   }
   System.err.println(
       window + " does not appear to be on any screen; fullscreening onto default screen");
   return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
 }
  public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(600, 400));
    final JToolBar toolBar = new JToolBar();

    // Create the popup menu.
    final JPopupMenu popup = new JPopupMenu();
    popup.add(
        new JMenuItem(
            new AbstractAction("Option 1") {
              public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Option 1 selected");
              }
            }));
    popup.add(
        new JMenuItem(
            new AbstractAction("Option 2") {
              public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Option 2 selected");
              }
            }));

    final JButton button = new JButton("Options");
    toolBar.add(button);
    frame.getContentPane().add(toolBar, BorderLayout.NORTH);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    Point p = frame.getLocationOnScreen();
    System.out.println(p.x);
    button.addMouseListener(
        new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            if (e.isMetaDown()) {
              popup.show(e.getComponent(), e.getX(), e.getY());
            } else {
              System.out.println(e.getXOnScreen() + "  " + e.getYOnScreen());
            }
          }
        });
  }