Navigation problem in Popup and how to use setLocationRelativeto()

I have a JButton named Graphics:

enter image description here

When I press it, a new popup menu appears and the user selects Screen Resolution:

enter image description here

I have added custom images to menu items, I am unable to navigate to any of the menu items using arrow keys.

The second problem is that the popup menu does not stick within the frame, please let me know how to make it to stick within JFrame or graphics button.

I have tried KeyListener But that didn’t work.

In the following code, I have created a JPopupMenu and initialized it and in the KeyListener function, I have tried to solve the problem of navigating but it didn’t work.

popgraphics=new JPopupMenu();
JMenuItem p1920=new JMenuItem();
JMenuItem p1536=new JMenuItem();
JMenuItem p1280=new JMenuItem();
p1920.setIcon(new ImageIcon(getClass().getResource("icons20p.png")));
p1536.setIcon(new ImageIcon(getClass().getResource("icons36p.png")));
p1280.setIcon(new ImageIcon(getClass().getResource("icons80p.png")));
p1920.setBorder(b1);
// p1920.setText("1920x1080 Pixels");
// p1536.setText("1536x1440 Pixels");
// p1280.setText("1280x720 Pixels");

p1920.setFont(new Font("MV Boli",0,14));
p1536.setFont(new Font("MV Boli",0,14));
p1280.setFont(new Font("MV Boli",0,14));

p1920.setOpaque(false);
p1536.setOpaque(false);
p1536.setOpaque(false);

// popgraphics.setSelected(null);
// popgraphics.setSelectionModel(SingleSelectionModel);
popgraphics.addKeyListener(new KeyListener()
{

    @Override
    public void keyTyped(KeyEvent e) {
        if(e.getKeyChar()==(char)38)
        {
            if(popgraphics.getSelectionModel().getSelectedIndex()==1)
            {
                popgraphics.getSelectionModel().setSelectedIndex(3);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
            {
                popgraphics.getSelectionModel().setSelectedIndex(1);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
            {
                popgraphics.getSelectionModel().setSelectedIndex(2);
            }
        }
        if(e.getKeyChar()==(char)40)
        {
            if(popgraphics.getSelectionModel().getSelectedIndex()==1)
            {
                popgraphics.getSelectionModel().setSelectedIndex(2);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
            {
                popgraphics.getSelectionModel().setSelectedIndex(3);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
            {
                popgraphics.getSelectionModel().setSelectedIndex(1);
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyChar()==(char)38)
        {
            if(popgraphics.getSelectionModel().getSelectedIndex()==1)
            {
                popgraphics.getSelectionModel().setSelectedIndex(3);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
            {
                popgraphics.getSelectionModel().setSelectedIndex(1);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
            {
                popgraphics.getSelectionModel().setSelectedIndex(2);
            }
        }
        if(e.getKeyChar()==(char)40)
        {
            if(popgraphics.getSelectionModel().getSelectedIndex()==1)
            {
                popgraphics.getSelectionModel().setSelectedIndex(2);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==2)
            {
                popgraphics.getSelectionModel().setSelectedIndex(3);
            }
            else if(popgraphics.getSelectionModel().getSelectedIndex()==3)
            {
                popgraphics.getSelectionModel().setSelectedIndex(1);
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
});

popgraphics.add(p1280);
popgraphics.add(p1536);
popgraphics.add(p1920);
Point p=graghics.getLocation();
popgraphics.setLocation(p);
popgraphics.setBackground(new Color(20,20,20));
popgraphics.setOpaque(false);

Let me know if any other information is needed.

Answer

Here is a simple program that displays a JButton with a large icon. A JPopupMenu is connected to the JButton. The JPopupMenu contains two JMenuItem where each one has its own, separate icon. Run the program. Place the mouse pointer over the JButton. Bring up the JPopupMenu. (On my Windows 10 machine, I click the right mouse button display the JPopupMenu.) Then hit the down arrow key on the keyboard.

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class PopupTst {

    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Icon ico = new ImageIcon("first icon"); // replace with actual path to icon.
        JButton button = new JButton(ico);

        JPopupMenu popup = new JPopupMenu();
        Icon ico2 = new ImageIcon("second icon"); // replace with actual path to icon.
        JMenuItem one = new JMenuItem(ico2);
        popup.add(one);
        Icon ico3 = new ImageIcon("third icon"); // replace with actual path to icon.
        JMenuItem two = new JMenuItem(ico3);
        popup.add(two);

        button.setComponentPopupMenu(popup);

        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        PopupTst instance = new PopupTst();
        EventQueue.invokeLater(() -> instance.createAndDisplayGui());
    }
}