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

JTextField Raises NullPointerException When mouseDrag Event is performed on it

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      I was trying to make JTextField,JTextArea or JPasswordField movable and resizable using mousePressed and mouseDragged Events . . .
      when i move are resize this components
      NullPointerException occures .

      seems to be problem with the caret . ..


      ERROR MESSAGES/STACK TRACES THAT OCCUR :

      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
              at javax.swing.text.DefaultCaret.moveCaret(DefaultCaret.java:311)
              at javax.swing.text.DefaultCaret.mouseDragged(DefaultCaret.java:565)
              at java.awt.AWTEventMulticaster.mouseDragged(AWTEventMulticaster.java:303)
              at java.awt.Component.processMouseMotionEvent(Component.java:6086)
              at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3278)
              at java.awt.Component.processEvent(Component.java:5807)
              at java.awt.Container.processEvent(Container.java:2058)
              at java.awt.Component.dispatchEventImpl(Component.java:4410)
              at java.awt.Container.dispatchEventImpl(Container.java:2116)
              at java.awt.Component.dispatchEvent(Component.java:4240)
              at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
              at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4003)

              at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
              at java.awt.Container.dispatchEventImpl(Container.java:2102)
              at java.awt.Window.dispatchEventImpl(Window.java:2429)
              at java.awt.Component.dispatchEvent(Component.java:4240)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
              at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
              at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

              at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class Temp extends JFrame
      {
      Point location;
      MouseEvent pressed;
      JTextField comp;
      int wi,hi;
      boolean set=false;
      public Temp()
      {
      super("Test");
      getContentPane().setLayout(null);
      comp = new JTextField("Hai",15);
      comp.setLocation(20,20);
      comp.setSize(100,50);
      makeMovable();
      getContentPane().add(comp);
      setSize(400,400);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      private void makeMovable()
      {
      comp.addMouseListener(new MouseAdapter()
      {
      public void mousePressed(MouseEvent me)
      {
      pressed = me;
      int xh= comp.getX()+comp.getWidth();
      int yh = comp.getY()+comp.getHeight();
      set=false;
      if(pressed.getX()<=xh&&(pressed.getX()>=comp.getWidth()-10)&&pressed.getY()<=yh&&(pressed.getY()>=comp.getHeight()-10))
      {
      comp.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
      wi = comp.getWidth();
      hi = comp.getHeight();
      set=true;
      }
      }
      public void mouseReleased(MouseEvent me)
      {
      comp.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
      }

      });
      comp.addMouseMotionListener(new MouseMotionAdapter(){
      public void mouseDragged(MouseEvent me)
      {
      if(!set)
      {
      location = comp.getLocation(location);
      int x = location.x - pressed.getX() + me.getX();
      int y = location.y - pressed.getY() + me.getY();
      if(x<0)
      x=0;
      if(y<0)
      y=0;
      if(x>(comp.getParent().getWidth()-comp.getWidth()))
      x=comp.getParent().getWidth()-comp.getWidth();
      if(y>(comp.getParent().getHeight()-comp.getHeight()))
      y=comp.getParent().getHeight()-comp.getHeight();
      comp.setLocation(x, y);
      comp.updateUI();
      comp.validate();
      }
      else
      {
      int w = wi+me.getX()-pressed.getX();
      int h = hi+me.getY()-pressed.getY();
      if(w<25)
      {
      w=25;
      }
      if(h<25)
      {
      h=25;
      }
      comp.setSize(w,h);
      comp.updateUI();
      comp.validate();
      }
      }
      });
      }
         public static void main( String[] args )
         {
      new Temp();
         }
      }

      ---------- END SOURCE ----------

            shickeysunw Shannon Hickey (Inactive)
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: