Results 1 to 3 of 3
  1. #1
    punitjain is offline Banned
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Drag and Drop problem...

    Here is my code:

    AbstractDropManager.java
    Java Code:
    import java.awt.Point;
    import java.awt.Rectangle;
    
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    
    public abstract class AbstractDropManager implements DropListener {
    	protected JComponent component;
    
    	public AbstractDropManager() {
    		this(null);
    	}
    	
    	public AbstractDropManager(JComponent component) {
    		this.component = component;
    	}
    
    	protected Point getTranslatedPoint(Point point) {
            Point p = (Point) point.clone();
            SwingUtilities.convertPointFromScreen(p, component);
    		return p;
    	}
    
    	protected boolean isInTarget(Point point) {
    		Rectangle bounds = component.getBounds();
    		return bounds.contains(point);
    	}
    
    	public void Dropped(DropEvent e) {
    	}
    }
    
    
    [U]ComponentAdapter.java[/U]
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    
    import javax.swing.SwingUtilities;
    
    public class ComponentAdapter extends DropAdapter
    {
        public ComponentAdapter(GlassPane glassPane, String action) {
            super(glassPane, action);
        }
    
        public void mousePressed(MouseEvent e)
        {
            Component c = e.getComponent();
    
            BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            c.paint(g);
    
            glassPane.setVisible(true);
    
            Point p = (Point) e.getPoint().clone();
            SwingUtilities.convertPointToScreen(p, c);
            SwingUtilities.convertPointFromScreen(p, glassPane);
    
            glassPane.setPoint(p);
            glassPane.setImage(image);
            glassPane.repaint();
        }
    
        public void mouseReleased(MouseEvent e)
        {
            Component c = e.getComponent();
    
            Point p = (Point) e.getPoint().clone();
            SwingUtilities.convertPointToScreen(p, c);
    
            Point eventPoint = (Point) p.clone();
            SwingUtilities.convertPointFromScreen(p, glassPane);
    
            glassPane.setPoint(p);
            glassPane.setVisible(false);
            glassPane.setImage(null);
    
            fireDropEvent(new DropEvent(action, eventPoint));
        }
    }
    
    
    
    [U]DragnDemo.java[/U]
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    
    public class DragnDemo extends JFrame{
        private GlassPane glassPane;
    
        public DragnDemo ()
        {
    	super ("Welcome");
    	setDefaultCloseOperation (EXIT_ON_CLOSE);
    
    	glassPane = new GlassPane ();
    	setGlassPane (glassPane);
    	//JPanel c=new JPanel(new BorderLayout());
    
    	JTextField txtField = new JTextField (20);
    
    	DropListener listener = new DropManagerDemo (txtField,txtField);
    
    	ComponentAdapter componentAdapter;
    	
    	Container c = getContentPane ();
    	c.setLayout (new BorderLayout ());
    
    
    	JPanel buttons = new JPanel ();
    	JLabel lbl,lbl2,lbl3,lbl4;
    
    
    	
    
    	lbl=new JLabel("Answer2");
    	String message2=lbl.getText();
    
    
    	buttons.add (lbl);
    	lbl.addMouseListener (componentAdapter = new ComponentAdapter (glassPane, message2));
    	componentAdapter.addDropListener (listener);
    	lbl.addMouseMotionListener (new MotionAdapter (glassPane));
    
    
    
    	lbl2=new JLabel("Answer3");
    	String message3=lbl2.getText();
    
    
    	buttons.add (lbl2);
    	lbl2.addMouseListener (componentAdapter = new ComponentAdapter (glassPane, message3));
    	componentAdapter.addDropListener (listener);
    	lbl2.addMouseMotionListener (new MotionAdapter (glassPane));
    
    
    	lbl3=new JLabel("Answer4");
    	String message4=lbl2.getText();
    
    
    	buttons.add (lbl3);
    	lbl3.addMouseListener (componentAdapter = new ComponentAdapter (glassPane, message4));
    	componentAdapter.addDropListener (listener);
    	lbl3.addMouseMotionListener (new MotionAdapter (glassPane));
    
    
    	lbl4=new JLabel("Answer5");
    	String message5=lbl4.getText();
    
    
    	buttons.add (lbl4);
    	lbl4.addMouseListener (componentAdapter = new ComponentAdapter (glassPane, message5));
    	componentAdapter.addDropListener (listener);
    	lbl4.addMouseMotionListener (new MotionAdapter (glassPane));
    
    
    
    
    
    	DropManagerDemo obj = new DropManagerDemo (glassPane,txtField);
    
    	c.add (BorderLayout.SOUTH, buttons);
    
    	JScrollPane scrollPane = new JScrollPane (txtField);
    	scrollPane.addMouseListener (new ComponentAdapter (glassPane, "Textfield"));
    	scrollPane.addMouseMotionListener (new MotionAdapter (glassPane));
    
    
    	c.add (BorderLayout.CENTER, scrollPane);
    
    	JPanel headerPanel = new JPanel (new BorderLayout ());
    	HeaderPanel header = new HeaderPanel ();
    	headerPanel.add (BorderLayout.NORTH, header);
    	headerPanel.add (BorderLayout.SOUTH, new JSeparator (JSeparator.HORIZONTAL));
    	headerPanel.setBorder (new EmptyBorder (0, 0, 6, 0));
    	c.add (BorderLayout.NORTH, headerPanel);
    
    
    	pack ();
    	setResizable (false);
    	setLocationRelativeTo (null);
        }
    
    
        public static void main (String [] args)
        {
    	DragnDemo demo = new DragnDemo ();
    	demo.setVisible (true);
        }
    }
    
    
    
    
    [U]DropAdapter.java[/U]
    import java.awt.event.MouseAdapter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Iterator;
    
    public class DropAdapter extends MouseAdapter {
        protected GlassPane glassPane;
    	protected String action;
    
    	private List listeners;
    
        public DropAdapter(GlassPane glassPane, String action) {
            this.glassPane = glassPane;
            this.action = action;
            this.listeners = new ArrayList();
        }
    
        public void addDropListener(DropListener listener) {
            if (listener != null)
                listeners.add(listener);
        }
    
        public void removeDropListener(DropListener listener) {
            if (listener != null)
                listeners.remove(listener);
        }
    
        protected void fireDropEvent(DropEvent evt) {
            Iterator it = listeners.iterator();
            while (it.hasNext()) {
            	((DropListener) it.next()).Dropped(evt);
            }
        }
    }
    
    
    [U]DropEvent.java[/U]
    import java.awt.Point;
    
    public class DropEvent {
    	private Point point;
    	private String action;
    
    	public DropEvent(String action, Point point) {
    		this.action = action;
    		this.point = point;
    	}
    
    	public String getAction() {
    		return action;
    	}
    
    	public Point getDropLocation() {
    		return point;
    	}
    }
    
    
    
    [U]DropListener.java[/U]
    public interface DropListener 
    {
    	public void Dropped(DropEvent e);
    }
    
    
    [U]DropManagerDemo.java[/U]
    import java.awt.Point;
    import javax.swing.JComponent;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    
    public class DropManagerDemo extends AbstractDropManager {
        private JComponent target;
        JTextField testF;
    
        public DropManagerDemo(JComponent target,JTextField tF) {
    	super(target);
    	testF = tF;
        }
    
    	public void Dropped(DropEvent e) {
    	   String action = e.getAction();
    	   Point p = getTranslatedPoint(e.getDropLocation());
    
    	   if (isInTarget(p)) 
    	   {
    	       testF.setText(action);
    	   }
    	}
    }
    
    
    [U]GlassPane.java[/U]
    import java.awt.AlphaComposite;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JPanel;
    
    public class GlassPane extends JPanel
    {
    	private AlphaComposite composite;
        private BufferedImage dragged = null;
        private Point location = new Point(0, 0);
    
        public GlassPane()
        {
            setOpaque(false);
            composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
        }
    
        public void setImage(BufferedImage dragged)
        {
            this.dragged = dragged;
        }
    
        public void setPoint(Point location)
        {
            this.location = location;
        }
    
        public void paintComponent(Graphics g)
        {
            if (dragged == null)
                return;
    
            Graphics2D g2 = (Graphics2D) g;
            g2.setComposite(composite);
            g2.drawImage(dragged,
                         (int) (location.getX() - (dragged.getWidth(this)  / 2)),
                         (int) (location.getY() - (dragged.getHeight(this) / 2)),
                         null);
        }
    }
    
    
    
    
    [U]HeaderPanel.java[/U]
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Paint;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.border.EmptyBorder;
    
    public class HeaderPanel extends JPanel {
    
        public HeaderPanel() {
    	
    	JPanel titres = new JPanel(new GridLayout(3, 1));
    	titres.setOpaque(false);
    	titres.setBorder(new EmptyBorder(12, 0, 12, 0));
    
    	JLabel titreEnTete = new JLabel("Welcome");
    	Font police = titreEnTete.getFont().deriveFont(Font.BOLD);
    	titreEnTete.setFont(police);
    	titreEnTete.setBorder(new EmptyBorder(0, 12, 0, 0));
    	titres.add(titreEnTete);
    
    	JLabel titre;
    	titres.add(titre = new JLabel("What is a class"));
    	police = titreEnTete.getFont().deriveFont(Font.PLAIN);
    	titre.setFont(police);
    	titre.setBorder(new EmptyBorder(0, 12, 0, 0));
    	
    
    	add(BorderLayout.WEST, titres);
    	add(BorderLayout.EAST, titre);
        }
    }
    
    
    [U]MotionAdapter.java[/U]
    import java.awt.Component;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.SwingUtilities;
    
    public class MotionAdapter extends MouseMotionAdapter
    {
        private GlassPane glassPane;
    
    	public MotionAdapter(GlassPane glassPane) {
    		this.glassPane = glassPane;
    	}
    
    	public void mouseDragged(MouseEvent e)
        {
            Component c = e.getComponent();
    
            Point p = (Point) e.getPoint().clone();
            SwingUtilities.convertPointToScreen(p, c);
            SwingUtilities.convertPointFromScreen(p, glassPane);
            glassPane.setPoint(p);
    
            glassPane.repaint();
        }
    }
    please run the DrannDemo.java class.
    What i am doing:-
    here in two text field and some labels, dragging the labels into text fields is happing here, dat is what i want, but the problem is when i m adding new TextFields here, i m nt be able to drop the labels into that newly added text field..

    Initally there was one text field and i added one more nd i make the changes in DropManagerDemo.java , earlier DropManagerDemo.java was like this:-

    Java Code:
    import java.awt.Point;
    import javax.swing.JComponent;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    
    public class DropManagerDemo extends AbstractDropManager {
        private JComponent target;
        JTextField testF;
    
        public DropManagerDemo(JComponent target,JTextField tF) {
    	super(target);
    	testF = tF;
        }
    
    	public void Dropped(DropEvent e) {
    	   String action = e.getAction();
    	   Point p = getTranslatedPoint(e.getDropLocation());
    
    	   if (isInTarget(p)) 
    	   {
    	       testF.setText(action);
    	   }
    	}
    }
    i add one more textfield in DropMangerDemo constructer, and add an else part, as shown in my earlier DropManagerDemo.java class now it's working but only for two text fields.

    Question:

    What can i do to add more TextFields???
    please help me to figure out this as soon as possble..
    i m also attaching a zip file(Containing all the files) with my thread if anyone don't want to copy and paste all the code from here.

    Thanks...
    Attached Files Attached Files
    Last edited by Norm; 09-21-2011 at 03:12 PM. Reason: added code tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Drag and Drop problem...

    Quote Originally Posted by punitjain View Post
    Here is my code:
    Really? to me it looks like it was lifted from geometrees - geometrees game class - Google Project Hosting

    Oh, and cross posted: Drag and Drop problem.. (Swing / AWT / SWT / JFace forum at JavaRanch)

    db

  3. #3
    punitjain is offline Banned
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Drag and Drop problem...

    Yes it is, nd i have also posted this thread on code ranch, i will be appreciable if u help me in this...

Similar Threads

  1. Need help with drag and drop
    By NixasMuraki in forum New To Java
    Replies: 2
    Last Post: 06-07-2011, 02:55 AM
  2. I have problem in drag and drop of component
    By Vin in forum New To Java
    Replies: 0
    Last Post: 02-03-2010, 12:53 PM
  3. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM
  4. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 04:04 PM
  5. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 03:10 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
  •