JButton event listening if timer event already exists
I would like to detect an event on a button, which is easy enough. But I already have a timer event happening and when I click a button nothinghappens.
I would like to store what button was pressed so that within the timer event I can check if a button and which button has been clicked and act on this.
Code I have is below, taken out most of what isn't needed to explain what I have.
int DELAY = 75;
private Timer movingTimer;
public BackgroundLayered(int inWidth,int inHeight)
{
//loading images
animation =new totalAnimation();
animation.............................
//displaying the images
ImageIcon image =new ImageIcon("background2.jpg");
JLabel background =.............................
JPanel panel =new JPanel();
//setting up labels
cow1Image = animation.cow1Right();
cow1 =new JLabel(cow1Image);
cow1.setBounds(cow1startX, cow1startY, 20, 20);
..................................
//adding buttons
block =new JButton("Block");
eat =new JButton("Eat");
........................
//setting bounds
block.setBounds(3, 305,70,30);
eat.setBounds(76, 305,70,30);
.....................................
//start timer
movingTimer =new Timer(DELAY,new TimerHandler());
movingTimer.start();
//adding components to display
// Labels added to Palette layer and buttons to default layer
getLayeredPane().add(cow0, JLayeredPane.PALETTE_LAYER);
getLayeredPane().add(cow1, JLayeredPane.PALETTE_LAYER);
getLayeredPane().add(block, JLayeredPane.DEFAULT_LAYER);
....................................
panel.setOpaque(false);
setContentPane(panel);
}
//this event handler doesnt get invoked when buttons are pressed
publicvoid actionPerformed(ActionEvent event)
{
System.out.println("button pressed");
}
privateclass TimerHandlerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent actionEvent)
{
//moving the cows
cow0.setLocation(cow0startX, cow0startY);
cow1.setLocation(cow1startX, cow1startY);
.................................
//check for collisions
collisionCow0 = obstacle.checkCol1Cow0();
collisionCow1 = obstacle.checkCol1Cow1();
cowBack = obstacle.cowob1Collided();
.................................................
}

