Making your class an event source in Java
Posted
by Crystal
on Stack Overflow
See other posts from Stack Overflow
or by Crystal
Published on 2010-05-24T18:18:26Z
Indexed on
2010/05/24
18:21 UTC
Read the original article
Hit count: 296
I'm making a custom button in Java that has two states, mousePressed, and mouseReleased. At the same time, if I wanted to reuse this button, so that other event listeners can register with it, are these the appropriate steps I should do:
override addActionListener(ActionListener action)
override removeActionListener(ActionListener action)
have a private variable like List <ActionListener> list = new List <ActionListener>() to keep track of when events get added
and some sort of function with for loop to run all the actions:
public void runListeners()
{
for (ActionListener al : list) {
al.actionPerfomed;
}
}
I'm not really sure if this is the way you can do it or if there are other things I am missing. Like does my custom class have to be implements ActionListener? Thanks.
© Stack Overflow or respective owner