john01dav
john01dav

Reputation: 1962

Custom JFrame movement "slipping"

Recently, I have been trying to make my gui's look good and part of it is implementing my own titlebar, maximize button, minimize button, etc.

I am having issues on the dragging, I want to be able to drag the JFrame by clicking anywhere in it.

I attempted to create a mousemotionlistener to handle it however it slips when I use it. As in I attempt to drag the window to a location however it appears to be skipping calls to mouseDragged and attempting to call mouseMoved instead.

Here is my code

public class MouseWindowDragManager implements MouseMotionListener{
    private JFrame frame;
    int prevPosX = -1;
    int prevPosY = -1;

    public MouseWindowDragManager(JFrame frame){
        this.frame = frame;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        int newX = (frame.getLocation().x + (e.getXOnScreen() - prevPosX));
        int newY = (frame.getLocation().y + (e.getYOnScreen() - prevPosY));
        frame.setLocation(newX, newY);
        prevPosX = e.getXOnScreen();
        prevPosY = e.getYOnScreen();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        prevPosX = e.getXOnScreen();
        prevPosY = e.getProperty("apple.awt.draggableWindowBackground", true);tYOnScreen();
    }
}

Upvotes: 0

Views: 107

Answers (1)

camickr
camickr

Reputation: 324197

Recently, I have been trying to make my gui's look good and part of it is implementing my own titlebar, maximize button, minimize button, etc.

Gee, Microsoft and Apple spend millions of dollars to develop GUI's that users can use. They will be disappointed to hear this :)

I want to be able to drag the JFrame by clicking anywhere in it.

Check out Moving Windows for a class that allows you to drag a window around. The example code shows how to use the class by just dragging on your custom title bar. However, if you really want to be able to drag the frame by clicking anywhere then you can register the frame's root pane with the ComponentMover.

Upvotes: 2

Related Questions