import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JPopupMenu; 

/** 
 * Components that extend javax.swing.JWindow are not shown in correct location when 
 * the system uses 2 displays with different scaling factors. 
 * 
 * First display (also set as primary) has a scaling factor of 200% 
 * Second display has no scaling factor (uses the default value of 100%). 
 * 
 * How to reproduce: 
 * The JFrame is shown on primary display. 
 * Move the frame to the second monitor, without maximizing it. 
 * Press the button. 
 * The popup menu will be show on primary monitor. 
 */ 
public class JPopupDisplayedInWrongPosition extends JFrame { 

  /** 
   * Main class. 
   * @param args 
   * @throws Exception 
   */ 
  public static void main(String[] args) throws Exception { 

    final JFrame frame = new JFrame("Frame title"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     
    JPopupMenu popup = new JPopupMenu(); 
    popup.add(new AbstractAction("An action with name big enough to overflow JFrame's bounds") { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
      } 
    }); 
     
    JButton jButton = new JButton("BUTON"); 
    jButton.addActionListener(e -> popup.show(jButton, 0, jButton.getHeight())); 
     
    JPanel panel = new JPanel(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.fill = GridBagConstraints.NONE; 
    panel.add(jButton, c); 
    frame.getContentPane().add(panel); 
     
    frame.setSize(300, 300); 
    frame.setVisible(true); 
  } }