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.


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

Results 1 to 3 of 3

Thread: Custom listener: how to separate interactors

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Thanked 0 Times in 0 Posts

    Default Custom listener: how to separate interactors

    Hello, my application shows a profile. The profile has various interactors. I'm trying to follow the MVC model, so I neeed to tell my controller that something was selected. But the profile has many elements that can be selected(mostly labels, so not setActionCommand), how do I tell it WHICH one was it?

    I understand that I will need to use some kind of(probably a custom one) listener. But the main problem is how do I separate those interactors?

    --- Update ---

    I solved it myself. I created a HashMap that maps from JLabels to Strings. When a mouse event occurs I loop trough it to search for the event source. If I find it I fire my custom event.

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Custom listener: how to separate interactors

    One's first introduction to MVC invariably involves a long if statement in the Controller to discriminate which element of the View has demanded attention. There are always discriminators available. All Components have Names, JButtons and JLabels have Text, and JButtons have ActionCommands. Even so, the long if statements in the Controller seem (and probably are) inefficient, so variations have been devised.

    Here are a few:

    Anonymous inner classes can be used as action listeners, but those create their own challenges, none of which are difficult to overcome. Some will suggest the use of listeners this way violates MVC, but it's an acceptable practice and consistent with Java's architecture. If followed faithfully, this design forces a one-to-one relationship between UI component and listener which may require more effort by the programmer but also allows each component to have very specific and possibly unique results.

    The Observer pattern is more flexible, allowing a range of one-to-as many as desired and is likely more faithful to MVC, but I suppose it is flexible enough to be as unfaithful as the programmer wants.

    The other pattern I've recently begun exploring is the EventBus pattern. While I haven't completed a significant program that uses it, I've preferred this approach in the study pieces I've written, using all of the patterns I've mentioned for direct comparison.

    I haven't answered your question directly, but here's what I suggest: If you're in a hurry and must complete your project using MVC per someone else's judgment, then find a way to discriminate between the components in an if statement in the Controller. If you have the time and are able to expand the definition of MVC a bit (slightly to not at all), then explore the other patterns I've mentioned and pick the one you like the best.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Bebras (January 12th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    My Mood
    Thanked 0 Times in 0 Posts

    Default Re: Custom listener: how to separate interactors

    I am not quite get your problem but, i think getSource() method may help you to find which controller is interracted.

    Object obj = e.getSource();
    if (obj instanceof JComboxBox){
       JComboBox jcb = (JComboBox)obj;
       // do something with this combo box.

Similar Threads

  1. [SOLVED] Does RMI require separate threads
    By mds1256 in forum Threads
    Replies: 1
    Last Post: November 17th, 2012, 09:38 AM
  2. executing separate queries via threading
    By nischalinn in forum Threads
    Replies: 2
    Last Post: June 15th, 2012, 12:06 PM
  3. How to separate ArrayLists in memory?
    By wattieboi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:18 AM
  4. how to separate this code in another class
    By Jhovarie in forum AWT / Java Swing
    Replies: 2
    Last Post: February 28th, 2011, 06:22 PM
  5. separate strings
    By ridg18 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 21st, 2011, 06:22 AM