Results 1 to 2 of 2
  1. #1
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Drag JLabel(ImageIcon)

    Hello,
    I am trying to drag and drop a JLabel inside a JPanel, just to change it's location via mouse.
    It seems that my code is all wrong.
    what is missing?
    Java Code:
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.TransferHandler;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    
    
    public class example {
    
    	public static void main(String args[]){
    			JFrame baza = new JFrame("Baza");
    			
    			JLabel jLabel = new JLabel();
    			jLabel = new JLabel(new ImageIcon("xls.jpg"));
    			int coordx = (int) (Math.random() * 696);
    			int coordy = (int) (Math.random() * 496);
    			jLabel.setLocation(coordx, coordy);
    			jLabel.setSize(new Dimension(200,200));
    			jLabel.setPreferredSize(new Dimension(200,200));
    			final String propertyName = "text";
    			jLabel.setTransferHandler(new TransferHandler(propertyName));
    			jLabel.addMouseListener(new MouseAdapter(){
    				
    				public void mousePressed(MouseEvent evt) {
    					JComponent component = (JComponent) evt.getSource();
    					TransferHandler th = component.getTransferHandler();
    					
    					th.exportAsDrag(component, evt, TransferHandler.COPY);
    					
    				}
    			});
    			
    			
    			JPanel panel_1 = new JPanel();
    			panel_1.add(jLabel);
    			baza.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			baza.add(panel_1);
    			baza.setSize(new Dimension(300,300));
    			panel_1.setSize(new Dimension(300,300));
    			baza.setPreferredSize(new Dimension(300,300));
    			panel_1.setPreferredSize(new Dimension(300,300));
    			baza.pack();
    			baza.setVisible(true);
    		
    	}
    	
    }
    I should explain perhaps...
    When i click and hold the JLabel, is shows me a + sign, and if i try to drag it around in the JPanel, is show me a "deny" sign(like it's not a valid location). If i drag it on top of let's say another JLabel it show the + sign, but when i release the mouse click, the JLabel is still in the original location.
    Last edited by tzeus; 12-12-2013 at 11:48 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Drag JLabel(ImageIcon)

    If you really want to do this, you need to use a null layout and specify the location of the component when the mouse is dragged. Drag and drop is used for transferring data, not moving components.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Using ImageIcon
    By David1234204 in forum AWT / Swing
    Replies: 4
    Last Post: 03-21-2012, 08:14 PM
  2. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 10:10 PM
  3. Replies: 5
    Last Post: 12-04-2010, 12:30 AM
  4. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 PM
  5. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 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
  •