-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioMenuItem.java
More file actions
78 lines (72 loc) · 1.91 KB
/
Copy pathRadioMenuItem.java
File metadata and controls
78 lines (72 loc) · 1.91 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
/**
* java.awt does not have a RadioMenuItem which can be used with the System
* Tray, so I made one. This class extends java.awt.CheckboxMenuItem, and
* therefore acts very similar. The difference is that only one radio menu
* item can be checked per group. This is meant to be used with
* the RadioMenuItemGroup class you should have received with this file.
*
* @title RadioMenuItem.java
* @author uPaymeiFixit
* @version 1.2
* @since 2015-08-17
*/
import java.awt.CheckboxMenuItem;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class RadioMenuItem extends CheckboxMenuItem
{
private ItemListener listener;
private RadioMenuItemGroup group;
// I don't know what this is but I needed it apparently.
public static final long serialVersionUID = 42L;
public RadioMenuItem()
{
super();
}
public RadioMenuItem( String label )
{
super( label );
}
public RadioMenuItem( String label, RadioMenuItemGroup group )
{
super( label );
setRadioMenuItemGroup( group );
}
public RadioMenuItem( String label, boolean state )
{
super( label, state );
}
public RadioMenuItem( String label, RadioMenuItemGroup group, boolean state )
{
super( label, state );
setRadioMenuItemGroup( group );
}
public RadioMenuItemGroup getRadioMenuItemGroup()
{
return group;
}
public void setRadioMenuItemGroup( RadioMenuItemGroup group )
{
this.group = group;
this.removeItemListener( listener );
if ( group != null )
{
// We need to make sure "this" is final so that we can refer to it
// in the scope below. Some compilers make you do this.
final RadioMenuItem me = this;
listener = new ItemListener()
{
@Override
public void itemStateChanged( ItemEvent e )
{
if ( e.getStateChange() == ItemEvent.SELECTED )
{
me.group.setSelectedRadioMenuItem( me );
}
}
};
this.addItemListener( listener );
group.addRadioMenuItem( this );
}
}
}