Is it possible to use same event code for several objects?
Hi,
I've made a gwt gui-app with 5 buttons and added mouseoverhandlers for these and
would like to use something like:
button.addMouseOverHandler(Object object, new MouseOverHandler()
{
@Override
public void onMouseOver(MouseOverEvent event) {
(button)object.getElement().getStyle().setOpacity( mouseOverOpacity);
}
});
instead of:
button1.addMouseOverHandler(new MouseOverHandler()
{
@Override
public void onMouseOver(MouseOverEvent event) {
button1.getElement().getStyle().setOpacity(mouseOv erOpacity);
}
});
button2.addMouseOverHandler(new MouseOverHandler()
{
@Override
public void onMouseOver(MouseOverEvent event) {
button2.getElement().getStyle().setOpacity(mouseOv erOpacity);
}
});
....
i.e avoiding to repeat same code for every button.
Is that possible to do and if so , how?
Regards
stab
Re: Is it possible to use same event code for several objects?
You create a single ActionListener (it's an interface, so it's common for people to implement the interface in their application-containing JFrame for example) and Event.getSource() has a reference to which control the event is 'for'.
Post code in code tags.
Re: Is it possible to use same event code for several objects?
Quote:
i.e avoiding to repeat same code for every button.
Is that possible to do and if so , how?
Yes, it is possible as Sean4u suggested. It would be fine if the code to handle the event is same for all buttons, otherwise you will come up with many if-else statements to identify which button is clicked, which may make the code looks messy.
Re: Is it possible to use same event code for several objects?
Actually I would like to do this for smartgwt imgbuttons, but I don't understand how to add an ActionListener.
I have a class:
public class gwtMap extends Composite {
and when I add, implements ActionListener, I'm supposed to import ava.awt.event.ActionListener but I guess I shouldn't mix awt and gwt.
Re: Is it possible to use same event code for several objects?
Quote:
Originally Posted by
stab
Actually I would like to do this for smartgwt imgbuttons, but I don't understand how to add an ActionListener.
I have a class:
public class gwtMap extends Composite {
and when I add, implements ActionListener, I'm supposed to import ava.awt.event.ActionListener but I guess I shouldn't mix awt and gwt.
I don't have experience in GWT, however I think there's an equivalent Action listener in GWT.
java memory