0

I have the following code:

panel.addMouseListener(new MouseAdapter() {
    public void mouseDragged(MouseEvent e) {
        frame.setLocation(e.getXOnScreen(), e.getYOnScreen());
    }
});

panel is a JPanel, and all imports have been included. Now, when I drag the panel, the frame doesn't move a tiny bit. Why is this, and how should I go about fixing it?

EDIT: frame is undecorated.
EDIT: mouseDragged isn't getting triggered - that's the problem to fix! EDIT: CODE!!!!!

package user_interface;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame {
    public Test() {
        this.setUndecorated(true);
        this.setLayout(new GridLayout(3, 3, 3, 3));
        for (int i = 0; i < 4; i++) {
            this.add(new JPanel());
        }
        JPanel panel = new JPanel();
        panel.setBackground(new Color(0));
        panel.setToolTipText("Drag to move frame");
        this.addMouseListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                Test.this.setLocation(e.getXOnScreen(), e.getYOnScreen());
                System.out.println(e);
            }
        });
        this.add(panel);
        for (int i = 0; i < 4; i++) {
            this.add(new JPanel());
        }
    }

    public static void main(String... args) {
        Test test = new Test();
        test.setSize(750, 500);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 3
        test.setVisible(true);
    }
}
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Sep 03 '15 at 01:42
  • And for [example](http://stackoverflow.com/questions/16869877/how-to-remove-window-box-from-any-java-gui/16869893#16869893) and [example](http://stackoverflow.com/questions/30836201/jframe-wrong-location-with-ubuntu-unity/30836537#30836537) – MadProgrammer Sep 03 '15 at 01:44
  • [_et al._](http://stackoverflow.com/search?tab=votes&q=%5bjava%5d%20ComponentMover) – trashgod Sep 03 '15 at 01:44
  • I've noticed that the mousedragged isn't even getting triggered. – hyper-neutrino Sep 03 '15 at 01:47
  • ***"EDIT:** frame is undecorated."* O...K. How is the user supposed to know that a borderless frame ***can*** be dragged? It is counterintuitive. – Andrew Thompson Sep 03 '15 at 05:54
  • @AndrewThompson Although it doesn't work on a bordered frame either (don't ask why I'm not just using the window border). – hyper-neutrino Sep 03 '15 at 14:37

1 Answers1

1

Add MouseMotionListener for that purpose.

JPanel panel = new JPanel();
panel.setBackground(new Color(0));
panel.setToolTipText("Drag to move frame");

panel.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
       super.mouseDragged(e);
       Test.this.setLocation(e.getXOnScreen(), e.getYOnScreen());
       System.out.println(e);
    }
});

this.setContentPane(panel);
Ferdinand Neman
  • 680
  • 3
  • 6