public interface MouseMotionListener extends EventListener {
public void mouseDragged(MouseEvent e);
public void mouseMoved(MouseEvent e);
}
void
mouseDragged(MouseEvent e)
Invoked
when a mouse button is pressed on a component and then dragged.
void
mouseMoved(MouseEvent e)
Invoked
when the mouse cursor has been moved onto a component but no buttons
have been pushed.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; public class MouseMotionListenerEx{ JTextArea displayArea; JTextField typingArea; JFrame frame1; MouseMotionListenerEx(){ /* Initialize typingArea */ typingArea = new JTextField(20); /* Initialize Display Area */ displayArea = new JTextArea(); displayArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(displayArea); scrollPane.setPreferredSize(new Dimension(375, 125)); /* Initialize Frame */ frame1 =new JFrame("Sample Frame"); frame1.pack(); frame1.setVisible(true); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.getContentPane().add(typingArea, BorderLayout.PAGE_START); frame1.getContentPane().add(scrollPane, BorderLayout.CENTER); frame1.setSize(300, 500); /* Add Key Listener */ typingArea.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e){ displayArea.append("Mouse Dragged\n" + e +"\n"); } public void mouseMoved(MouseEvent e){ displayArea.append("Mouse Moved\n " + e +"\n"); } }); } public static void main(String args[]){ new MouseMotionListenerEx(); } }
Perform
some drag and mouse movements on the text box. You can see the output
like below window.
No comments:
Post a Comment