Results 1 to 4 of 4
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default Drag and Drop JPanel at Runtime

    See i have a frame and it contains multiple panels

    These panels contain buttons

    Now i want to add a feature in my frame that the user can change the position of the panels by dragging them anywhere in the frame like JTable columns can change their position

    So can anybody help me please

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

    Default

    Sounds like a job for JDesktopPane and JInternalFrame.
    See How to Use Internal Frames.

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

    Default

    Try something like this:

    Java Code:
    public class MyMotionListener implements MouseMotionListener {
    	private JPanel movingPanel;
    	private Point pt;
    	public MyMotionListener (JPanel movingPanel) {
    		this.movingPanel = movingPanel;
    	}
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		pt = SwingUtilities.convertPoint(movingPanel, e.getX(), e.getY(), movingPanel.getParent());
    		movingPanel.setBounds(pt.x, pt.y, movingPanel.getWidth(), mowingPanel.getHeight());
    	}
    	
    	@Override
    	public void mouseMoved(MouseEvent e) {
    	}
    }
    And use:
    Java Code:
    JPanel somePanel = new JPanel();
    ...
    JPanel temp = somePanel;
    somePanel.addMouseMotionListener(new MyMotionListener(temp));
    I hope, that I wrote it without mistakes :o

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You would probably be better off with a MouseAdapter so you could capture the MouseListener events as well (i.e., mousePressed and mouseReleased), and then add an object of this class as both the JPanel's MouseListener and MouseMotionListener. Also, the JPanel's layout would need to be either null, or perhaps you could use a JLayeredPane.

Similar Threads

  1. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 01:01 PM
  2. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 09:18 AM
  3. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  4. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM
  5. help drag and drop in JTabbedPane
    By RO86 in forum AWT / Swing
    Replies: 0
    Last Post: 08-14-2007, 01:22 PM

Posting Permissions

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