Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By jim829

Thread: how to drag a jLabel correctly across a JFrame?

  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default how to drag a jLabel correctly across a JFrame?

    Hi. I am currently trying to make a method that will enable a jLabel to be dragged across the screen. I want to be able to drag the object pretty much anywhere on the screen(I hope to eventually make a destination for it where it can be dropped but I will get to that later). I have looked on various places on the internet on how to achieve this and the most simple solution does something like this:

    Java Code:
    jLabel6.setText("jLabel6");
            jLabel6.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
    public void mouseDragged(java.awt.event.MouseEvent evt) {
                    jLabel6MouseMoved(evt);
                }
            });
    
    private void jLabel6MouseDragged(java.awt.event.MouseEvent evt) {                                     
      
        x = evt.getX();
        y = evt.getY();
        jLabel6.setLocation(x, y);
        repaint();
          
        }
    This actually works somewhat but it is very faulty. For example the jLabel keeps appearing and reappearing as I drag it. Furthermore when I drag it I can see a duplicate Jlabel being dragged as well in the top left hand screen. So how might I fix this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: how to drag a jLabel correctly across a JFrame?

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to drag a jLabel correctly across a JFrame?

    The setLocation( ... ) method of the JLabel works in terms of its parents coordinates system, while the MouseEvent reports coordinates in terms of that JLabel itself; you have to find the coordinates in terms of the parents' coordinate system yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to drag a jLabel correctly across a JFrame?

    The SwingUtilities class has some methods that might be of interest to you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: how to drag a jLabel correctly across a JFrame?

    How might I access the Swing Utilities library. I remember the import for everything was like java. something *

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to drag a jLabel correctly across a JFrame?

    Java Code:
    import javax.swing.SwingUtilities;
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: how to drag a jLabel correctly across a JFrame?

    Also, if x = evt.getX(); is getting the location of the jLabel, then why can't I say something like x=this.getX() to get the coordinates of the the Jframe?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: how to drag a jLabel correctly across a JFrame?

    Did you try it? Print it out to see what value it is.

    something like x=this.getX() to get the coordinates of the the Jframe?
    The object that 'this' refers to needs to have a getX() method.

    Read the API doc for the JFrame class (and the classes it extends) to see what methods could be useful.
    Last edited by Norm; 05-27-2014 at 09:23 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to drag a jLabel correctly across a JFrame?

    Quote Originally Posted by Snake006 View Post
    Also, if x = evt.getX(); is getting the location of the jLabel, then why can't I say something like x=this.getX() to get the coordinates of the the Jframe?
    First, that is not the location of the JLabel. It is the location of your mouse within the JLabel component. And when you reference getX(), that is a method of the MouseEvent class, not of the JLabel class or any other component class (at least, not the getX() you are expecting).

    The location of the JLabel for an instance label is label.getLocation().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: how to drag a jLabel correctly across a JFrame?

    That makes sense. So when I use x = evt.getX(); I'm actually getting the location of the mouse. Now I just need to fix the dragging so the jLabel stops disappearing and reappearing when I drag it

  11. #11
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: how to drag a jLabel correctly across a JFrame?

    Any ideas of how to fix that?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: how to drag a jLabel correctly across a JFrame?

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to drag a jLabel correctly across a JFrame?

    It's hard to say what the problem is without seeing the code. But you do not have to issue a repaint when changing the location.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: how to drag a jLabel correctly across a JFrame?

    Just copy and paste this in netbeans or something and run it. I made a quick sample to demonstrate my problem:
    Java Code:
    package dragtest;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    /**
     *
     * @author Snake
     */
    public class DragTest  {
        /**
         * @param args the command line arguments
         */
        int x;
        int y;
        JLabel jLabel6=new JLabel();
        JFrame frame=new JFrame();
        JButton button=new JButton();
        
        public void drag(){
            frame.setSize(800, 800);
            jLabel6=new JLabel();
            jLabel6.setText("jLabel6");
            button.setText("Exit");
            frame.add(jLabel6);
            frame.add(button,BorderLayout.SOUTH);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(1);
                }
            });
            frame.setVisible(true);
            
            jLabel6.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseDragged(java.awt.event.MouseEvent evt) {
                    jLabel6MouseDragged(evt);
                }
            });
        }
        
        private void jLabel6MouseDragged(java.awt.event.MouseEvent evt) {                                     
        x = evt.getX();
        y = evt.getY();
        jLabel6.setLocation(x, y);
        }  
        public static void main(String[] args) {
            // TODO code application logic here
            DragTest test=new DragTest();
            test.drag();
        }
    }
    FYI how can I just upload the actual project so you all can just run it?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: how to drag a jLabel correctly across a JFrame?

    To see what is happening, try some debugging. Add a println() after the call to setVisible(true) that prints out the value of jLable6.getBounds() and also add one another one that prints the same thing after the call to setLocation().
    The printed values will help you see some of what is happening.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: how to drag a jLabel correctly across a JFrame?

    Here is an unrelated tip that will simplify your code. If you do the following in your constructor:

    Java Code:
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    You can get rid of you exit button and actionListener. This will allow you to close the window and exit the program
    when you click the red X in the upper right corner of the JFrame.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Drag JLabel(ImageIcon)
    By tzeus in forum New To Java
    Replies: 1
    Last Post: 12-13-2013, 03:48 PM
  2. Replies: 27
    Last Post: 11-15-2013, 08:38 PM
  3. Make a JLabel (and obviously how to add it to a JFrame...)
    By watachiaieto in forum AWT / Swing
    Replies: 6
    Last Post: 05-23-2012, 10:18 AM
  4. Cannot refresh a JLabel on a JFrame
    By carnotan in forum AWT / Swing
    Replies: 11
    Last Post: 03-25-2011, 04:02 PM
  5. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 08:44 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
  •