Package io.github.parubok.button.group
Class KButtonGroup<K extends AbstractButton>
- java.lang.Object
-
- javax.swing.ButtonGroup
-
- io.github.parubok.button.group.KButtonGroup<K>
-
- Type Parameters:
K- Type of the buttons in the group.
- All Implemented Interfaces:
Serializable,Iterable<K>
public class KButtonGroup<K extends AbstractButton> extends ButtonGroup implements Iterable<K>
Extension of SwingButtonGroupwhich adds the following functionality:- Type-safety via generics.
- Auto-selects 1st added button.
- Group listeners.
- Mnemonics.
- Access to the buttons by index.
- Implements
Iterableto iterate over buttons in the group.
- See Also:
ButtonModel.setGroup(ButtonGroup), Serialized Form- Implementation Note:
- Adds
ItemListenerto the buttons in the group.
-
-
Field Summary
-
Fields inherited from class javax.swing.ButtonGroup
buttons
-
-
Constructor Summary
Constructors Constructor Description KButtonGroup(boolean autoSelectFirstButton)Constructs an empty group.KButtonGroup(Collection<K> buttons)Constructs group with the specified buttons.KButtonGroup(K... buttons)Constructs group with the specified buttons.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(AbstractButton button)Deprecated.This method is not type-safe.voidaddButton(K button)voidaddButtons(K... buttons)voidaddListener(KButtonGroupListener<K> listener)KButtonGroupEventwill be fired when there is a new selected button in the group.KgetButton(int index)List<K>getButtons()intgetLastIndex()KgetSelectedButton()intgetSelectedIndex()booleanisAutoSelectFirstButton()Iterator<K>iterator()voidremove(AbstractButton button)voidremoveListener(KButtonGroupListener<K> listener)voidsetMnemonics()Auto-assigns mnemonic characters to the buttons of this group.voidsetSelectedIndex(int index)StringtoString()-
Methods inherited from class javax.swing.ButtonGroup
clearSelection, getButtonCount, getElements, getSelection, isSelected, setSelected
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
KButtonGroup
@SafeVarargs public KButtonGroup(K... buttons)
Constructs group with the specified buttons. The first added button will be automatically selected.- Parameters:
buttons- Buttons to add to the group.
-
KButtonGroup
public KButtonGroup(Collection<K> buttons)
Constructs group with the specified buttons. The first added button will be automatically selected.- Parameters:
buttons- Buttons to add to the group.
-
KButtonGroup
public KButtonGroup(boolean autoSelectFirstButton)
Constructs an empty group.- Parameters:
autoSelectFirstButton- If true, the first added button will be automatically selected viaAbstractButton.setSelected(boolean).
-
-
Method Detail
-
addButton
public void addButton(K button)
- Parameters:
button- Button to add to the group. Not null.
-
addButtons
@SafeVarargs public final void addButtons(K... buttons)
-
add
@Deprecated public void add(AbstractButton button)
Deprecated.This method is not type-safe. UseaddButton(AbstractButton)instead.- Overrides:
addin classButtonGroup
-
remove
public void remove(AbstractButton button)
- Overrides:
removein classButtonGroup
-
addListener
public void addListener(KButtonGroupListener<K> listener)
KButtonGroupEventwill be fired when there is a new selected button in the group.- Implementation Specification:
- No event is fired if the selection becomes empty.
-
removeListener
public void removeListener(KButtonGroupListener<K> listener)
-
setMnemonics
public void setMnemonics()
Auto-assigns mnemonic characters to the buttons of this group.- See Also:
AbstractButton.setMnemonic(char)
-
getButton
public K getButton(int index)
- Returns:
- Button at the specified index.
-
getSelectedButton
public K getSelectedButton()
- Returns:
- The group's selected button or
nullif no button is selected.
-
getSelectedIndex
public int getSelectedIndex()
- Returns:
- Index of the group's selected button or -1 if no button is selected.
-
getLastIndex
public int getLastIndex()
- Returns:
- Maximum button index in the group.
- Throws:
NoSuchElementException- If the group is empty.
-
setSelectedIndex
public void setSelectedIndex(int index)
- Parameters:
index- Index of the button to select.- See Also:
getSelectedIndex()
-
iterator
public Iterator<K> iterator()
- Specified by:
iteratorin interfaceIterable<K extends AbstractButton>
-
isAutoSelectFirstButton
public boolean isAutoSelectFirstButton()
-
-