All Classes Namespaces Files Functions Variables
RadioButtonChooser.java
Go to the documentation of this file.
1 package fi.jyu.mit.fxgui;
2 
3 import java.util.ArrayList;
4 
5 import javafx.beans.property.BooleanProperty;
6 import javafx.beans.property.SimpleBooleanProperty;
7 import javafx.beans.value.ChangeListener;
8 import javafx.scene.control.RadioButton;
9 import javafx.scene.control.ToggleGroup;
10 
11 /**
12  * Allows multiple radiobuttons to be selected and managed with only 1 component
13  * @author terop
14  * @version 13.1.2017
15  * @param <T> Type to be stored in to this class
16  */
17 public class RadioButtonChooser<T> extends BaseBoxChooser<T, RadioButton> {
18 
19  private ToggleGroup group = new ToggleGroup();
20  /**
21  * Should multiple selections be allowed
22  */
23  protected BooleanProperty multipleSelectionsAllowed = new SimpleBooleanProperty(false);
24 
25  @Override
26  protected void addChangeListener(RadioButton box, ChangeListener<Boolean> listener) {
27  box.selectedProperty().addListener(listener);
28  }
29 
30  @Override
31  protected RadioButton addCorrectComponent(String name, T object) {
32  RadioButton button = new RadioButton(name);
33  if(!multipleSelectionsAllowed.get())
34  button.setToggleGroup(group);
35  button.setMnemonicParsing(true);
36  return button;
37  }
38 
39 
40 
41  @Override
42  protected void removeListener(RadioButton node, ArrayList<ChangeListener<Boolean>> list) {
43  for (ChangeListener<Boolean> changeListener : list) {
44  node.selectedProperty().removeListener(changeListener);
45  }
46 
47  }
48 
49  /**
50  * Sets if selecting multiple toggles is allowed
51  * @param allowMultiples if multiples are allowed
52  */
53  public void setMultipleSelectionsAllowed(boolean allowMultiples){
54  this.multipleSelectionsAllowed.set(allowMultiples);
55  UpdateComponents(getItems());
56  }
57 
58  /**
59  * Gets the multiple selection allowed propertys value
60  * @return if multiple selections are allowed
61  */
62  public boolean getMultipleSelectionsAllowed(){
63  return this.multipleSelectionsAllowed.get();
64  }
65 
66 
67  @Override
68  protected boolean isComponentSelected(RadioButton component) {
69  return component.selectedProperty().get();
70  }
71 
72 
73  @Override
74  protected boolean setComponentSelected(RadioButton component, boolean value) {
75  boolean oldSelect = isComponentSelected(component);
76  component.selectedProperty().set(value);
77  return oldSelect;
78  }
79 
80 }