Drag and Drop in JPanel having GridBagLayout

Hi,
I have a JPanel (longitudinal) which has labels with imageicons (actually they are created using javax.media package and other JAI packages, but for simplicity sake labels are good). These labels are arranged longitudinally one below the other.
I have to implement drag and drop for these labels. I should be able to change the order etc. of the labels using DnD. I have been able to do that successfully. From the mouse coordinattes when the mouse is released, i calculated the grid on the layout, and then add the component to the panel in the grid.
All is well, but it doesnt end well :-(
The problem I am having is that when i releaase the mouse button, the component gets added on top of the component that is already there in that grid.
Is there a way that they can adjust positions, and the other labels move one grid down when another one is added in the same grid??
I would really appreciate any help. I did a lot of work to come to this level, and now i have just run out of ideas.
Thanks a lot in advance!!
Sri.

Hi,
I have a JPanel (longitudinal) which has labels with imageicons (actually they are created using javax.media package and other JAI packages, but for simplicity sake labels are good). These labels are arranged longitudinally one below the other.
I have to implement drag and drop for these labels. I should be able to change the order etc. of the labels using DnD. I have been able to do that successfully. From the mouse coordinattes when the mouse is released, i calculated the grid on the layout, and then add the component to the panel in the grid.
All is well, but it doesnt end well :-(
The problem I am having is that when i releaase the mouse button, the component gets added on top of the component that is already there in that grid.
Is there a way that they can adjust positions, and the other labels move one grid down when another one is added in the same grid??
I would really appreciate any help. I did a lot of work to come to this level, and now i have just run out of ideas.
Thanks a lot in advance!!
Sri.

Similar Messages

  • Drag and drop with JPanel

    Hi there,
    I have JPanel which contains label and image icon and i want to drag them out to another panel and droped there.
    Is this really doable with swing.
    Please share your thoughts with me
    Thanks,
    - Prashant

    Thanks Daya,
    but I want something which gives me the feel of drag and drop, Dragging is good with your code but when i do drop and that doesn't fall in desired area, i am expecting that to go back on that's previous locaton.
    - Prashant

  • Drag and drop ALV to table control

    Hi all,
    Does anyone know if it is possible to program the drag-and-drop functionality from an ALV list to a table control? I would like to have this feature to let the user enter data on 1 screen. On the left part the user sees an ALV list and on the right the user sees the online transaction. I would like the user to enter data simply by drag and drop instead of having to enter the data.
    Does someone has a sample program for this?
    Regards,
    Kris

    Hi Kris
    I can download the programs and send them to you, but you should give me your e-mail.
    Anyway you can use Drag and Drop if you build your table control by ALV, i.e. ALV GRID.
    But I'm using 4.7 and I don't know how class CL_GUI_ALV_GRID is in your release, I believe the release 4.6B is one of first release whit this class.
    Max

  • Image drag and drop

    Hi all,
    I am writing a program, with 2 JPanels, the first one is with 4 images (JLabel), I need to drag the images and drop on second JPanel. When the image is dropped, it should also allow dragging to desired position in that JPanel(through MouseMotionListener).
    I read a lot of examples about DnD, TransferHandler, I knew how to implement drag and drop between JPanels, but still have no idea how to implement both. Can anyone advise a brief idea how can I do.
    Thank you so much....

    Read this: http://forum.java.sun.com/thread.jsp?forum=57&thread=516990
    It shouldn't be a big streach to convert the panel to an image.
    If you have problems after trying this out, repost.

  • Outlook 2010 with 365 stripping attachments off of email that have been drag and dropped

    In my office we just switched to 365 and since this I've noticed that roughly 1/3 to 1/2 of my email with drag and drop attachments are having those attachments stripped off.
    Several times a day I send an email that has 6 attachments, mix of excel and pdf, and the excel called 'Checklist' I attach by drag and drop often get stripped off the instant I hit the Send button.  
    I will literally be looking at it, as one of 6 files, hit Send, and in my sent box there are only 5 files, the Checklist is gone!  It's random; about 1/3 to 1/2 of them do this.  Others make it through.
    Please let me know if there is a fix to this?  I'm having to resend attachments about 6 times a day now! 

    Hi,
    Please send several test emails with attachments on OWA, check if this issue will occur.
    If the issue is on client only, we can start Outlook in Safe Mode to test, this can eliminate the influence by 3rd-party add-ins:
    Press Win + R, type "outlook.exe /safe" in the blank box, press Enter.
    In Outlook Safe Mode, send several test emails with attachments to see if the problem will ever occur. If no, we may suspect the issue was caused by some add-ins, go to FILE -> Options -> Add-Ins to disable the suspicious add-ins to verify which one
    caused the problem.
    We can also test with a new profile to setup the account, a new profile will be a new environment for the account, we can check if the problem will occur with the new profile and you don't need to remove the old profile.
    To create a new profile, go to Control Panel -> Mail -> Show Profiles -> Add.
    Regards,
    Melon Chen
    TechNet Community Support
    It's recommended to download and install
    Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office
    programs.

  • Implementing Drag and Drop

    I would buy this product if the ajax features somehow
    supported the implementaion of drag and drop in a way that was
    simple to use.
    I am dabbling in vb.net/asp.net and its version of ajax
    tools. The support for drag and drop are not all that good at this
    time.
    I have even bought a book that had a chapter on it. It still
    takes too much java hand written code to impliment it.
    I also own dreamweaver MX. I use this product to do all my
    web sites. They all are home stuff and fun stuff. My wife wants her
    site to have the user be able to grab an object and drop it on a
    specific location and have an event occur. In her case she wants an
    animated GIF to fire.
    I have turned to vb.net because i am a programmer and use
    vb.net for application development. But!!! i am excited about DW
    cs3. So my question is!!
    Does DW CS3 Ajax tools provide a somewhat easy way to
    impliment drag and drop without me having to write to much Java by
    hand?
    Thanx up front
    jerryCic

    >I would buy this product if the ajax features somehow
    supported the
    > implementaion of drag and drop in a way that was simple
    to use.
    Drag and Drop interfaces in javascript are nothing simple.
    Don't expect any
    magic wizards to help you with this.
    There are a lot of AJAX/JS libraries. Many have DnD featuers,
    but they'll
    differ from library to library. you're going ot have to do a
    lot of legwork
    and takea look at them all.
    I've used Scriptaculous in the past, but have found it
    limiting. I'm going
    to be looking at Jquery UI next.
    > Does DW CS3 Ajax tools provide a somewhat easy way to
    impliment drag and
    > drop
    > without me having to write to much Java by hand?
    Java is not involved. JAVASCRIPT is.
    DW does not do .net, so you you're going to do .net, you
    should look at
    Visual Studio as your main development IDE.
    _Darrel

  • Weebly drag and drop editor will not load

    I use ipage for my host. When trying to edit my site using weebly drag and drop I am having an issue. I login to ipage, goto control panel, click weebly drag and drop, then click edit site. It brings up a blank page! I have Firefox 3.6.17. I downgraded from the latest version (had the same problem), because Norton plugins were not compatible with the latest version.

    Nothing seems to work!!  PSE 8 is supposed to be plug and play without having to mess with anything, get tech support or waste my time.  Why then have I spent 3 evenings trying to make it work?   I called Wal Mart who have a no refund policy on software.  I HAVE HAD IT WITH THIS NONSENSE!!!
    Please provide the link to get a refund from ADOBE.

  • Why am I having trouble importing vCards? I follow the Help Center instructions by dragging and dropping, but some don't sync into the cloud. And why can't I drag multiple vCards? I have over 4,000. Please tell me I don't have to do one at a time?

    Why am I having trouble importing vCards? I follow the Help Center instructions by dragging and dropping, but some don't sync into the cloud. And why can't I drag multiple vCards? I have over 4,000. Please tell me I don't have to do one at a time?

    carolinechx wrote:
    i know the description may be a little bit too confusing
    Mostly because you are not using any capital letters or paragraph returns and your post is difficult to read.

  • How to perform a Drag and Drop operation on JButton in a JPanel?

    Hi guys,
    There is a requirement to perform Drag and Drop operation on JButton component added to a JPanel Componet.
    I could not get a proper solition to move the JButton component inside a JPanel component by drag and drop.
    I request every one to provide some related information on moving a JButton component using Mouse operation.
    Rajashekar.T

    Here is some code for moving a window around the screen. The concept would be the same for moving a button around a panel:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=599181

  • Drag and drop JButtons between JPanels

    I'm trying to write an application with some simple drag and drop for JButtons between JPanels. Instead of going through all the hassle with DragSourceListener, DropTargetListener etc, I was thinking about a simple MouseMotionListener which sets a boolean to true when the user is dragging, and let the other panel add a MouseListener which listens for a MouseReleased.
    The drag part is working fine, however the drop is not; in fact, it doesn't register the release. However, when I click and release, it seems to register the release.
    Any suggestions? Thanks in advance.
    Code for Card:
    public Card extends JButton {
    public static boolean drag = false;
         public Card(ImageIcon icon, ImageIcon fullicon)
              addMouseMotionListener(new DragMouseListener());
         class DragMouseListener extends MouseMotionAdapter
              public void mouseDragged(MouseEvent e)
                   System.out.println("Drag register");
                   drag = true;
    }Code for the panel:
    benchpanel.addMouseListener(new MouseAdapter() {
         public void mouseReleased(MouseEvent e)
              if (Card.drag && GUI.getInstance().findComponentAt(e.getPoint()) == GUI.getInstance().benchpanel)
                   System.out.println("Drop registered");
              Card.drag = false;
    });

    Sorry for the double post. I had some experimenting done, and I come up with the following:
    if (Card.drag && GUI.getInstance().benchpanel.getBounds().contains(e.getLocationOnScreen()))
         System.out.println("Drop registered");
    }Here's the problem: The second part of the if-statement never seems to get true; if I write the following:
    System.out.println(e.getPoint());
    System.out.println(benchpanel.getBounds());
    System.out.println(benchpanel.getBounds().contains(e.getPoint()));and i click in the upper left part of the JPanel, i get the following output:
    java.awt.Point[x=9,y=13]
    java.awt.Rectangle[x=210,y=420,width=400,height=108]
    false- so for some reason, the bounds on the JPanel are totally messed up - any ideas on why?
    Edited by: pg-robban on Apr 14, 2009 3:14 AM

  • Which Listener I should use if I drag and Drop an image/icon into a JPanel?

    Dar Friends:
    Happy new year.
    I try to drag and Drop an image/icon into a JPanel, and hope I can immediately detect it after DND,
    Which Listener I should use in this JPanel if I drag and Drop an image/icon into a JPanel??
    Thanks

    Thank camickr .
    I can dnd an image into a JPanel called JPanelOld already, I hope to use another JPanel or JTree to listen to any Dropped Image in JPanelOld later on so I can take some action in another JPanel or JTree.
    so what kind of Listener I should use for my purpose??
    where to add this Listeners??
    Happy New Year.

  • How to drag and drop an Image between two JPanels inside a Split Pane

    I'm tring to do that, and my actual problem is as follows:
    I drag the Image from the bottomPanel but I can't drop it in the topPanel, I'm using this classes:
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.awt.dnd.*;
    import java.awt.datatransfer.*;
    import java.awt.image.*;
    public class MoveableComponentsContainer extends JSplitPane {     
         public DragSource dragSource;
         public DropTarget dropTarget;
    public JPanel topPanel = new JPanel();
    public JPanel bottomPanel = new JPanel();
         public int ancho;
    public int alto;
    public MoveableLabel lab1,lab2,lab3,lab4;
    public Icon ico1, ico2,ico3,ico4;
    private static BufferedImage buffImage = null; //buff image
         private static Point cursorPoint = new Point();
    public int getMaximumDividerLocation() {
    return ( ( int ) ( alto * .85 ) );
    public int getMinimumDividerLocation() {
    return( ( int ) ( alto * .85 ) );
         public MoveableComponentsContainer(int Weight, int Height ) {
    alto = Height;
    ancho = Weight;
    setOrientation( VERTICAL_SPLIT);
    setDividerSize(2);
    getMaximumDividerLocation();
    getMinimumDividerLocation();
    setPreferredSize(new Dimension( (int) ( Weight * .99 ), (int) ( Height * .94 ) ) );
    setDividerLocation( getMaximumDividerLocation() );
    System.out.println( " getDividerLocation() = " + getDividerLocation() );
    topPanel.setName("topPanel");
    bottomPanel.setName("bottomPanel");
    bottomPanel.setPreferredSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .15 ) ) );
    bottomPanel.setMaximumSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .15 ) ) );
    bottomPanel.setMinimumSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .15 ) ) );
    topPanel. setPreferredSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .85 ) ) );
    topPanel. setMaximumSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .85 ) ) );
    topPanel. setMinimumSize( new Dimension( (int) ( Weight * .99 ), (int) ( ( Height * .94 ) * .85 ) ) );
    bottomPanel.setEnabled(true);
    bottomPanel.setVisible(true);
    bottomPanel.setBackground( new Color( 57,76,123 ) );
    topPanel. setEnabled(true);
    topPanel. setVisible(true);
    topPanel. setBackground( new Color( 57,76,123 ) );
    setBottomComponent( bottomPanel );
    setTopComponent( topPanel );
    setOneTouchExpandable( false );
    bottomPanel.setBorder(BorderFactory.createTitledBorder("Drag and Drop Test"));
              setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.white, Color.gray));
    bottomPanel.setLayout( new FlowLayout() );
    topPanel.setLayout(new FlowLayout());
              addMoveableComponents();
         private void addMoveableComponents() {
    ico1 = new ImageIcon( "/usr/local/installers/java/lll/DnD/switchm.gif");
    lab1 = new MoveableLabel("Centrales", ico1, topPanel );
    lab1.setName("labelOne");
    bottomPanel.add( lab1 );
              lab2 = new MoveableLabel("Destinos", ico1, topPanel);
    lab2.setName("labelTwo");
              bottomPanel.add( lab2 );
              lab3 = new MoveableLabel("Registros", ico1, topPanel );
    lab3.setName("labelThree");
              bottomPanel.add( lab3 );
              lab4 = new MoveableLabel("Parametros", ico1, topPanel);
    lab4.setName("labelFour");
              bottomPanel.add( lab4 );
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.awt.dnd.*;
    import java.awt.datatransfer.*;
    public class MoveableLabel extends JLabel implements Transferable {
    final static int FILE = 0;
    final static int STRING = 1;
    final static int IMAGE = 2;
    DataFlavor flavors[] = { DataFlavor.javaFileListFlavor,
    DataFlavor.stringFlavor,
    DataFlavor.imageFlavor };
    public JPanel PanelDestino;
    public JPanel PanelOrigen;
    public DropTarget dropTarget;
    public DropTargetListener dropTargetLis;
         private static final Border border = BorderFactory.createLineBorder(Color.black, 1);
         public MoveableLabel(String text, Icon ic, JPanel DestPanel) {
              super( text, ic, TRAILING);
    PanelDestino = DestPanel;
              MouseEventForwarder forwarder = new MouseEventForwarder();
              addMouseListener(forwarder);
              addMouseMotionListener(forwarder);
              setBorder(border);
              setBounds(0,0,50,100);
              setOpaque(true);
    setTransferHandler(new TransferHandler("text"));
    setBackground( new Color( 57,76,123 ) );
    public synchronized DataFlavor[] getTransferDataFlavors() {
         return flavors;
    public boolean isDataFlavorSupported(DataFlavor flavor) {
    boolean b = false;
    b |= flavor.equals(flavors[ FILE ]);
    b |= flavor.equals(flavors[STRING]);
    b |= flavor.equals(flavors[ IMAGE]);
    return (b);
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, java.io.IOException {
    return this;
         final class MouseEventForwarder extends MouseInputAdapter {
              public void mousePressed(MouseEvent e) {
                   Container parent = getParent();
    Container brother = (Container)PanelDestino;
    System.out.println( "Parent 1 = " + parent.getName() );
    System.out.println( "Destino 1 = " + brother.getName() );
    JComponent c = (JComponent) e.getSource();
    System.out.println( "getsource() = " + c.getName() );
    TransferHandler th = c.getTransferHandler();
    th.exportAsDrag( c, e, TransferHandler.COPY_OR_MOVE );
    dropTarget = getDropTarget();
    System.out.println( "dropTarget.getComponent().getName() = " + dropTarget.getComponent().getName() );
    for ( int a=0; a < parent.getComponentCount(); a++ ) {
    parent.getComponent( a ).setEnabled( false );
    brother.setDropTarget( dropTarget );
    System.out.println( "dropTarget.getComponent().getName() = " + dropTarget.getComponent().getName() );
              public void mouseReleased(MouseEvent e) {
                   Container parent = getParent();
    Container brother = PanelDestino;
    System.out.println( "Parent 2 = " + parent.getName() );
    System.out.println( "Destino 2 = " + PanelDestino.getName() );
    parent.setEnabled( true );
    brother.setEnabled( false );
    for ( int a=0; a < parent.getComponentCount(); a++ ) {
    parent.getComponent( a ).setEnabled( true );
    import java.awt.*;
    import javax.swing.*;
    public class TestDragComponent extends JFrame {
         public TestDragComponent() {
    super("TestDragComponent");
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dm = new Dimension();
    dm = tk.getScreenSize();
    System.out.println(dm.height);
    System.out.println(dm.width );
    Container cntn = getContentPane();
    cntn.setFont(new Font("Helvetica", Font.PLAIN, 14));
    cntn.add(new MoveableComponentsContainer(dm.width,dm.height));
              pack();
              setVisible(true);
         public static void main(String[] args) {
              new TestDragComponent();

    Ok I found the answer to your problem. If you download the tutorial they have the code there it's in one folder. I hope this helps.
    http://java.sun.com/docs/books/tutorial/

  • Drag and drop an image over a JPanel (only change position on same JPanel)

    Hi all,
    there is JPanel added to a JDialog.
    There is a BufferedImage on the JPanel: I need to let the image be dragged and dropped in another position over the same JPanel and read the new position's coordinates.
    (Maybe the image has to be wrapped in a JComponent?)
    Thanks in advance for any suggestions.
    Ludovico

    (Maybe the image has to be wrapped in a JComponent?)Just add the image to a JLabel and add the label to the panel.

  • Having Problem Adding to drag and drop to move furniture over a floor plan

    I have the code which follows. I need to create floor plans where web visitors can drag and drop furniture onto the floor plan.  I have  three items that work but when I convert any additional items to a symbol and select movie clip and then add more lines to the code, none will work.. How can the code determine that  my movie clip I named "couch" is going to movie clip 4.  Thank you if you can help, Frankie Foster
    Below is my code:
    var numObjects:uint = 3  // assign the total number of movieclips that will be dragged
    for(var i:uint=1; i<=numObjects; i++){
         this["movieClip_"+String(i)].addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
         this["movieClip_"+String(i)].addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    var selectedMC:MovieClip;
    movieClip_1.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_2.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_3.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_1.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    movieClip_2.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    movieClip_3.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    function clickToDrag(event:MouseEvent):void
         selectedMC = MovieClip(event.currentTarget);
         selectedMC.startDrag(); 
    function releaseToDrop(event:MouseEvent):void
         event.currentTarget.stopDrag();
    stage.addEventListener(KeyboardEvent.KEY_DOWN, nextQuestion);
    function nextQuestion(event:KeyboardEvent):void {
          //trace(event.keyCode);
          if(event.keyCode == Keyboard.RIGHT)
               selectedMC.rotation += 5;
          if(event.keyCode == Keyboard.LEFT)
               selectedMC.rotation -= 5;

    Your appear to have redundant coding assigning your listeners.  What code are you trying to add/modify to make the couch function like the others?
    this...
    for(var i:uint=1; i<=numObjects; i++){
         this["movieClip_"+String(i)].addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
         this["movieClip_"+String(i)].addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    currently does what this also does....
    movieClip_1.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_2.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_3.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    movieClip_1.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    movieClip_2.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    movieClip_3.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

  • I download firefox 5.0 and now I'm having trouble dragging and dropping PDF files into Google Docs and I can't view the PDF once uploaded. I had no problems with this in the previous verion of Firefox

    With firefox 5.0 I can't drag and drop pdf files in the google docs screen. When I do eventually get a pdf file uploaded I can't view it. I get "no preview available". In my previous verion of firefox (4.something) I had no problems with this and I was able to drag and drop and after uploaded I could view the entire file.

    Thanks one again. I know you helped me the last to. I do have the Deja Vu font set in my folder. I downloaded and the last time. So I don't know if there is supposed to be more or not, but those were installed with Windows-XP after my reformat.
    This is really driving me nuts. as I've done several re-formats on my computer and several friends computers and even upgraded hardware so I am not necessarily a software or programming expert, I am somewhat advanced when it comes to computers in general and can usually diagnose the problem but these font's are the problem and they never were before.
    I did see in another forum that if you had a Dell Computer and did the Microsoft update for the onboard video card driver that it could be the culprit, but I have that disabled being I have a much better graphics card so the updates from Microsoft for the onboard video weren't installed.
    It' a Dell Dimension 3000 with an Intel(R) 82865G Graphics Controller so I don't believe it is the culprit being it is disabled. The actual video card that I am using is a PCI Nvidia GeForce 9400 GT with 1 GB Memory. And I didn't have any problem before.
    I appreciate the help. But still can't get things to work. Live Help Chat doesn't open for another two hours so maybe they can do "Remote Access" or something and can check setting and help me. But if you come up with other suggestions, they would be greatly appreciated.
    Thanks again.
    Susan

Maybe you are looking for