Friday, 4 April 2014

MouseWheelListener


The listener interface for receiving mouse wheel events on a component.

public interface MouseWheelListener extends EventListener {
    public void mouseWheelMoved(MouseWheelEvent e);
}


void mouseWheelMoved(MouseWheelEvent e)
    Invoked when the mouse wheel is rotated.

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

public class MouseWheelListenerEx{
 JTextArea displayArea;
 JTextField typingArea;
 JFrame frame1;
  
 MouseWheelListenerEx(){
  
  /* 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.addMouseWheelListener(new MouseWheelListener(){
   public void mouseWheelMoved(MouseWheelEvent e){
    displayArea.append("Mouse Wheel Moved\n" + e +"\n");
   } 
  });   
 }
 
 public static void main(String args[]){
  new MouseWheelListenerEx();
 }
}

Perform Mouse wheel Operation on the text box. You can get the output in the text Area like below.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment