I have a JButton named Graphics:
When I press it, a new popup menu appears and the user selects Screen Resolution:
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("icons\\1920p.png")));
p1536.setIcon(new ImageIcon(getClass().getResource("icons\\1536p.png")));
p1280.setIcon(new ImageIcon(getClass().getResource("icons\\1280p.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.


Here is a simple program that displays a
JButtonwith a large icon. AJPopupMenuis connected to theJButton. TheJPopupMenucontains twoJMenuItemwhere each one has its own, separate icon. Run the program. Place the mouse pointer over theJButton. Bring up theJPopupMenu. (On my Windows 10 machine, I click the right mouse button display theJPopupMenu.) Then hit the down arrow key on the keyboard.