Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3974166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:32:10+00:00 2026-05-20T04:32:10+00:00

I have class JLabelExtended, which extends class javax.swing.JLabel. I extend it, because I want

  • 0

I have class JLabelExtended, which extends class javax.swing.JLabel.
I extend it, because I want to add property dragging using mouse.
Here is my code:

public class JLabelExtended extends JLabel {
private MouseMotionAdapter mouseMotionAdapter;

private JLabelExtended jLabelExtended;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println(e.getX() + "  :   " + e.getY());
            jLabelExtended.setLocation(e.getX(), e.getY()
            );
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
}

}

This is console part after label dragged:

163  :   163
144  :   -87
163  :   162
144  :   -88
163  :   161
144  :   -89

I have several questions:

  1. Why e.getY() takes negative results?

  2. When I drag my label there are appeares copy of label which drags near my label. How can I fix it?

  3. When I drag my label, it drags very slowly.For example: when I move my cursor on 10 points my label moves only on 5 point. How can I fix it?

Thanks in advance

Here are else one way to extend JLabel:

public class LabelEasy extends JLabel {
private MouseAdapter moveMouseAdapter;
private MouseMotionAdapter mouseMotionAdapter;

private LabelEasy jLabelExtended;

private int xAdjustment, yAdjustment;
Boolean count = false;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    moveMouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                xAdjustment = e.getX();
                yAdjustment = e.getY();
            }
        }
    };

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (count) {
                System.out.println(e.getX() + "  :   " + e.getY());
                jLabelExtended.setLocation(xAdjustment + e.getX(), yAdjustment + e.getY());
                count = false;
            } else {
                count = true;
            }
            ;
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
    jLabelExtended.addMouseListener(moveMouseAdapter);
}

}

But it works like previous variant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:32:11+00:00Added an answer on May 20, 2026 at 4:32 am

    I think you’re doing it wrong. The MouseMotionListener is added to the JLabel and its location is relative to the JLabel, not the Container which holds the JLabel, so the information is useless to help you drag it. You may wish to use a MouseAdapter and add it both as a MouseListener and a MouseMotionListener. On mousePressed, get the location of the JLabel and the mouse relative to the screen and then use that for your dragging on mouseDragged. Myself, I wouldn’t extend JLabel to do this but would rather just use a regular JLabel, but that’s my preference.

    Edit: it worked better for me when I dealt with the mouse’s position relative to the screen (by calling getLocationOnScreen) and the JLabel’s position relative to its Container (by calling getLocation). For e.g.,

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class DragLabelEg {
        private static final String[] LABEL_STRINGS = { "Do", "Re", "Me", "Fa",
                "So", "La", "Ti" };
        private static final int HEIGHT = 400;
        private static final int WIDTH = 600;
        private static final Dimension MAIN_PANEL_SIZE = new Dimension(WIDTH,
                HEIGHT);
        private static final int LBL_WIDTH = 60;
        private static final int LBL_HEIGHT = 40;
        private static final Dimension LABEL_SIZE = new Dimension(LBL_WIDTH,
                LBL_HEIGHT);
        private JPanel mainPanel = new JPanel();
        private Random random = new Random();
    
        public DragLabelEg() {
            mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
            mainPanel.setLayout(null);
    
            MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
            for (int i = 0; i < LABEL_STRINGS.length; i++) {
                JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
                label.setSize(LABEL_SIZE);
                label.setOpaque(true);
                label.setLocation(random.nextInt(WIDTH - LBL_WIDTH),
                        random.nextInt(HEIGHT - LBL_HEIGHT));
                label.setBackground(new Color(150 + random.nextInt(105),
                        150 + random.nextInt(105), 150 + random.nextInt(105)));
                label.addMouseListener(myMouseAdapter);
                label.addMouseMotionListener(myMouseAdapter);
    
                mainPanel.add(label);
            }
        }
    
        public JComponent getMainPanel() {
            return mainPanel;
        }
    
        private class MyMouseAdapter extends MouseAdapter {
            private Point initLabelLocation = null;
            private Point initMouseLocationOnScreen = null;
    
            @Override
            public void mousePressed(MouseEvent e) {
                JLabel label = (JLabel)e.getSource();
                // get label's initial location relative to its container
                initLabelLocation = label.getLocation();
    
                // get Mouse's initial location relative to the screen 
                initMouseLocationOnScreen = e.getLocationOnScreen();
            }
    
            @Override
            public void mouseReleased(MouseEvent e) {
                initLabelLocation = null;
                initMouseLocationOnScreen = null;
            }
    
            @Override
            public void mouseDragged(MouseEvent e) {
                // if not dragging a JLabel
                if (initLabelLocation == null || initMouseLocationOnScreen == null) {
                    return;
                }
                JLabel label = (JLabel)e.getSource();
    
                // get mouse's new location relative to the screen
                Point mouseLocation = e.getLocationOnScreen();
    
                // and see how this differs from the initial location.
                int deltaX = mouseLocation.x - initMouseLocationOnScreen.x;
                int deltaY = mouseLocation.y - initMouseLocationOnScreen.y;
    
                // change label's position by the same difference, the "delta" vector
                int labelX = initLabelLocation.x + deltaX;
                int labelY = initLabelLocation.y + deltaY;
    
                label.setLocation(labelX, labelY);
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            JFrame frame = new JFrame("App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new DragLabelEg().getMainPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class(Customer) which holds more than 200 string variables as property. I'm using
I have Class and Student objects. Both have collection of another as property. Which
I have class that extend FragmentActivity in it I add fragment to layout as
I have Class which Extends View I'm able To Move one Image Over another
I have class method: public object MyMethod(object obj) { // I want to add
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class Meat extends Food { $var = Food::foodFunction… } I need to
We have class lua. In lua class there is a method registerFunc() which is
I have class like this below shown. which contains the shopping items where the
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.