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]
# 1
Hi,Write your codes on mouse pressed and key pressed event.
dhinakaran_apeca at 2007-7-15 1:11:16 > top of Java-index,Security,Event Handling...
# 2
Are you saying i should use the MousListener instead of the ActionListener.If so how do i check on which Jbutton is pressed.?
DJ_Kata at 2007-7-15 1:11:16 > top of Java-index,Security,Event Handling...
# 3

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

}

dhinakaran_apeca at 2007-7-15 1:11:16 > top of Java-index,Security,Event Handling...
# 4

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");

}

}

DJ_Kata at 2007-7-15 1:11:16 > top of Java-index,Security,Event Handling...
# 5
Sorry I had to get back on that,Your solution did work. Thanks againMessage was edited by: DJ_Kat
DJ_Kata at 2007-7-15 1:11:16 > top of Java-index,Security,Event Handling...