I've been looking for the way but they all throw me a mistake, I'd like you to help me with this. I'm doing a minesweeper and I need to leave a caution flag when I right click.
JButton boton = new JButton();
I've been looking for the way but they all throw me a mistake, I'd like you to help me with this. I'm doing a minesweeper and I need to leave a caution flag when I right click.
JButton boton = new JButton();
You can do it by adding a ActionListener
and check the modifiers:
boton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getModifiers() & 4) !=0){
// boton derecho
}
}
});
Left button would be getModifiers() & 16
and the middle button getModifiers() & 8
.
Alternatively you could use a MouseListener
:
MouseListener mouseListener= new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getButton());
// MouseEvent.BUTTON3 es el boton derecho
}
};
boton.addMouseListener(mouseListener);
If you use onMousePressed
and onMouseReleased
you can show your information only while the button is pressed.