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 3 of 3

Thread: MVC with MouseListener

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default MVC with MouseListener

    I am really stuck on what may be fundamental architectural understanding of how to implement an MVC ( Model-View) Java Application. Any help is really appreciated.

    I have implemented a pretty standard MouseListener in a Jframe. How do I get the mouse click data out of the Listener and into another thread. Most of the online examples show the manipulation of the event data within the scope of the listener object. Something like this is pretty typical:


    public class GUI extends JFrame{

    public GUI() {

    myCanvas = new DrawCanvas();

    MouseAdapter mouser = new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
    System.out.format( "mouse coor: %d %d\n",e.getX(),e.getY() );
    // STUFF ALL YOUR CODE HERE USING e.getX() and e.getY() FOR MOUSE DATA
    }
    };

    myCanvas.addMouseListener(mouser);
    Container cp = getContentPane();
    cp.add(myCanvas, BorderLayout.CENTER);

    }
    }
    class DrawCanvas extends JPanel {
    ..............
    }

    This is great for a nice simple example. But to keep the design modular I have another thread (Model) elsewhere. In addition, I have other listeners such as KeyAdapters, etc... How do I aggregate all these inputs into an object that can be grabbed by another thread. It seems I can't get e.getX() outside the scope of the MouseAdapter instantiation.

    And then i have the same problem of how do I get the model data (graphics) back into the JFrame once it has been computed.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: MVC with MouseListener

    FWIW Swing is already designed as MVC, its up to you to provide the model/data

    I'm not sure I truly understand the question. See if I have this correct - you have a separate thread that manages your data. Why do you need another thread? And is this one thread, or do you launch many?

    Going by the example you provided, the goal you wish is to modify the data in the listener class or launch a Thread/SwingWorker to do longer tasks - when that is complete update the UI. So to prevent tight integration between classes (which is one of the fundamental keys of Model-View-Controller) construct your own listener system in your data model. Have the View listen to changes in data and update accordingly. In this way, the controller (eg MouseListener) tells the model to update, the model updates, and when complete the model then tells the view to update. Things can be plugged in and out by adding/removing the appropriate listeners.

    See the following for examples: Java SE Application Design With MVC

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

    horvath62 (June 3rd, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MVC with MouseListener

    Thanks copeg. Your simple explanation of how it works helps alot!

    My struggle with MVC was knowing how to implement the listeners and observers, and getting data to flow between them. I found these two simple code examples which fully implement MVC. As usual having actual working code examples are huge. I hope others will find these useful as well....


    MVC Pattern

    The simplest Model View Controller (MVC) Java example

Similar Threads

  1. MouseListener Conundrum
    By Dirnol in forum Java Theory & Questions
    Replies: 6
    Last Post: October 11th, 2011, 07:11 AM
  2. problem with the MouseListener
    By boaz1001 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 23rd, 2011, 07:23 AM
  3. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  4. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM