-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
85 lines (72 loc) · 2.49 KB
/
Copy pathExample.java
File metadata and controls
85 lines (72 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* This is an example showing how to make full use of the RadioMenuItem
* and RadioMenuItemGroup classes you should have received with this file.
*
* @title Example.java
* @author uPaymeiFixit
* @version 1.2
* @since 2015-08-17
*/
import java.awt.AWTException;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Example
{
public static void main( String[] args )
{
if ( SystemTray.isSupported() )
{
final PopupMenu popup = new PopupMenu();
final Image image = Toolkit.getDefaultToolkit().getImage("icon.gif");
final TrayIcon trayIcon = new TrayIcon(image, "Tray Icon");
final SystemTray tray = SystemTray.getSystemTray();
// Group 1
RadioMenuItemGroup g1 = new RadioMenuItemGroup();
RadioMenuItem a1 = new RadioMenuItem( "A1", g1 );
RadioMenuItem a2 = new RadioMenuItem( "A2", true );
a2.setRadioMenuItemGroup( g1 );
RadioMenuItem a3 = new RadioMenuItem( "A3", g1, true );
RadioMenuItem a4 = new RadioMenuItem( "A4" );
g1.addRadioMenuItem( a4 );
g1.setSelectedRadioMenuItem( a4 );
System.out.println( g1.getSelectedRadioMenuItem().getLabel() );
// A4
System.out.println( g1 );
// [A1: false, A2: false, A3: false, A4: true]
// Add a listener so we can act when the user clicks this item
a4.addItemListener( new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
if ( e.getStateChange() == ItemEvent.SELECTED )
System.out.println( a4.getLabel() + " has been checked." );
else
System.out.println( a4.getLabel() + " has been unchecked" );
}
});
popup.add(a1);
popup.add(a2);
popup.add(a3);
popup.add(a4);
trayIcon.setPopupMenu(popup);
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.out.println("TrayIcon could not be loaded.");
}
}
else
{
System.out.println("Tray is not supported");
}
}
}