Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListener

 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know how to trigger an event when i clicked on a button and then clicked on another button(buttonPlay[0]).
I think my code is wrong but i dunn have any idea how to implement it. Thanks

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("1"))
{
if (e.getSource() == buttonPlay[0])
{
System.out.println("1 and buttonPlay[0] pressed");
}
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A listener for the first button would have to record the fact that the button was pressed, by setting a flag, for example. Then a separate listener for the second button would need to check the flag to see if the first button had already been pressed. Presumably you'd want other buttons and things to clear that flag if other UI actions occur after you press the first button.
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any samples for setting flag ... just a short code will do ...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand the question, here's a simple solution:



I don't know why you'd want to do that. Could you explain a little bit more about what you're trying to do and why?
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i try but it dunn seem to work.

trying to create a set of buttons on the toolbar(with numbers from 1 to 9)
and another set of buttons in the center of the program.

For example when i click on the button labeled "1" on the toolbar, and then click on one of the buttons in the center of the program, the button will be labeled "1".
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code(cant work), flag variable is still 0 when its at the second loop

public void actionPerformed(ActionEvent e)
{
int flag = 0;
if (e.getSource() == buttonNumbers[0])
{
flag = 1;
System.out.println("1 is pressed");
}
if (e.getSource() == buttonPlay[0] && flag == 1)
{
System.out.println("1 is print");
System.out.println(flag);
flag = 0;
}
}
 
Joe McCarthy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The flag is [re]initialized to zero each time the actionPerformed method is invoked. You might want to use a separate variable -- external to actionPerformed -- to store the label you want to use for the button in the center of the window.

Here's a potential solution fragment:
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic