-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioMenuItemGroup.java
More file actions
85 lines (75 loc) · 2.55 KB
/
Copy pathRadioMenuItemGroup.java
File metadata and controls
85 lines (75 loc) · 2.55 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
/**
* java.awt does not have a RadioMenuItem which can be used with the System
* Tray, so I made one. This class extends allows you to make full use of the
* RadioMenuItem class you should have received with this file by grouping
* them together in one object so only one can be set “on” at a time.
*
* @title RadioMenuItemGroup.java
* @author uPaymeiFixit
* @version 1.2
* @since 2015-08-17
*/
import java.util.ArrayList;
public class RadioMenuItemGroup
{
ArrayList<RadioMenuItem> radios;
// Default methods from java.awt.CheckboxGroup
public RadioMenuItemGroup()
{
radios = new ArrayList<RadioMenuItem>();
}
// Gets the current choice from this radio menu item group. The current
// choice is the radio menu item in this group that is currently in the "on"
// state, or null if all radio menu itemes in the group are off.
public RadioMenuItem getSelectedRadioMenuItem()
{
for ( RadioMenuItem radio : radios )
if ( radio.getState() )
return radio;
return null;
}
// Sets the currently selected radio menu item in this group to be the
// specified radio menu item. This method sets the state of that radio menu
// item to "on" and sets all other radio menu itemes in the group to be off.
// If the radio menu item argument is null, all radio menu itemes in this
// radio menu item group are deselected. If the radio menu item argument
// belongs to a different radio menu item group, this method does nothing.
public void setSelectedRadioMenuItem( RadioMenuItem radio )
{
// If the user gave us a random RadioMenuItem not in this group, add it
if ( radio.getRadioMenuItemGroup() != this )
radio.setRadioMenuItemGroup( this );
// Set all of the radios to off
for ( RadioMenuItem local_radio : radios )
local_radio.setState( false );
// Turn our radio on
radio.setState( true );
}
// Returns a string representation of this radio menu item group, including
// the value of its current selection.
public String toString()
{
// I don't know exactly how CheckboxGroup's toString works
String output = "[";
for ( RadioMenuItem radio : radios )
output += radio.getLabel() + ": " + radio.getState() + ", ";
return output.substring( 0, output.length() - 2 )+"]";
}
// Custom methods
public void addRadioMenuItem( RadioMenuItem radio )
{
// If the user gave us a random RadioMenuItem not in this group, add it
if ( radio.getRadioMenuItemGroup() != this )
{
radio.setRadioMenuItemGroup( this );
}
else
{
radios.add( radio );
if ( radio.getState() )
{
setSelectedRadioMenuItem( radio );
}
}
}
}