I gave up and use a JDialog instead of a JPopupMenu. It seems impossible to get the click event that closes the popup.
Starting mouse drag when JPopupMenu is open?
-
11-10-2022 - |
Question
I have a popup menu that opens on mouse hovering on a component. Now I want to be able to start dragging the component while the popup is still open, but the popup menu close event always consumes the first mouse click. Is there a workaround for that?
Solution 2
OTHER TIPS
public class A
.....
implements MouseListener, ActionListener
{
JPopupMenu pmnu ;
JMenuItem setcol ;
JList lst ;
Component cmp = null ;
int x = 0, y = 0;
...
private void
pop ( MouseEvent ev )
{
if ( ev.isPopupTrigger () )
{
cmp = ev.getComponent () ;
x = ev.getX () ;
y = ev.getY () ;
pmnu.show ( cmp, x, y ) ;
}
}
public void mouseDragged( MouseEvent ev )
{
pop ( ev ) ;
}
public void mouseReleased ( MouseEvent ev )
{
pop ( ev ) ;
}
public void actionPerformed( ActionEvent ev )
{
Object src = ev.getSource() ;
if ( src == setcol && cmp != null )
{
cmp.setBackground ( Color.yellow ) ;
return ;
}
}
public A( )
{
pmnu = new JPopupMenu () ;
setcol = new JMenuItem ( "Set color" ) ;
pmnu.add ( setcol ) ;
....
}
public void
init()
{
setcol.addActionListener ( this ) ;
....
lst.addMouseListener ( this ) ;
}
public void mouseClicked(MouseEvent ev) {}
public void mouseEntered(MouseEvent ev) {}
public void mouseExited(MouseEvent ev) {}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow