This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

MouseWheelListener

Aubrey Bourke
Aubrey Bourke Member Posts: 31 Blue Ribbon
edited Oct 30, 2010 1:24PM in Abstract Window Toolkit (AWT)
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();
  
  }


  }
}

Answers

  • Aubrey Bourke
    Aubrey Bourke Member Posts: 31 Blue Ribbon
    Oh sorry, that code is wrong.

    Heres the correct code:
    //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 TrayIcon trayIcon;
      //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
      {
      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);
            }
        };
         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();
      
      }
    
    
      }
    }
  • 800322
    800322 Member Posts: 14,794
    Not sure whether the OS actually forwards mouse wheel events to the tray icon, and a TrayIcon is not really a GUI widget in the way the other widgets (Components) are.

    When I hit the volume control with a click in windows, it pops up a slider. A JSlider does have MouseWheelListener capability. That's probably the way to go. Maybe it even works with an invisible JSlider having the focus.
  • Aubrey Bourke
    Aubrey Bourke Member Posts: 31 Blue Ribbon
    Great idea thanks for the tip. However, Its not exactly what I want. If I'm to use a JSlider it means i will have to click on the system tray icon first, then scroll with the mouse wheel. What i want is to control the volume without clicking on the tray icon, just hover the mouse over it and scroll!

    Do you think its possible to combine mouseEntered() on the trayIcon with an invisible JSlider that implements the mouse wheel scroll?

    Regards
    Aubrey.
  • darrylburke
    darrylburke Member Posts: 18,007
    Moderator action: Moved from New to Java

    db
  • darrylburke
    darrylburke Member Posts: 18,007
    806496 wrote:
    Do you think its possible to combine mouseEntered() on the trayIcon with an invisible JSlider that implements the mouse wheel scroll?
    Best way to find out is to try it. But first of all check whether a mouseEntered is detected or not.

    db

    -- MrBoombastic?
  • Aubrey Bourke
    Aubrey Bourke Member Posts: 31 Blue Ribbon
    HaHa, yeah Booooooooooombastic!
This discussion has been closed.