Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Is it possible to use same event code for several objects?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is it possible to use same event code for several objects?

    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.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is it possible to use same event code for several objects?

    Quote Originally Posted by stab View Post
    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
    Last edited by namhm; December 4th, 2011 at 05:51 PM.

Similar Threads

  1. Event Issues
    By fractalorbit in forum AWT / Java Swing
    Replies: 3
    Last Post: September 2nd, 2011, 07:49 PM
  2. [SOLVED] jTable event help
    By banny7 in forum AWT / Java Swing
    Replies: 12
    Last Post: August 1st, 2011, 07:42 AM
  3. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 02:29 AM
  4. After a rowInserted() event!
    By MarkusHendersonicus in forum JDBC & Databases
    Replies: 1
    Last Post: December 20th, 2010, 11:55 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM