< prev index next >
src/java.desktop/share/classes/javax/swing/JToggleButton.java
Print this page
@@ -32,10 +32,11 @@
import javax.swing.plaf.*;
import javax.accessibility.*;
import java.io.ObjectOutputStream;
import java.io.IOException;
+import java.util.Enumeration;
/**
* An implementation of a two-state button.
* The <code>JRadioButton</code> and <code>JCheckBox</code> classes
* are subclasses of this class.
@@ -206,10 +207,108 @@
*/
boolean shouldUpdateSelectedStateFromAction() {
return true;
}
+ private JToggleButton getGroupSelection() {
+ ButtonModel model = getModel();
+ JToggleButton selection = this;
+ if (model instanceof DefaultButtonModel) {
+ ButtonGroup group = ((DefaultButtonModel) model).getGroup();
+ if (group != null && group.getSelection() != null
+ && !group.isSelected(model)) {
+ Enumeration<AbstractButton> elements = group.getElements();
+ while (elements.hasMoreElements()) {
+ AbstractButton member = elements.nextElement();
+ if (group.isSelected(member.getModel())) {
+ if (member instanceof JToggleButton &&
+ member.isVisible() && member.isDisplayable() &&
+ member.isEnabled() && member.isFocusable()) {
+ selection = (JToggleButton) member;
+ }
+ break;
+ }
+ }
+ }
+ }
+ return selection;
+ }
+
+ /**
+ * If this toggle button is a member of the {@link ButtonGroup} which has
+ * an another focusable toggle button selected, and the focus cause argument
+ * denotes window activation or focus traversal action of any direction
+ * the result of the method execution is the same as calling
+ * {@link Component#requestFocus(FocusEvent.Cause)} on the toggle button
+ * selected in the group.
+ * In all other cases the result of the method is the same as calling
+ * {@link Component#requestFocus(FocusEvent.Cause)} on this toggle button.
+ *
+ * @param cause the cause why the focus is requested
+ * @see ButtonGroup
+ * @see Component#requestFocus(FocusEvent.Cause)
+ * @see FocusEvent.Cause
+ *
+ * @since 9
+ */
+ @Override
+ public void requestFocus(FocusEvent.Cause cause) {
+ switch (cause) {
+ case ACTIVATION:
+ case TRAVERSAL:
+ case TRAVERSAL_UP:
+ case TRAVERSAL_DOWN:
+ case TRAVERSAL_FORWARD:
+ case TRAVERSAL_BACKWARD:
+ getGroupSelection().requestFocusUnconditionally(cause);
+ break;
+ default:
+ requestFocusUnconditionally(cause);
+ }
+ }
+
+ private void requestFocusUnconditionally(FocusEvent.Cause cause) {
+ super.requestFocus(cause);
+ }
+
+ /**
+ * If this toggle button is a member of the {@link ButtonGroup} which has
+ * an another focusable button selected, and the focus cause argument
+ * denotes window activation or focus traversal action of any direction
+ * the result of the method execution is the same as calling
+ * {@link Component#requestFocusInWindow(FocusEvent.Cause)} on the group
+ * selection button.
+ * In all other cases the result of the method is the same as calling
+ * {@link Component#requestFocusInWindow(FocusEvent.Cause)} on this toggle
+ * button.
+ *
+ * @param cause the cause why the focus is requested
+ * @see ButtonGroup
+ * @see Component#requestFocusInWindow(FocusEvent.Cause)
+ * @see FocusEvent.Cause
+ *
+ * @since 9
+ */
+ public boolean requestFocusInWindow(FocusEvent.Cause cause) {
+ switch (cause) {
+ case ACTIVATION:
+ case TRAVERSAL:
+ case TRAVERSAL_UP:
+ case TRAVERSAL_DOWN:
+ case TRAVERSAL_FORWARD:
+ case TRAVERSAL_BACKWARD:
+ return getGroupSelection().
+ requestFocusInWindowUnconditionally(cause);
+ default:
+ return requestFocusInWindowUnconditionally(cause);
+ }
+ }
+
+ private boolean requestFocusInWindowUnconditionally(FocusEvent.Cause cause) {
+ return super.requestFocusInWindow(cause);
+ }
+
// *********************************************************************
/**
* The ToggleButton model
* <p>
< prev index next >