Results 1 to 5 of 5
  1. #1
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Question Moving whole content of JScrollPane by mousedrag

    Hi, at first, I'm sorry for my english. And I am not much experienced in Java, so please be patient ;)

    My problem is:

    I have got a DesktopPane, which is set as Viewport of a JScrollPane. The DesktopPane is 2000pts wide and 1000pts high, while the JScrollPane is generally much smaller. And what I want is: to move content of the DesktopPane by mouse drag (using right mouse button), but not only when I drag mouse on the DesktopPane itself, but also when I drag mouse on JInternalFrames (and theirs components), which are situated in this DesktopPane.

    Any ideas?

    Meanwhile I am using a "temporary" alternative solution, which is the modified MouseMotionListener:

    Java Code:
    class DragMoverListener extends MouseInputAdapter
        implements MouseMotionListener{
    		private final Cursor dc;
    		private final Cursor hc = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    		private final Rectangle rect = new Rectangle();
    		private final JComponent comp;
    		private final JViewport vport;
    		private Point startPt = new Point();
    		private Point move  = new Point();
    
    		public DragMoverListener(JViewport vport, JComponent comp) {
    			this.vport = vport;
    			this.comp = comp;
    			this.dc = comp.getCursor();
    			vport.addMouseMotionListener(this);
    			vport.addMouseListener(this);
    		}
    
    		public void mouseDragged(MouseEvent e) {
    			Point pt = e.getPoint();
    			move.setLocation(pt.x-startPt.x, pt.y-startPt.y);
    			startPt.setLocation(pt);
    			Rectangle vr = vport.getViewRect();
    			int w = vr.width;
    			int h = vr.height;
    			Point ptZero = SwingUtilities.convertPoint(vport,0,0,comp);
    			rect.setRect(ptZero.x-move.x, ptZero.y-move.y, w, h);
    			comp.scrollRectToVisible(rect);
    		}
    		
    		public void mousePressed(MouseEvent e) {
    			comp.setCursor(hc);
    			startPt.setLocation(e.getPoint());
    		}
    		public void mouseReleased(MouseEvent e) {
    			comp.setCursor(dc);
    		}
    	}
    Use:
    Java Code:
    jScrollPane1.addMouseMotionListener(new DragMoverListener(jScrollPane1.getViewport(), myDesktopPane));
    It works so, that when I drag mouse (any mouse button) on the DesktopPane itself, it moves (whole content of the DesktopPane, including JInternalFrames), but when I drag mouse on some JInternalFrame, it does nothing (of course).

    I was speculating about modifiing this DragMoverListener (mainly recounting coordinates) and adding it to all JInternalFrames (and their components). But it seems to be uselessly difficult.

    I think, that there must be some simple solution, which I am still missing :( Can somebody help?

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Some clarifications:

    When you say 2000pts, you seem to mean 2000pixels.
    A point is about 1/72 of an inch. A pixel is often smaller on modern displays.
    (It would be better style to really code in points, but that adds complexity.)

    The word "drag" probably means what is often called "pan".
    You want the scroll area to display a different portion of the JInternalFrame.
    {"Drag" often means the first part of a drag-and-drop operation.}

    After some googling about, I found an old post about capturing mouse events.
    Both AWTEventListener and the Glass Pane can be made to work.
    I once posted some code that used GlassPane' it may be helpful.

  3. #3
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    Thanks. I'll take a look at it. Glasspane sounds good.

    When you say 2000pts, you seem to mean 2000pixels.
    A point is about 1/72 of an inch. A pixel is often smaller on modern displays.
    (It would be better style to really code in points, but that adds complexity.)
    I meant pixels :o

    The word "drag" probably means what is often called "pan".
    You want the scroll area to display a different portion of the JInternalFrame.
    {"Drag" often means the first part of a drag-and-drop operation.}
    - My only "counsellors" were a dictionary and a deduction (In MotionListener there is a method "mouseDragged", so, I just extracted the "drag" word :o ). I meant "click and move" :o
    - But I want the scroll area to display a different portion of the JDesktopPane (which contains a lot of JInternalFrames).

    But, I apologized for my english.

    :)
    Last edited by Taiko; 01-16-2010 at 09:46 PM. Reason: mistakes

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    You have no need to apologize for your English.
    It is far, far better than my any-other-language.

    I intended my comments only to clarify technical points.

    Good luck with GlassPane.

  5. #5
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    zweibieren: Thanks once again.

    I took a look at both, the AWTEventListener and the Glass Pane, and solved my problem by using the AWTEventListener. But, new problem appeared.

    At first, my solution:

    Java Code:
    class MyAlmightyListener implements AWTEventListener {
    	private Point startPt;
    	private final Cursor defaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
    	private final Cursor handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    	private final JViewport vport;
    	private final JComponent comp;
    	private Point move  = new Point();
    	private Point ptZero, it;
    	private final Rectangle rect = new Rectangle();
    	private Rectangle vr;
    	private int n, w, h;
    	private MouseEvent event;
    	private boolean rightButtonPressed = false;
    
    		
    	//"JComponent comp" is in my case the JDesktopPane object
    	public MyAlmightyListener(JViewport vport, JComponent comp) {
    		this.comp = comp;
    		this.vport = vport;
    	}
    		
    	public void eventDispatched(AWTEvent e) {
    		event = (MouseEvent) e;
    
    		//catching press of button no. 3 (right button)
    		if (event.getID() == MouseEvent.MOUSE_PRESSED) {
    			if (event.getButton() == 3) {
    
    				//getting mouse location, when mouse button is pressed
    				startPt = event.getLocationOnScreen();
    				n = comp.getComponentCount();
    
    				//changing mouse cursor on every showing component of my JDesktopPane
    				for (int i = 0; i < n; i++) {
    					comp.getComponent(i).setCursor(handCursor);
    				}
    
    				//changing mouse cursor on my JDesktopPane
    				comp.setCursor(handCursor);
    				rightButtonPressed = true;
    			}
    		}
    
    		//catching release of button no. 3 (right button)
    		//and changing mouse cursor back on all components
    		if (event.getID() == MouseEvent.MOUSE_RELEASED) {
    			if (event.getButton() == 3) {
    				for (int i = 0; i < n; i++) {
    					comp.getComponent(i).setCursor(defaultCursor);
    				}
    				comp.setCursor(defaultCursor);
    				rightButtonPressed = false;
    			}
    		}
    
    		//catching mouse move when the right mouse button is pressed
    		if (event.getID() == MouseEvent.MOUSE_DRAGGED) {
    			if (rightButtonPressed) {
    				it = event.getLocationOnScreen();
    
    				//calculation of move
    				move.setLocation(it.x-startPt.x, it.y-startPt.y);
    				startPt.setLocation(it);
    				vr = vport.getViewRect();
    				w = vr.width;
    				h = vr.height;
    
    				//getting zero point in my JDesktopPane coordinates
    				ptZero = SwingUtilities.convertPoint(vport, 0, 0, comp);
    
    				//setting new rectangle to view
    				rect.setRect(ptZero.x-move.x, ptZero.y-move.y, w, h);
    
    				//viewing of new rectangle
    				comp.scrollRectToVisible(rect);
    			}
    		}
    	}
    }
    Use:
    Java Code:
    long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
    Toolkit.getDefaultToolkit()
    	.addAWTEventListener( new MyAlmightyListener(jScrollPane1.getViewport(), myDesktopPane), eventMask);
    It works fine.

    When I was trying to use the GlassPane, I had problems with redispatching the mouse events to other components. It didn't work correctly in my case and I was out of depth (Does it mean "helpless"? ).

    And about my new problem: I have decided, that I will start a new thread for it, because it doesn't relate with this topic. And I will mark this thread as SOLVED.
    Last edited by Taiko; 01-17-2010 at 10:18 AM. Reason: mistakes

Similar Threads

  1. moving a file
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 09:44 AM
  2. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 01:29 PM
  3. Replies: 2
    Last Post: 05-30-2009, 11:42 AM
  4. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 05:07 PM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •