ActionListener on pressed
Hi,
I have a couple of j buttons in my application i want the println to be done when the Jbutton is pressed and not when it is released. Any ideas?
publicvoid actionPerformed(ActionEvent evt)
{
if (evt.getSource() == button1)
{
System.out.println("Gas geven");
}
elseif (evt.getSource() == button2)
{
System.out.println("turnLeft");
}
elseif (evt.getSource() == button3)
{
System.out.println("turnRight");
}
else
{
System.out.println("decelerate");
}
}
Message was edited by:
DJ_Kat
[1251 byte] By [
DJ_Kata] at [2007-10-3 6:25:17]

try like this
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
if(evt.getSource() == jButton2)
//ACTION TO BBUTTON2
if(evt.getSource() == jButton3)
//ACTION TO BBUTTON3
}
private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getSource() == jButton2)
//ACTION TO BBUTTON2
if(evt.getSource() == jButton3)
//ACTION TO BBUTTON3
}
Hi thnx for the replie,
I tried the code and ajusted it since I have implemented my class with MouseListener.
public void mousePressed(MouseEvent evt)
{
buttonPressed = true;
if(evt.getSource()=="button1"){
System.out.println("dit is button 1");
}
}
this didnt work for me.
However i did something else that work for me
public class Controller extends JFrame implements MouseListener
{
private JButton button1;
public Controller (Car myCar)
{
button1 = new JButton("^");
contentPane.add(button1);
button1.addMouseListener(this);
button1.setName("button1");//I add a name atribute to make this work
button1.setPreferredSize(new Dimension(180,20));
}
//Implementatie van interface MouseListener
public void mouseClicked(MouseEvent evt)
{
}
public void mouseEntered(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{
}
public void mousePressed(MouseEvent evt)
{
System.out.println(((JButton)(evt.getSource())).getName());
}
public void mouseReleased(MouseEvent evt)
{
System.out.println("mouse released");
}
}