Hi
I have various buttons on my frame and i want to add a context menu for each button on right click but i have already an action listener on those buttons
So how can i add the context menu on right click
Help Me Please
Printable View
Hi
I have various buttons on my frame and i want to add a context menu for each button on right click but i have already an action listener on those buttons
So how can i add the context menu on right click
Help Me Please
I think you have to add a mouselistener
or perhaps you just want to use a tool tip instead.
Here's how I created a context menu for a text field. Should work just as well for a button.
Code:final String demoText
= "Fred's flight objection fixes and zaps the wavy bump quickly.";
Action resetAction = new AbstractAction("Reset") {
public void actionPerformed(ActionEvent e) {
text.setText(demoText);
}
};
final JPopupMenu resetMenu = new JPopupMenu();
resetMenu.add(new JMenuItem(resetAction));
text = new JTextField(demoText);
text.addActionListener(this);
text.addMouseListener(new MouseListener() {
void popup(MouseEvent e) {
resetMenu.show(text, e.getX(), e.getY());
}
public void mousePressed(MouseEvent e)
{ if(e.isPopupTrigger()) popup(e); }
public void mouseReleased(MouseEvent e)
{ if(e.isPopupTrigger()) popup(e); }
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
} );
Code is supposed to check isPopupTrigger for both mousePressed and mouseReleased because platforms differ as to which is the trigger.