KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Tray


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 import org.jdesktop.jdic.tray.*;
22 import javax.swing.*;
23 import java.awt.*;
24 import java.awt.event.*;
25
26
27 public class Tray implements ActionListener, ItemListener {
28
29     SystemTray tray = SystemTray.getDefaultSystemTray();
30     TrayIcon ti;
31     JFrame frame;
32     public Tray() {
33
34         JPopupMenu menu;
35         JMenu submenu;
36         JMenuItem menuItem;
37         JRadioButtonMenuItem rbMenuItem;
38         JCheckBoxMenuItem cbMenuItem;
39         
40         try {
41             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
42         } catch (Exception JavaDoc e) {
43             e.printStackTrace();
44         }
45         if( Integer.parseInt(System.getProperty("java.version").substring(2,3)) >=5 )
46             System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
47         menu = new JPopupMenu("A Menu");
48         
49         // a group of JMenuItems
50
menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
51         // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
52
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
53                 ActionEvent.CTRL_MASK));
54         menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
55         menuItem.addActionListener(this);
56         menu.add(menuItem);
57
58         // ImageIcon icon = new ImageIcon("middle.gif");
59
ImageIcon icon = new ImageIcon(Tray.class.getResource("images/middle.gif"));
60
61         menuItem = new JMenuItem("Both text and icon", icon);
62         menuItem.setMnemonic(KeyEvent.VK_B);
63         menuItem.addActionListener(this);
64         menu.add(menuItem);
65
66         menuItem = new JMenuItem(icon);
67         menuItem.setMnemonic(KeyEvent.VK_D);
68         menuItem.addActionListener(this);
69         menu.add(menuItem);
70
71         // a group of radio button menu items
72
menu.addSeparator();
73         ButtonGroup group = new ButtonGroup();
74
75         rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
76         rbMenuItem.setSelected(true);
77         rbMenuItem.setMnemonic(KeyEvent.VK_R);
78         group.add(rbMenuItem);
79         rbMenuItem.addActionListener(this);
80         menu.add(rbMenuItem);
81
82         rbMenuItem = new JRadioButtonMenuItem("Another one");
83         rbMenuItem.setMnemonic(KeyEvent.VK_O);
84         group.add(rbMenuItem);
85         rbMenuItem.addActionListener(this);
86         menu.add(rbMenuItem);
87
88         // a group of check box menu items
89
menu.addSeparator();
90         cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
91         cbMenuItem.setMnemonic(KeyEvent.VK_C);
92         cbMenuItem.addItemListener(this);
93         menu.add(cbMenuItem);
94
95         cbMenuItem = new JCheckBoxMenuItem("Another one");
96         cbMenuItem.setMnemonic(KeyEvent.VK_H);
97         cbMenuItem.addItemListener(this);
98         menu.add(cbMenuItem);
99
100         // a submenu
101
menu.addSeparator();
102         submenu = new JMenu("A submenu");
103         submenu.setMnemonic(KeyEvent.VK_S);
104
105         menuItem = new JMenuItem("An item in the submenu");
106         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
107                 ActionEvent.CTRL_MASK));
108         menuItem.addActionListener(this);
109         submenu.add(menuItem);
110
111         menuItem = new JMenuItem("Another item");
112         menuItem.addActionListener(this);
113         submenu.add(menuItem);
114         menu.add(submenu);
115
116         // "Quit" menu item
117
menu.addSeparator();
118         menuItem = new JMenuItem("Quit");
119         menuItem.addActionListener(this);
120         menu.add(menuItem);
121         
122         // ImageIcon i = new ImageIcon("duke.gif");
123
ImageIcon i = new ImageIcon(Tray.class.getResource("images/duke.gif"));
124
125         ti = new TrayIcon(i, "JDIC Tray Icon API Demo - TrayIcon", menu);
126
127         ti.setIconAutoSize(true);
128         ti.addActionListener(new ActionListener() {
129             public void actionPerformed(ActionEvent e) {
130                 frame.setVisible(!frame.isVisible());
131             }
132         });
133         ti.addBalloonActionListener(new ActionListener(){
134             public void actionPerformed(ActionEvent e) {
135               JOptionPane.showMessageDialog(null,
136               "Balloon Message been clicked - TrayIcon", "Message",
137               JOptionPane.INFORMATION_MESSAGE);
138             }
139         });
140         
141         tray.addTrayIcon(ti);
142         
143         // Construct the GUI for balloon message.
144
frame = new JFrame("Show Balloon Message");
145         frame.getContentPane().setLayout(new BorderLayout());
146         frame.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
147         
148         JPanel topPanel = new JPanel();
149         topPanel.setBorder(BorderFactory.createEtchedBorder());
150         topPanel.setLayout(new BorderLayout());
151         topPanel.add(new JLabel("Caption: "), BorderLayout.WEST);
152         final JTextField captionField = new JTextField("JDIC TrayIcon");
153         topPanel.add(captionField, BorderLayout.CENTER);
154         JPanel typePanel = new JPanel();
155         final JComboBox typeBox = new JComboBox(new String JavaDoc[]{"INFO", "ERROR", "WARNING", "NONE" });
156         typePanel.add(new JLabel(" Type:"), BorderLayout.WEST);
157         typePanel.add(typeBox, BorderLayout.EAST);
158         topPanel.add(typePanel, BorderLayout.EAST);
159         frame.getContentPane().add(topPanel, BorderLayout.NORTH);
160         
161         JPanel messagePanel = new JPanel();
162         messagePanel.setLayout(new BorderLayout());
163         messagePanel.add(new JLabel("Message:"), BorderLayout.NORTH);
164         final JTextArea messageArea = new JTextArea(5, 20);
165         messageArea.setText("This is a balloon message.\nYou can set the caption, message, \nand message type");
166         messageArea.setBorder(BorderFactory.createEtchedBorder());
167         messagePanel.add(messageArea);
168         frame.getContentPane().add(messagePanel, BorderLayout.CENTER);
169         
170         JPanel buttonPanel = new JPanel();
171         final JButton okButton = new JButton("OK");
172         final JButton cancelButton = new JButton("Cancel");
173         ActionListener al = new ActionListener(){
174             public void actionPerformed(ActionEvent e) {
175                 if(e.getSource() == cancelButton)
176                     frame.setVisible(false);
177                 else if(e.getSource() == okButton){
178                     ti.displayMessage(captionField.getText(), messageArea.getText(), typeBox.getSelectedIndex());
179                 }
180             }
181         };
182         okButton.addActionListener(al);
183         cancelButton.addActionListener(al);
184         buttonPanel.add(okButton);
185         buttonPanel.add(cancelButton);
186         frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
187         frame.pack();
188         frame.setLocationRelativeTo(null);
189         frame.setVisible(true);
190     }
191
192     // Returns just the class name -- no package info.
193
protected String JavaDoc getClassName(Object JavaDoc o) {
194         String JavaDoc classString = o.getClass().getName();
195         int dotIndex = classString.lastIndexOf(".");
196
197         return classString.substring(dotIndex + 1);
198     }
199
200     public void actionPerformed(ActionEvent e) {
201         JMenuItem source = (JMenuItem) (e.getSource());
202         String JavaDoc s = source.getText();
203         if (s.equalsIgnoreCase("Quit")) {
204             System.out.println("Quit menu item selected!");
205             System.exit(0);
206         } else {
207             s = "Action event detected." + "\n" + " Event source: "
208                 + source + " (an instance of " + getClassName(source) + ")";
209
210             System.out.println(s);
211         }
212     }
213
214     public void itemStateChanged(ItemEvent e) {
215         JMenuItem source = (JMenuItem) (e.getSource());
216         String JavaDoc s = "Item event detected." + "\n" + " Event source: "
217                 + source.getText() + " (an instance of " + getClassName(source)
218                 + ")" + "\n" + " New state: "
219                 + ((e.getStateChange() == ItemEvent.SELECTED)
220                         ? "selected"
221                         : "unselected");
222
223         System.out.println(s);
224     }
225
226     public static void main(String JavaDoc[] args) {
227         new Tray();
228     }
229
230 }
231
Popular Tags