-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonPanel.java
More file actions
40 lines (35 loc) · 1.12 KB
/
ButtonPanel.java
File metadata and controls
40 lines (35 loc) · 1.12 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
package optionDialog;
import javax.swing.*;
/**
* A panel with radio buttons inside a titled border.
*/
public class ButtonPanel extends JPanel {
private ButtonGroup group;
/**
* Construct a button panel.
*
* @param title the title shown in the button
* @param options an array of radio button labels
*/
public ButtonPanel(String title, String... options) {
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
group = new ButtonGroup();
// make one radio button for each option
for (String option : options) {
JRadioButton button = new JRadioButton(option);
button.setActionCommand(option);
add(button);
group.add(button);
button.setSelected(option == options[0]);
}
}
/**
* Gets the currently selected option.
*
* @return the label of the currently selected radio button.
*/
public String getSelection() {
return group.getSelection().getActionCommand();
}
}