Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
MouseWheelListener

Aubrey Bourke
Member Posts: 31 Blue Ribbon
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!!!):
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(); } } }
Tagged:
Answers
-
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(); } } }
-
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. -
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. -
Moderator action: Moved from New to Java
db -
806496 wrote:Best way to find out is to try it. But first of all check whether a mouseEntered is detected or not.
Do you think its possible to combine mouseEntered() on the trayIcon with an invisible JSlider that implements the mouse wheel scroll?
db
-- MrBoombastic? -
HaHa, yeah Booooooooooombastic!
This discussion has been closed.