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

  • Home
  • SEARCH
  • 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 204515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:28+00:00 2026-05-11T17:28:28+00:00

I searched the web for examples of draggable Swing components, but I found either

  • 0

I searched the web for examples of draggable Swing components,
but I found either incomplete or non-working examples.

What I need is a Swing component that can be dragged by the mouse
inside an other component. While being dragged, it should already
change
its position, not just ‘jump’ to its destination.

I would appreciate examples which work without non-standard APIs.

Thank you.

  • 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-11T17:28:29+00:00Added an answer on May 11, 2026 at 5:28 pm

    I propose a simple, but well-working solution, found out by myself 😉

    What do I do?

    • When mouse is pressed, I record the cursor’s position on screen, and
      the component’s position.
    • When mouse is dragged, I calculate the difference between new and
      old cursor’s position on screen, and move the
      component by this difference.

    Tested with latest JDK 6 unter Linux (OpenSuse, KDE3),
    but hey, it’s Java Swing, should work equally everywhere.

    Here goes the code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class MyDraggableComponent
        extends JComponent {
    
      private volatile int screenX = 0;
      private volatile int screenY = 0;
      private volatile int myX = 0;
      private volatile int myY = 0;
    
      public MyDraggableComponent() {
        setBorder(new LineBorder(Color.BLUE, 3));
        setBackground(Color.WHITE);
        setBounds(0, 0, 100, 100);
        setOpaque(false);
    
        addMouseListener(new MouseListener() {
    
          @Override
          public void mouseClicked(MouseEvent e) { }
    
          @Override
          public void mousePressed(MouseEvent e) {
            screenX = e.getXOnScreen();
            screenY = e.getYOnScreen();
    
            myX = getX();
            myY = getY();
          }
    
          @Override
          public void mouseReleased(MouseEvent e) { }
    
          @Override
          public void mouseEntered(MouseEvent e) { }
    
          @Override
          public void mouseExited(MouseEvent e) { }
    
        });
        addMouseMotionListener(new MouseMotionListener() {
    
          @Override
          public void mouseDragged(MouseEvent e) {
            int deltaX = e.getXOnScreen() - screenX;
            int deltaY = e.getYOnScreen() - screenY;
    
            setLocation(myX + deltaX, myY + deltaY);
          }
    
          @Override
          public void mouseMoved(MouseEvent e) { }
    
        });
      }
    
    }
    
    public class Main {
    
      public static void main(String[] args) {
        JFrame f = new JFrame("Swing Hello World");
    
        // by doing this, we prevent Swing from resizing
        // our nice component
        f.setLayout(null);
    
        MyDraggableComponent mc = new MyDraggableComponent();
        f.add(mc);
    
        f.setSize(500, 500);
    
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched the web a bit, but all I found were abandoned projects and
I searched for this and found Maudite's question about text editors but they were
I searched and found this question but did not like the answer. Is there
I've searched on the Internet for comparisons between F# and Haskell but haven't found
I have few very simple question. I searched a web for them, but I
I have searched the web for resolution of this error, but everything I have
Has anybody found any documentation on comments in jaxb.index files? I've searched the web,
Searched and found many results and tried everything but couldnt figure it out! Created
I searched all over this site and the web for a good and simple
I've searched a lot for an answer for this question in the web: they

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.