Results 1 to 1 of 1
  1. #1
    Yaridovich is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Slow Response Time with Drag and Drop JPanel subclass

    I designed a JPanel subclass which, when dragged with the mouse, will move around the screen appropriately. I then added a JTextArea as a member of the class which shares the same mouseMotionListener as does MovableJPanel. Therefore, both the movableJPanel and the JTextArea will move around the screen when dragged, regardless of which was selected with the cursor to begin the dragging.

    Java Code:
    public class MovableJPanel extends JPanel {
    
            MovableJPanel() {
                this.setLayout(null);
                myLetter = 'J';
                letterLoc = new JTextArea();
    
    
    
    
                this.setBackground(new java.awt.Color(255, 0, 0));
                this.setBorder(new LineBorder(new java.awt.Color(0, 255, 0), 2));
                this.setSize(40, 40);
    
                this.add(letterLoc, javax.swing.JLayeredPane.DEFAULT_LAYER);
                letterLoc.setBounds(this.getInsets().left + 8, this.getInsets().top + 5, 25, 25);
                this.initializeComponents();
    
    
    
                this.addMouseMotionListener(new java.awt.event.MouseAdapter() {
    
                    @Override
                    public void mouseDragged(java.awt.event.MouseEvent evt) {
                        MovableJPanelMouseDragged(evt);
                    }
                });
    
                this.addMouseListener(new java.awt.event.MouseAdapter() {
    
                    @Override
                    public void mouseReleased(java.awt.event.MouseEvent evt) {
                        MovableJPanelMouseReleased(evt);
                    }
                });
    
    
                letterLoc.addMouseMotionListener(this.getMouseMotionListeners()[0]);
    
            }
    
    
    
            final void initializeComponents() {
                letterLoc.setText(String.valueOf(myLetter));
                letterLoc.setBackground(this.getBackground());
                letterLoc.setOpaque(false);
    
                Font font = new Font("Times New Roman", Font.BOLD, 28);
                letterLoc.setBackground(new java.awt.Color(0,0,255));
                letterLoc.setEditable(false);
                letterLoc.setFont(font);
                letterLoc.setDragEnabled(false);
                letterLoc.setFocusable(true);
                letterLoc.setSize(letterLoc.getPreferredSize());
    
    
            }
           void MovableJPanelMouseDragged(java.awt.event.MouseEvent evt) {
    
    
    
                if (this.isEnabled()) {
                    if (!beingHeld) {
                        beingHeld = true;
    
                        if (evt.getSource() == this) {
                            myRelativeStartingLocation = new Point(evt.getX(), evt.getY());
                        } else {
    
                            myRelativeStartingLocation = new Point(evt.getX() + (40 - letterLoc.getPreferredSize().width) / 2, evt.getY() + (40 - letterLoc.getPreferredSize().height) / 2 + 4);
                        }
    
    
                    }
                    else{
                    }
    
                    Point p = this.getLocation();
    
                    p.translate(evt.getXOnScreen() - this.getX() - JLayeredPane1.getLocationOnScreen().x - myRelativeStartingLocation.x, evt.getYOnScreen() - this.getY() - JLayeredPane1.getLocationOnScreen().y - myRelativeStartingLocation.y);
    
                    this.setLocation(p);
    
                }
           }
           
           void MovableJPanelMouseReleased(java.awt.event.MouseEvent evt) {
               beingHeld = false;
           }
    
    
            private Point myRelativeStartingLocation;
            private boolean beingHeld;
            private char myLetter;
            private JTextArea letterLoc;
    
            void setRelativeStartingLocation(Point p) {
                myRelativeStartingLocation = p;
            }
        }
    The problem is that when I select the JTextArea with my mouse and quickly drag the JPanel across the screen, the program has a much slower response time to my movement than when I select the JPanel and quickly drag that across the screen. If someone could give me an indication of why this might be happening I would greatly appreciate it, thanks.
    Last edited by Yaridovich; 07-31-2011 at 08:49 PM.

Similar Threads

  1. drag and drop
    By Dean29126 in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2011, 08:24 AM
  2. Slow down drag senstivity
    By x_singp in forum SWT / JFace
    Replies: 0
    Last Post: 12-29-2010, 11:28 AM
  3. Response from Tomcat Server slow and Invariable to JSP
    By jason.3dmagic in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 04-22-2010, 04:13 AM
  4. Drag and Drop JPanel at Runtime
    By ravjot28 in forum AWT / Swing
    Replies: 3
    Last Post: 01-17-2010, 04:46 PM
  5. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •