views:

142

answers:

1

Hello everyone, 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);


    }
}
+2  A: 

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);
    }
}
trashgod
Umm the point is to actually show the new frame at the point of the click, how would it figure that without the adapter?
devilwontcry
Umm, it's still isn't working actually... For some reason it's not looking at the MouseAdapter even in your solution. It simply shows the new JFrame in the middle of the screen nothing related to the mouse click. Move the first frame with the button and hit it and you'll see what I mean.
devilwontcry
@devilwontcry: Yes, setLocationRelativeTo(null) is invoked on each new frame. Comment it out to see the effect. If you want the new frame positioned relative to the original click, you'll need to add a mouse listener to the parent frame instead of a button.
trashgod