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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:05:07+00:00 2026-05-15T13:05:07+00:00

I am trying to make a JFrame appear on mousePressed Location but I keep

  • 0

I am trying to make a JFrame appear on mousePressed Location but I keep failing and it get’s annoying 🙁 Any ideas what isn’t working?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SSCCE
{

    @SuppressWarnings("static-access")
    public static void getInputData()
    {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
        JLabel emptyLabel = new JLabel("Test");
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
        frame.setSize(new Dimension(375, 100));

        MouseAdapter ml = new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent me)
            {

                frame.setLocation(me.getX(), me.getY());
            }

            @Override
            public void mouseDragged(MouseEvent me)
            {
                frame.setLocation(me.getX(), me.getY());
            }
        };
        frame.getContentPane().addMouseListener(ml);
        frame.getContentPane().addMouseMotionListener(ml);

        frame.setVisible(true);

    }

    public static void main(String args[])
    {
        JFrame test = new JFrame();
        JButton but = new JButton("Click me");
        but.addActionListener(
                new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {
                        getInputData();
                    }
                });
        test.getContentPane().add(but, BorderLayout.CENTER);
        test.setSize(500, 500);
        test.setVisible(true);


    }
}
  • 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-15T13:05:08+00:00Added an answer on May 15, 2026 at 1:05 pm

    Use the SwingUtilities methods convertPointToScreen() and convertPointFromScreen() to transform the MouseEvent coordinates.

    Addendum: Alternatively, calculate the offset from getLocationOnScreen(), which is “the component’s top-left corner in the screen’s coordinate space.”

    Addendum: To position the new frame relative to the original mouse click, add a mouse listener to the parent frame instead of a button; use the coordinates to position the new frame, as shown below.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SSCCE {
    
        public static void getInputData(MouseEvent e) {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel emptyLabel = new JLabel("Test", JLabel.CENTER);
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
            frame.setPreferredSize(new Dimension(375, 100));
    
            MouseAdapter ma = new MouseAdapter() {
    
                Point local, global;
                Point delta = new Point();
    
                @Override
                public void mousePressed(MouseEvent me) {
                    local = me.getPoint();
                }
    
                @Override
                public void mouseDragged(MouseEvent me) {
                    delta.setLocation(
                        me.getX() - local.x, me.getY() - local.y);
                    global = frame.getLocationOnScreen();
                    global.setLocation(
                        global.x + delta.x, global.y + delta.y);
                    frame.setLocation(global.x, global.y);
                }
            };
            frame.getContentPane().addMouseListener(ma);
            frame.getContentPane().addMouseMotionListener(ma);
            frame.pack();
            frame.setLocation(e.getLocationOnScreen());
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setPreferredSize(new Dimension(640, 480));
            frame.add(new JLabel("Click me", JLabel.CENTER));
            frame.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mousePressed(MouseEvent e) {
                    getInputData(e);
                }
            });
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    Previously,

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SSCCE {
    
        public static void getInputData() {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel emptyLabel = new JLabel("Test", JLabel.CENTER);
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
            frame.setPreferredSize(new Dimension(375, 100));
    
            MouseAdapter ma = new MouseAdapter() {
    
                Point local = new Point();
                Point delta = new Point();
                Point global = new Point();
    
                @Override
                public void mousePressed(MouseEvent me) {
                    local = me.getPoint();
                }
    
                @Override
                public void mouseDragged(MouseEvent me) {
                    delta.setLocation(
                        me.getX() - local.x,
                        me.getY() - local.y);
                    global = frame.getLocationOnScreen();
                    global.setLocation(global.x + delta.x, global.y + delta.y);
                    frame.setLocation(global.x, global.y);
                }
            };
            frame.getContentPane().addMouseListener(ma);
            frame.getContentPane().addMouseMotionListener(ma);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
        }
    
        public static void main(String args[]) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton but = new JButton("Click me");
            but.addActionListener(
                new ActionListener() {
    
                    public void actionPerformed(ActionEvent e) {
                        getInputData();
                    }
                });
            frame.getContentPane().add(but, BorderLayout.CENTER);
            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'm trying to make the case for click-once and smart client development but my
I'm trying to make a JFrame with a usable content area of exactly 500x500.
I'm trying to make a GUI in java, but JMenuBar has been giving me
I am just messing around trying to make a game right now, but I
I am working with nHibernate, and trying make sense of bag collections. My data
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make a data bound column invisible after data binding, because it

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.