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

    Default Drag JLabel(ImageIcon)

    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();
    			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));
    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-13-2013 at 12:48 AM.

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

    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 - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

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

Posting Permissions

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