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

Thread: Drag and drop in MVC

  1. #1
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Drag and drop in MVC

    Hey everyone,

    I'm new here. This is my first post.

    A bit about myself first: I'm a fairly inexperienced Java programmer but I've worked with other (mostly sequential) languages before. I still find it kind of hard thinking in OO and I hope to learn a lot from you guys.

    I have a complex project I wish to realize, and to develop a framework I wrote a cute little program that I could later expand into my project. The test case program is called ColoredDots and you can find the Eclipse project files here. (No binaries, but the program compiles and works).

    When you look at my code, you'll see I use the Observer interface and the Observable class. However, I don't have a controller.

    I read this article and I tried to model ColoredDots more like the Calculator. But since I'm not using Swing controls (buttons and such) in ColoredDots I can't simply use the ActionListener in the controller. I somehow need to get the MouseListener (currently on the lPane in my view) and move it into the controller.

    Any ideas how I do that?


    Kisses,

    Alice


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Drag and drop in MVC

    You should make you ColouredDots class implementment the method, addMouseListener(MouseListener ml); and then your controller class can call ColouredDots.addMouseListener(new MouseListener());

    I hope that makes sense :/.

    I'm never the best at explaining things.

    Chris

  3. #3
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drag and drop in MVC

    Hey Chris, thanks for replying.
    Quote Originally Posted by Freaky Chris View Post
    You should make you ColouredDots class implementment the method, addMouseListener(MouseListener ml); and then your controller class can call ColouredDots.addMouseListener(new MouseListener());
    Yes... but I thought ColoredDots (I renamed it to ColoredDotsMVC because I have more than one version in Eclipse) is where model, view and controller are linked together. If I mirror the calculator example it should give something like this:

    package com.whatever.ColoredDotsMVC;
     
    import javax.swing.*;
     
    import com.whatever.ColoredDotsMVC.controller.ColoredDotsAppController;
    import com.whatever.ColoredDotsMVC.model.ColoredDotsAppModel;
    import com.whatever.ColoredDotsMVC.view.ColoredDotsAppView;
     
     
    public class ColoredDotsMVC
    {
      public static void main(String[] args)
      {
        ColoredDotsAppModel model = new ColoredDotsAppModel();
        ColoredDotsAppView view = new ColoredDotsAppView(model);
        ColoredDotsAppController controller = new ColoredDotsAppController(model, view);
      }
    }
    Where do I put the addMouseListener(...) ?

    My view looks like this at the moment (code does not work).

    package com.whatever.ColoredDotsMVC.view;
     
    import java.awt.Color;
    import java.awt.Dimension;
     
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
     
    import com.whatever.ColoredDotsMVC.model.ColoredDotsAppModel;
     
     
    public class ColoredDotsAppView
    {
      private ColoredDotsAppModel model;
      private static final Dimension MAIN_SIZE = new Dimension(800, 600);
     
      public JLayeredPane lPane = new JLayeredPane();
      private JPanel bPanel = new JPanel(null);
     
      /** constructor **/
      public ColoredDotsAppView(ColoredDotsAppModel model)
      {
        // Link to the model for data access
        this.model = model;
        // Create and show the GUI.
        createAndShowGUI();
      }
     
      private JPanel createContentPane()
      {
        JPanel contentPane = new JPanel(null);
     
        lPane.setPreferredSize(MAIN_SIZE);
        bPanel.setBounds(0, 0, MAIN_SIZE.width, MAIN_SIZE.height);
        bPanel.setBackground(Color.LIGHT_GRAY);
        lPane.add(bPanel, new Integer(0));
     
        return contentPane;
      }      
     
      private void createAndShowGUI()
      {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Colored Dots");
     
        // Set the content pane.
        frame.setContentPane(createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
      }
     
    } // end class ColoredDotsAppView

    So, it would be easy to add the mouseListener to the lPane in createContentPane(). But that would not put the control logic into the controller.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Drag and drop in MVC

    You need to add the method, addMouseListener(MouseListener l)to the class ColoredDotsAppView, then in your ColoredDotsAppController class in the constructor you can call view.addMouseListener(myMouseListener);

    public class ColoredDotsAppView
    {
         public void addMouseListener(MouseListener ml){
              lPane.addMouseListener(ml);
         }
    }

    public class ColoredDotsAppController{
        public  ColouredDotsAppController(ColoredDotsAppModel model, ColoredDotsAppView view){
              view.addMouseListener(loadMouseListener());
         }
     
        private MouseListener loadMouseListener(){
             return new MouseListener(){
                             //imp
             };
        }
    }

    Chris

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Alice (December 3rd, 2010)

  6. #5
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drag and drop in MVC

    Yes, I see now.

    I've got a lot of stuff to do today, but tomorrow I have all night to play with this lead.

    Thanks Chris. I'll post my results.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Drag and drop in MVC

    I look forward to see how you managed to get on in the end. Hopefully I'll be around more often now to provide help as a break from my assignments


    Chris

  8. #7
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drag and drop in MVC

    Quote Originally Posted by Freaky Chris View Post
    I look forward to see how you managed to get on in the end.
    The mouseListener solution worked like a charm. Thank you so much!

    Actually I had more trouble getting the JPanel to behave. I keep having trouble with layered panels.

    Anyway, I uploaded my Eclipse project (without binaries) here.

    Since the shapes too should be MVC, I converted them into a model, view and controller of their own. They are being instantiated in the ColoredDotsAppController class inside a mouseReleased method:

    public void mouseReleased(MouseEvent mE)
    {
      // If left mouse button clicked and released...
      if ((mE.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)
      {
        DotModel newDotM = new DotModel(mE.getX()-10, mE.getY()-10, 1);
        appModel.addDot(newDotM);
        DotView newDotV = new DotView(newDotM);
        newDotV.setBounds(mE.getX()-10, mE.getY()-10, 20, 20);
        appView.lPane.add(newDotV.getPanel(), new Integer(10));
        DotController newDotC = new DotController(newDotM, newDotV);
      }
    }
    However, I'm not sure the setBounds belongs there. Shouldn't it somehow go into the DotView class?

    And also, probably part of the layered panel issues, I don't get the dots to be drawn on their panels. I can move them around, but the filled circles don't appear.

    It started with a stack overflow error where addMouseListener was called. Back then my DotView class was an extended JPanel. I tried to work around it by creating a new JPanel inside the class and attach the addMouseListener to that. This got rid of the stack overfow, but created a bunch of new errors, which I all took care of. But now the circles don't get drawn.

    public class DotView implements Observer // extends JPanel  <-   I took this out ...
    {
    	private DotModel dotModel;
    	private JPanel dotPanel;     // <- ... and put this in
      ...
     
      public void addMouseListener(MouseListener mL)
      {
      	dotPanel.addMouseListener(mL); // <- this is what caused the stack overflow before I added the dotPanel
      }
      ...
    }
    I'll be around more often now to provide help as a break from my assignments
    Your assignments? Work or school? Or household chores?

  9. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Drag and drop in MVC

    Seems odd that adding a mouse listener to a jframe would cause a stack overflow error.

    I'll look it over and see what I find

    I'm a University Student in England. Second year computer scientist.

    Interestingly the porject I just handed in was about bouncing balls

    Chris

  10. #9
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drag and drop in MVC

    Quote Originally Posted by Freaky Chris View Post
    Seems odd that adding a mouse listener to a jframe would cause a stack overflow error.
    Umm... well I added it to a JPanel.

    I've uploaded the version that causes the Stack Overflow Error here.

    This is the DotView code where the error originates from:

    package com.whatever.ColoredDotsMVC.view;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.Observable;
    import java.util.Observer;
    import javax.swing.JPanel;
     
    import com.whatever.ColoredDotsMVC.model.DotModel;
     
     
    public class DotView extends JPanel implements Observer 
    {
      private DotModel dotModel;
      //private JPanel dotPanel;
     
      public DotView(DotModel nModel)
      {
        dotModel = nModel;
        dotModel.getObservable().addObserver(this);
     
        setSize(20,20);
        setLocation(dotModel.getX()-10, dotModel.getY()-10);
        setVisible(true);
      }
     
      public void addMouseListener(MouseListener mL)
      {
        // dotPanel.addMouseListener(mL);
        addMouseListener(mL);        // <--- this is line 44
      }
     
      public void addMouseMotionListener(MouseMotionListener mML)
      {
        // dotPanel.addMouseMotionListener(mML);
        addMouseMotionListener(mML);
      }
     
      public void update(Observable obs, Object obj)
      {
        System.out.println("DotView update");
        // dotPanel.setLocation(dotModel.getX(), dotModel.getY());
        // dotPanel.repaint();
        setLocation(dotModel.getX(), dotModel.getY());
        repaint();
      }
     
      public void paintComponent(Graphics g)
      {
        System.out.println("DotView paintComponent");
        g.setColor(convertStatus(dotModel.getStatus()));
        g.fillOval(0,0,20,20);
      }
     
      private Color convertStatus(int status)
      {
        Color returnColor = Color.RED;		
        if (status == 2)
          returnColor = Color.GREEN;
        if (status == 3)
          returnColor = Color.BLUE;
     
        return returnColor;
      }
    } // end class DotGraphic
    I know it's not exactly SSCCE, but I don't know which part of the code is wrong. I left some of the code in comment what I did to work around the error.

    And here's the error message:

    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at com.whatever.ColoredDotsMVC.view.DotView.addMouseL istener(
    DotView.java:44)
    As far as I have found out, it has something to do with repeatedly calling the method. It is typically a recursive call error.

    Quote Originally Posted by Freaky Chris
    I'll look it over and see what I find
    I'm a University Student in England. Second year computer scientist.
    Oh, then by all means don't spend too much time looking through my silly mistakes. Your studies are much more important.

    Interestingly the porject I just handed in was about bouncing balls
    I'd love to see that some time.

  11. #10
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drag and drop in MVC

    I finally found why I had a stack overflow error.

    Since I have two controllers (one for the application and one for the individual dots), and both of them call a mouseListener method from their related view, there was confusion between the names. I had named both calling methods addMouseListener().

    I gave them different names and now the error is gone. (Few, it took me three days to think of that.)


    So, I'll close this thread in a few days. If someone would like to add a comment, please do. Especially if it would help me improve my program.

    I have a few more questions to ask concerning MVC, but I'll create a new thread for those.

    Thanks again for your suggestion, Chris.

Similar Threads

  1. Drag and Drop in JTrees
    By helloworld922 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 21st, 2014, 11:16 AM
  2. Looking for help with Walter Zorn's Drag & Drop script
    By yuri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2010, 02:52 AM
  3. Drag and Drop in JTrees
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 19th, 2010, 11:51 PM
  4. Struts drop down
    By kalees in forum Web Frameworks
    Replies: 1
    Last Post: January 15th, 2010, 12:56 AM
  5. 'MouseUp' Drag and Drop Events
    By copeg in forum AWT / Java Swing
    Replies: 0
    Last Post: November 10th, 2009, 07:21 PM

Tags for this Thread