2012-11-04 5 views
2

SystemTrayPopUpMenu에 java 항목을 추가 할 수 있습니까? 메뉴에 항목을 추가하려고 시도했지만 작동하지 않습니다. 추가하지 않고 TrayIcon.setPopMenu(PopUpMenu)과 그 이후에 SystemTray.add(TrayIcon) 메서드를 호출하십시오. 어떤 제안?Java에서 시스템 트레이를 동적으로 업데이트

+0

당신이 [SSCE] (HTTP를 게시 할 수 없습니다를 : // sscce.org/)? – mercutio

답변

3

물론 가능합니다.

내가 만든 예제 참조 :

enter image description here

import java.awt.AWTException; 
import java.awt.CheckboxMenuItem; 
import java.awt.Image; 
import java.awt.Menu; 
import java.awt.MenuItem; 
import java.awt.PopupMenu; 
import java.awt.SystemTray; 
import java.awt.TrayIcon; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class Test { 

    public Test() throws Exception { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new Test(); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
    } 

    private void initComponents() throws Exception { 

     createAndShowTray(); 

    } 

    private void createAndShowTray() throws Exception { 

     //Check the SystemTray is supported 
     if (!SystemTray.isSupported()) { 
      System.out.println("SystemTray is not supported"); 
      return; 
     } 

     PopupMenu popup = new PopupMenu(); 
     //retieve icon form url and scale it to 32 x 32 
     final TrayIcon trayIcon = new TrayIcon(ImageIO.read(
       new URL("http://www.optical-illusions.in/illusions/blue_rotation_optical_illusion.jpg")) 
       .getScaledInstance(32, 32, Image.SCALE_SMOOTH), null); 

     //get the system tray 
     final SystemTray tray = SystemTray.getSystemTray(); 

     // Create a pop-up menu components 
     MenuItem aboutItem = new MenuItem("About"); 
     aboutItem.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       JOptionPane.showMessageDialog(null, "About"); 
      } 
     }); 

     final CheckboxMenuItem cb1 = new CheckboxMenuItem("Set tooltip"); 
     cb1.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent ie) { 
       if(cb1.getState()==true) { 
       trayIcon.setToolTip("Hello, world"); 
       }else { 
       trayIcon.setToolTip(""); 
       } 
      } 
     }); 

     Menu displayMenu = new Menu("Display"); 
     MenuItem infoItem = new MenuItem("Info"); 

     MenuItem exitItem = new MenuItem("Exit"); 
     //add actionlistner to exit menu item 
     exitItem.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       System.exit(0); 
      } 
     }); 

     //Add components to pop-up menu 
     popup.add(aboutItem); 
     popup.addSeparator(); 
     popup.add(cb1); 
     popup.addSeparator(); 
     popup.add(displayMenu); 
     displayMenu.add(infoItem); 
     popup.add(exitItem); 

     trayIcon.setPopupMenu(popup); 

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

참고 :