aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListener" Watch "ActionListener" New topic
Author

ActionListener

Lim Youjie
Greenhorn

Joined: Dec 27, 2004
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Lim Youjie
Greenhorn

Joined: Dec 27, 2004
Posts: 22
any samples for setting flag ... just a short code will do ...
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Dec 27, 2004
Posts: 22
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

Joined: Dec 27, 2004
Posts: 22
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

Joined: Sep 28, 2005
Posts: 14
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

Joined: Dec 27, 2004
Posts: 22
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionListener