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 8231383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:28:13+00:00 2026-06-07T17:28:13+00:00

I have a program where I am creating a JLabel on a click and

  • 0

I have a program where I am creating a JLabel on a click and then dragging that to another portion of the interface.

What I want is to click somewhere in the JPanel, have it drop a JLabel there, and then drag another JLabel all in the same click.

I am able to do this, but it takes multiple clicks. Can I do it in one click?

To illustrate what I mean, I created this sample program:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class DragTest extends JFrame implements MouseMotionListener,
        MouseListener {

    private JPanel panel = new JPanel(null);
    private JLabel dragLabel = new JLabel("drag");;
    private final JWindow window = new JWindow();

    public DragTest() {
        this.add(panel);
        panel.addMouseListener(this);

        dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        dragLabel = new JLabel("drag");
        dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
        int x = me.getPoint().x;
        int y = me.getPoint().y;
        window.add(dragLabel);
        window.pack();
        Point pt = new Point(x, y);
        Component c = (Component) me.getSource();
        SwingUtilities.convertPointToScreen(pt, c);
        window.setLocation(pt);
        window.setVisible(true);
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

        JLabel dropLabel = new JLabel("drop");
        panel.add(dropLabel);
        dropLabel.setForeground(Color.RED);
        dropLabel.setFont(new Font("Serif", Font.BOLD, 48));
        dropLabel.setBounds(e.getX(), e.getY(), 100, 60);
        dropLabel.addMouseMotionListener(this);
        dropLabel.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                dragLabel.setVisible(false);
                window.setVisible(false);

            }

        });
        repaint();

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) {
        DragTest frame = new DragTest();
        frame.setVisible(true);
        frame.setSize(600, 400);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

So the initial click creates the “drop” JLabel, and then clicking on and dragging on the “drop” JLabel will create a “drag” JLabel that follows the mouse around.

How can I do this in one click and drag?

  • 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-06-07T17:28:15+00:00Added an answer on June 7, 2026 at 5:28 pm

    Don’t create a new JLabel in the mouseDragged method but rather use the same JLabel. For example:

    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.*;
    
    public class DragTest2 extends JPanel {
       private static final int PREF_W = 500;
       private static final int PREF_H = 400;
       private static final float LABEL_PTS = 24f;
       private static final String LABEL_TEXT = "My Label";
       private JLabel label = null;
    
       public DragTest2() {
          setLayout(null);
          MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
          addMouseListener(myMouseAdapter);
          addMouseMotionListener(myMouseAdapter);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       class MyMouseAdapter extends MouseAdapter {
    
          @Override
          public void mousePressed(MouseEvent e) {
             label = new JLabel(LABEL_TEXT);
             label.setFont(label.getFont().deriveFont(LABEL_PTS));
             Dimension labelSize = label.getPreferredSize();
             label.setSize(labelSize);
             int x = e.getX() - labelSize.width / 2;
             int y = e.getY() - labelSize.height / 2;
             label.setLocation(x , y);
             add(label);
             repaint();
          }
    
          @Override
          public void mouseDragged(MouseEvent e) {
             if (label != null) {
                Dimension labelSize = label.getPreferredSize();
                int x = e.getX() - labelSize.width / 2;
                int y = e.getY() - labelSize.height / 2;
                label.setLocation(x , y);
                repaint();
             }
          }
    
          @Override
          public void mouseReleased(MouseEvent e) {
             if (label != null) {
                Dimension labelSize = label.getPreferredSize();
                int x = e.getX() - labelSize.width / 2;
                int y = e.getY() - labelSize.height / 2;
                label.setLocation(x , y);
                repaint();
                label = null;
             }
          }
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("DragTest2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new DragTest2());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a program that uses JFrame, JPanel, JLabel and all other sorts
I have program that runs fast enough. I want to see the number of
I have a program which creates JButtons which are then added to a JPanel
i have a program that takes some time creating pdf files i would like
I have a program that downloads a binary file, from another PC. I also
Im creating a program that is supposed have the user enter a student name
I have a program that's creating a secure directory for user output. This is
I have a program that is creating multiple files. There is a function for
im creating a program that will compile java files, at the moment i have
I have a program I am porting that links together multiple libraries when creating

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.