kevinarpe
kevinarpe

Reputation: 21309

How to reduce Java Swing jitter/flicker/shake when moving dialog with MouseListener/MouseMotionListener?

I built a Java Swing dialog with no decoration. I have implemented dialog move using drag/drop with the MouseListener and MouseMotionListener interfaces.

However, during testing, the dialog flickers/jitters/shakes when moving. I would like the drag/drop motion to be fluid/smooth.

I am running JDK 1.6.0.21 on Windows XP (SP3).

Here is some sample code:

@Override
public void mouseDragged(MouseEvent event) {
    Component component = event.getComponent();
    if (this != component) {
        return;
    }
    int modifiers = event.getModifiersEx();
    // This means no Ctrl/Shift/Alt/Meta.
    if (InputEvent.BUTTON1_DOWN_MASK == modifiers) {
        Point point_relative_to_source = event.getPoint();
        int delta_x = point_relative_to_source.x - _mouse_press_point.x;
        int delta_y = point_relative_to_source.y - _mouse_press_point.y;
        Point component_location = component.getLocation();
        component_location.x += delta_x;
        component_location.y += delta_y;
        component.setLocation(component_location);
    }
}

protected Point _mouse_press_point;

@Override
public void mousePressed(MouseEvent event) {
    Object source = event.getSource();
    if (this != source) {
        return;
    }
    System.out.println("mousePressed");
    _mouse_press_point = event.getPoint();
}

@Override
public void mouseReleased(MouseEvent event) {
    Object source = event.getSource();
    if (this != source) {
        return;
    }
    System.out.println("mouseReleased");
    _mouse_press_point = null;
}

Do you know how to reduce the flicker/jitter/shake?

Thanks, Arpe

Upvotes: 2

Views: 1108

Answers (1)

camickr
camickr

Reputation: 324118

Moving Windows provides a class that can be used to move a window.

Upvotes: 3

Related Questions