Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MouseWheelListener

Aubrey BourkeOct 30 2010 — edited Oct 30 2010
Hi,

Im writing a volume level mixer for linux in Java. I tried adding a mouse wheel listener to a System Tray Icon. After checking the System Tray Icon API, it seem it doesnt allow a wheel listener, but does allow other mouse listeners. Can someone guide me with what to do next.

Heres what I have so far (only 1 error - cant add wheel listener!!!):
//Level.java - Program that displays the volume in bars

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.Graphics2D.*;
import javax.imageio.ImageIO;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener.*;

public class Level extends JWindow {
  
  static public int volume=70;
  private Rectangle rect;
  
  //static SystemTray tray;
  final static String newline = "\n";





  public void Level()
  {
  setSize(300, 120);
  setLocation(getWidth()-320, getHeight()-140);
  init();
  
  
  repaint();
  }
  
  public static int getVolume(){
    return volume;
  }

  public void init(){
    try {  
	Process p = Runtime.getRuntime().exec("amixer set Master 70%");  
	} catch (IOException e)
	    {  
            e.printStackTrace();  
	    }  
	}



	    
	 
  
  public static void upFade(){
  try {  
	volume = volume + 5;
	Process p = Runtime.getRuntime().exec("amixer set Master "+volume+"%");  
	} catch (IOException e)
	    {  
            e.printStackTrace();  
	    }  
	}
  

  public static void downFade(){
  try {  
	volume = volume - 5;
	Process p = Runtime.getRuntime().exec("amixer set Master "+volume+"%");  
	} catch (IOException e)
	    {  
            e.printStackTrace();  
	    }  
	}
  void saySomething(String eventDescription) {
    System.out.println(eventDescription);
      }
  public void paint(Graphics g)
  {
  Graphics2D g2 = (Graphics2D)g;
  //g2.drawRect();
  }

  public static void main(String[] args) throws Exception
  {
  final TrayIcon trayIcon;
  Level level = new Level();
    if (SystemTray.isSupported()) {
    SystemTray tray = SystemTray.getSystemTray();
    

    InputStream smoke = Level.class.getResourceAsStream("volume.jpg");
    Image image2 = ImageIO.read(smoke);

    MouseListener mouseListener = new MouseListener() {
                
        public void mouseClicked(MouseEvent e) {
           // System.out.println("Tray Icon - Mouse clicked!");                 
		
        }

        public void mouseEntered(MouseEvent e) {
            //System.out.println("Tray Icon - Mouse entered!");                 
        }

        public void mouseExited(MouseEvent e) {
            //System.out.println("Tray Icon - Mouse exited!");                 
        }

        public void mousePressed(MouseEvent e) {
            //System.out.println("Tray Icon - Mouse pressed!");                 
        }

        public void mouseReleased(MouseEvent e) {
            //System.out.println("Tray Icon - Mouse released!");                 
        }
    };
ActionListener donateListener = new ActionListener() {
        public void actionPerformed(ActionEvent ea) {
           	//BrowserControl bc = new BrowserControl();
		//bc. displayURL("UUUUUUUUUUUUUUUUURRRRRRRRRRRRRRLLLLLLLLLLLLLLLLLL");
        }
    };
    ActionListener exitListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Exiting...");
            System.exit(0);
        }
    };
            
    PopupMenu popup = new PopupMenu();
    MenuItem defaultItem = new MenuItem("Exit");
    MenuItem donateItem = new MenuItem("Donate");
    donateItem.addActionListener(donateListener);
    defaultItem.addActionListener(exitListener);
    popup.add(defaultItem);
   popup.add(donateItem);
    trayIcon = new TrayIcon(image2, "JMix", popup);
    trayIcon.setImageAutoSize(true);
 
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            trayIcon.displayMessage("Action Event", 
                "action event has been performed",
                TrayIcon.MessageType.INFO);
        }
    };
    Component ti = (Component)trayIcon;
      
     trayIcon.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
    String message;
    int notches = e.getWheelRotation();
    if (notches < 0) {
      System.out.println("Mouse wheel moved UP " + -notches + " notch(es)"
          + newline);
    } else {
      System.out.println(message = "Mouse wheel moved DOWN " + notches + " notch(es)"
          + newline);
    }
    
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
      message += "    Scroll type: WHEEL_UNIT_SCROLL" + newline;
      message += "    Scroll amount: " + e.getScrollAmount()
          + " unit increments per notch" + newline;
      message += "    Units to scroll: " + e.getUnitsToScroll()
          + " unit increments" + newline;
    /*  message += "    Vertical unit increment: "
          + scrollPane.getVerticalScrollBar().getUnitIncrement(1)
          + " pixels" + newline;*/
    } else { //scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL
      message += "    Scroll type: WHEEL_BLOCK_SCROLL" + newline;
      /*message += "    Vertical block increment: "
          + scrollPane.getVerticalScrollBar().getBlockIncrement(1)
          + " pixels" + newline;*/
    }
    //saySomething(message, e);
  }
    });     
 
    trayIcon.addActionListener(actionListener);
    trayIcon.addMouseListener(mouseListener);
    //trayIcon.addMouseWheelListener(wheelListener);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
    }

} else {

    //  System Tray is not supported
	System.out.println("System Tray not supported");
}
  while (volume < 100){
  System.out.println(getVolume());
  upFade();
  
  }


  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2010
Added on Oct 30 2010
6 comments
3,168 views