Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6338165

JFrame jumps while dragging XAWT

XMLWordPrintable

    • generic
    • linux

      I've been working on a custom Look and Feel API lately and the last thing I need to do is come up with my own window decorators instead of the default for top level windows like a JFrame. So I'm trying to implement a draggable window. My code works fine in Windows 2000/NT/etc, but when run on Linux, the window jiggles as you drag it around. Seems like calling setLocation() causes extra mouse events to be fired in Linux? Maybe? I've tried all kinds of ways around this without any luck. Help would be much appreciated.

      --Huy

      Here's the sample code:

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class FrameTest extends JFrame {
          Point startDrag = null;
          Point curLocation = null;
          
          public FrameTest()
          {
              this.setUndecorated(true);
              this.setSize(300,300);
              
              JPanel dragPanel = new JPanel();
              this.setContentPane(dragPanel);
              
              dragPanel.setLayout(new FlowLayout());
              
              dragPanel.addMouseMotionListener(new MouseMotionAdapter()
              {
                  public void mouseDragged(MouseEvent event)
                  {
                      Point curDrag = event.getPoint();
                      
                      if (startDrag == null)
                      {
                          startDrag = curDrag;
                      }
                      curLocation = curDrag;
                      
                      Point location = getLocation();
                      setLocation(
                          location.x + (curLocation.x - startDrag.x),
                          location.y + (curLocation.y - startDrag.y)
                      );
                  }
              });
              
              dragPanel.addMouseListener(new MouseAdapter(){
              
                  public void mouseReleased(MouseEvent event)
                  {
                      Point location = getLocation();
                      setLocation(
                          location.x + (curLocation.x - startDrag.x),
                          location.y + (curLocation.y - startDrag.y)
                      );
                      startDrag = null;
                  }
              });
          }
          
          public static void main(String [] args)
          {
              FrameTest test = new FrameTest();
              test.setVisible(true);
          }
      }

            dav Andrei Dmitriev (Inactive)
            dav Andrei Dmitriev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: