Top Description Inners Fields Constructors Methods
javax.swing

public Class JToggleButton

extends AbstractButton
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
javax.swing.JCheckBox, javax.swing.JRadioButton
Annotations
@JavaBean
defaultProperty:UIClassID
description:An implementation of a two-state button.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.AWTEvent, .Component, .EventQueue, java.awt.event.ActionEvent, .FocusEvent, .InputEvent, .ItemEvent, .ItemListener, java.beans.BeanProperty, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, java.util.Iterator, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, .AccessibleState, javax.swing.plaf.ButtonUI

An implementation of a two-state button. The JRadioButton and JCheckBox classes are subclasses of this class. For information on using them see How to Use Buttons, Check Boxes, and Radio Buttons, a section in The Java Tutorial.

Buttons can be configured, and to some degree controlled, by Actions. Using an Action with a button has many benefits beyond directly configuring a button. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Jeff Dinkins
Since
1.2
See Also
JRadioButton, JCheckBox

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JToggleButton.AccessibleJToggleButton

This class implements accessibility support for the JToggleButton class.

public static class
JToggleButton.ToggleButtonModel

The ToggleButton model

Warning

Serialized objects of this class will not be compatible with future Swing releases.

Field Summary

Modifier and TypeField and Description
private static final String
Inherited from javax.swing.AbstractButton:
actionListenerBORDER_PAINTED_CHANGED_PROPERTYchangeEventchangeListenerCONTENT_AREA_FILLED_CHANGED_PROPERTYdefaultCapableDISABLED_ICON_CHANGED_PROPERTYDISABLED_SELECTED_ICON_CHANGED_PROPERTYFOCUS_PAINTED_CHANGED_PROPERTYHORIZONTAL_ALIGNMENT_CHANGED_PROPERTYHORIZONTAL_TEXT_POSITION_CHANGED_PROPERTYICON_CHANGED_PROPERTYitemListenerMARGIN_CHANGED_PROPERTYMNEMONIC_CHANGED_PROPERTYmodelMODEL_CHANGED_PROPERTYPRESSED_ICON_CHANGED_PROPERTYROLLOVER_ENABLED_CHANGED_PROPERTYROLLOVER_ICON_CHANGED_PROPERTYROLLOVER_SELECTED_ICON_CHANGED_PROPERTYSELECTED_ICON_CHANGED_PROPERTYTEXT_CHANGED_PROPERTYVERTICAL_ALIGNMENT_CHANGED_PROPERTYVERTICAL_TEXT_POSITION_CHANGED_PROPERTY

Constructor Summary

AccessConstructor and Description
public
JToggleButton()

Creates an initially unselected toggle button without setting the text or image.

public
JToggleButton(Icon
the image that the button should display
icon
)

Creates an initially unselected toggle button with the specified image but no text.

public
JToggleButton(Icon
the image that the button should display
icon
,
boolean
if true, the button is initially selected; otherwise, the button is initially unselected
selected
)

Creates a toggle button with the specified image and selection state, but no text.

public
JToggleButton(String
the string displayed on the toggle button
text
)

Creates an unselected toggle button with the specified text.

public
JToggleButton(String
the string displayed on the toggle button
text
,
boolean
if true, the button is initially selected; otherwise, the button is initially unselected
selected
)

Creates a toggle button with the specified text and selection state.

public
JToggleButton(Action
an instance of an Action
a
)

Creates a toggle button where properties are taken from the Action supplied.

public
JToggleButton(String
the string displayed on the button
text
,
Icon
the image that the button should display
icon
)

Creates a toggle button that has the specified text and image, and that is initially unselected.

public
JToggleButton(String
the text of the toggle button
text
,
Icon
the image that the button should display
icon
,
boolean
if true, the button is initially selected; otherwise, the button is initially unselected
selected
)

Creates a toggle button with the specified text, image, and selection state.

Method Summary

Modifier and TypeMethod and Description
public AccessibleContext

Returns:

an AccessibleJToggleButton that serves as the AccessibleContext of this JToggleButton
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JToggleButton.
private JToggleButton
public String

Returns:

the string "ToggleButtonUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns a string that specifies the name of the l&f class that renders this component.
protected String

Returns:

a string representation of this JToggleButton.
paramString
()

Overrides javax.swing.AbstractButton.paramString.

Returns a string representation of this JToggleButton.
public void
requestFocus(FocusEvent.Cause
the cause why the focus is requested
cause
)

Overrides java.awt.Component.requestFocus.

If this toggle button is a member of the ButtonGroup which has another toggle button which is selected and can be the focus owner, 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 Component#requestFocus(FocusEvent.Cause) on the toggle button selected in the group.
public boolean
requestFocusInWindow(FocusEvent.Cause
the cause why the focus is requested
cause
)

Overrides java.awt.Component.requestFocusInWindow.

If this toggle button is a member of the ButtonGroup which has another toggle button which is selected and can be the focus owner, 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 Component#requestFocusInWindow(FocusEvent.Cause) on the toggle button selected in the group.
private boolean
private void
pack-priv boolean
shouldUpdateSelectedStateFromAction()

Overrides javax.swing.AbstractButton.shouldUpdateSelectedStateFromAction.

Overridden to return true, JToggleButton supports the selected state.
public void
updateUI()

Overrides javax.swing.AbstractButton.updateUI.

Resets the UI property to a value from the current look and feel.
private void
writeObject(ObjectOutputStream
the ObjectOutputStream in which to write
s
)

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.
Inherited from javax.swing.AbstractButton:
actionPropertyChangedaddActionListeneraddChangeListeneraddImpladdItemListenercheckHorizontalKeycheckVerticalKeyclientPropertyChangedconfigurePropertiesFromActioncreateActionListenercreateActionPropertyChangeListenercreateActionPropertyChangeListener0createChangeListenercreateItemListenerdoClickdoClickfireActionPerformedfireItemStateChangedfireStateChangedgetActiongetActionCommandgetActionListenersgetChangeListenersgetDisabledIcongetDisabledSelectedIcongetDisplayedMnemonicIndexgetHideActionTextgetHorizontalAlignmentgetHorizontalTextPositiongetIcongetIconTextGapgetItemListenersgetLabelgetMargingetMnemonicgetModelgetMultiClickThreshholdgetPressedIcongetRolloverIcongetRolloverSelectedIcongetSelectedIcongetSelectedObjectsgetTextgetUIgetVerticalAlignmentgetVerticalTextPositionimageUpdateinitisBorderPaintedisContentAreaFilledisFocusPaintedisRolloverEnabledisSelectedlargeIconChangedpaintBorderremoveActionListenerremoveChangeListenerremoveItemListenerremoveNotifysetActionsetActionCommandsetBorderPaintedsetContentAreaFilledsetDisabledIconsetDisabledSelectedIconsetDisplayedMnemonicIndexsetEnabledsetFocusPaintedsetHideActionTextsetHorizontalAlignmentsetHorizontalTextPositionsetIconsetIconFromActionsetIconTextGapsetLabelsetLayoutsetMarginsetMnemonicsetMnemonicsetModelsetMultiClickThreshholdsetPressedIconsetRolloverEnabledsetRolloverIconsetRolloverSelectedIconsetSelectedsetSelectedIconsetTextsetUIsetUIPropertysetVerticalAlignmentsetVerticalTextPositionsmallIconChanged

Field Detail

uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JComponent.uiClassID.

See Also
getUIClassID, readObject

Constructor Detail

JToggleButtonback to summary
public JToggleButton()

Creates an initially unselected toggle button without setting the text or image.

JToggleButtonback to summary
public JToggleButton(Icon icon)

Creates an initially unselected toggle button with the specified image but no text.

Parameters
icon:Icon

the image that the button should display

JToggleButtonback to summary
public JToggleButton(Icon icon, boolean selected)

Creates a toggle button with the specified image and selection state, but no text.

Parameters
icon:Icon

the image that the button should display

selected:boolean

if true, the button is initially selected; otherwise, the button is initially unselected

JToggleButtonback to summary
public JToggleButton(String text)

Creates an unselected toggle button with the specified text.

Parameters
text:String

the string displayed on the toggle button

JToggleButtonback to summary
public JToggleButton(String text, boolean selected)

Creates a toggle button with the specified text and selection state.

Parameters
text:String

the string displayed on the toggle button

selected:boolean

if true, the button is initially selected; otherwise, the button is initially unselected

JToggleButtonback to summary
public JToggleButton(Action a)

Creates a toggle button where properties are taken from the Action supplied.

Parameters
a:Action

an instance of an Action

Since
1.3
JToggleButtonback to summary
public JToggleButton(String text, Icon icon)

Creates a toggle button that has the specified text and image, and that is initially unselected.

Parameters
text:String

the string displayed on the button

icon:Icon

the image that the button should display

JToggleButtonback to summary
public JToggleButton(String text, Icon icon, boolean selected)

Creates a toggle button with the specified text, image, and selection state.

Parameters
text:String

the text of the toggle button

icon:Icon

the image that the button should display

selected:boolean

if true, the button is initially selected; otherwise, the button is initially unselected

Method Detail

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JToggleButton. For toggle buttons, the AccessibleContext takes the form of an AccessibleJToggleButton. A new AccessibleJToggleButton instance is created if necessary.

Returns:AccessibleContext

an AccessibleJToggleButton that serves as the AccessibleContext of this JToggleButton

Annotations
@BeanProperty
bound:false
expert:true
description:The AccessibleContext associated with this ToggleButton.
getGroupSelectionback to summary
private JToggleButton getGroupSelection(FocusEvent.Cause cause)
getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JComponent.getUIClassID.

Returns a string that specifies the name of the l&f class that renders this component.

Returns:String

the string "ToggleButtonUI"

Annotations
@BeanProperty
bound:false
description:A string that specifies the name of the L&F class
See Also
JComponent#getUIClassID, UIDefaults#getUI
paramStringback to summary
protected String paramString()

Overrides javax.swing.AbstractButton.paramString.

Returns a string representation of this JToggleButton. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns:String

a string representation of this JToggleButton.

requestFocusback to summary
public void requestFocus(FocusEvent.Cause cause)

Overrides java.awt.Component.requestFocus.

If this toggle button is a member of the ButtonGroup which has another toggle button which is selected and can be the focus owner, 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 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 Component#requestFocus(FocusEvent.Cause) on this toggle button.

Parameters
cause:FocusEvent.Cause

the cause why the focus is requested

Annotations
@Override
Since
9
See Also
ButtonGroup, Component#requestFocus(FocusEvent.Cause), FocusEvent.Cause
requestFocusInWindowback to summary
public boolean requestFocusInWindow(FocusEvent.Cause cause)

Overrides java.awt.Component.requestFocusInWindow.

If this toggle button is a member of the ButtonGroup which has another toggle button which is selected and can be the focus owner, 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 Component#requestFocusInWindow(FocusEvent.Cause) on the toggle button selected in the group. In all other cases the result of the method is the same as calling Component#requestFocusInWindow(FocusEvent.Cause) on this toggle button.

Parameters
cause:FocusEvent.Cause

the cause why the focus is requested

Returns:boolean

Doc from java.awt.Component.requestFocusInWindow.

false if the focus change request is guaranteed to fail; true if it is likely to succeed

Since
9
See Also
ButtonGroup, Component#requestFocusInWindow(FocusEvent.Cause), FocusEvent.Cause
requestFocusInWindowUnconditionallyback to summary
private boolean requestFocusInWindowUnconditionally(FocusEvent.Cause cause)
requestFocusUnconditionallyback to summary
private void requestFocusUnconditionally(FocusEvent.Cause cause)
shouldUpdateSelectedStateFromActionback to summary
pack-priv boolean shouldUpdateSelectedStateFromAction()

Overrides javax.swing.AbstractButton.shouldUpdateSelectedStateFromAction.

Overridden to return true, JToggleButton supports the selected state.

updateUIback to summary
public void updateUI()

Overrides javax.swing.AbstractButton.updateUI.

Resets the UI property to a value from the current look and feel.

See Also
JComponent#updateUI
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Parameters
s:ObjectOutputStream

Doc from javax.swing.JComponent.writeObject.

the ObjectOutputStream in which to write

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs

javax.swing back to summary

protected Class JToggleButton.AccessibleJToggleButton

extends AccessibleAbstractButton
implements ItemListener
Class Inheritance
All Implemented Interfaces
java.awt.event.ItemListener, java.util.EventListener
Known Direct Subclasses
javax.swing.JCheckBox.AccessibleJCheckBox, javax.swing.JRadioButton.AccessibleJRadioButton
Annotations
@SuppressWarnings:serial

This class implements accessibility support for the JToggleButton class. It provides an implementation of the Java Accessibility API appropriate to toggle button user-interface elements.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Constructor Summary

AccessConstructor and Description
public
AccessibleJToggleButton()

Constructs AccessibleJToggleButton

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.
public void
itemStateChanged(ItemEvent
the event to be processed
e
)

Implements java.awt.event.ItemListener.itemStateChanged.

Fire accessible property change events when the state of the toggle button changes.
Inherited from javax.swing.AbstractButton.AccessibleAbstractButton:
doAccessibleActiongetAccessibleActiongetAccessibleActionCountgetAccessibleActionDescriptiongetAccessibleExtendedComponentgetAccessibleIcongetAccessibleKeyBindinggetAccessibleNamegetAccessibleRelationSetgetAccessibleStateSetgetAccessibleTextgetAccessibleValuegetAfterIndexgetAtIndexgetBeforeIndexgetCaretPositiongetCharacterAttributegetCharacterBoundsgetCharCountgetCurrentAccessibleValuegetIndexAtPointgetMaximumAccessibleValuegetMinimumAccessibleValuegetSelectedTextgetSelectionEndgetSelectionStartgetTitledBorderTextgetToolTipTextsetCurrentAccessibleValue

Constructor Detail

AccessibleJToggleButtonback to summary
public AccessibleJToggleButton()

Constructs AccessibleJToggleButton

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

itemStateChangedback to summary
public void itemStateChanged(ItemEvent e)

Implements java.awt.event.ItemListener.itemStateChanged.

Fire accessible property change events when the state of the toggle button changes.

Parameters
e:ItemEvent

Doc from java.awt.event.ItemListener.itemStateChanged.

the event to be processed

javax.swing back to summary

public Class JToggleButton.ToggleButtonModel

extends DefaultButtonModel
Class Inheritance
Annotations
@SuppressWarnings:serial

The ToggleButton model

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Field Summary

Inherited from javax.swing.DefaultButtonModel:
actionCommandARMEDchangeEventENABLEDgrouplistenerListmnemonicPRESSEDROLLOVERSELECTEDstateMask

Constructor Summary

AccessConstructor and Description
public
ToggleButtonModel()

Creates a new ToggleButton Model

Method Summary

Modifier and TypeMethod and Description
public boolean
isSelected()

Overrides javax.swing.DefaultButtonModel.isSelected.

Implements javax.swing.ButtonModel.isSelected.

Checks if the button is selected.
public void
setPressed(boolean
whether or not the button should be pressed
b
)

Overrides javax.swing.DefaultButtonModel.setPressed.

Implements javax.swing.ButtonModel.setPressed.

Sets the pressed state of the toggle button.
public void
setSelected(boolean
true selects the toggle button, false deselects the toggle button.
b
)

Overrides javax.swing.DefaultButtonModel.setSelected.

Implements javax.swing.ButtonModel.setSelected.

Sets the selected state of the button.
Inherited from javax.swing.DefaultButtonModel:
addActionListeneraddChangeListeneraddItemListenerfireActionPerformedfireItemStateChangedfireStateChangedgetActionCommandgetActionListenersgetChangeListenersgetGroupgetItemListenersgetListenersgetMnemonicgetSelectedObjectsisArmedisEnabledisMenuItemisPressedisRolloverremoveActionListenerremoveChangeListenerremoveItemListenersetActionCommandsetArmedsetEnabledsetGroupsetMenuItemsetMnemonicsetRollover

Constructor Detail

ToggleButtonModelback to summary
public ToggleButtonModel()

Creates a new ToggleButton Model

Method Detail

isSelectedback to summary
public boolean isSelected()

Overrides javax.swing.DefaultButtonModel.isSelected.

Implements javax.swing.ButtonModel.isSelected.

Checks if the button is selected.

Returns:boolean

Doc from javax.swing.ButtonModel.isSelected.

true if the button is selected

setPressedback to summary
public void setPressed(boolean b)

Overrides javax.swing.DefaultButtonModel.setPressed.

Implements javax.swing.ButtonModel.setPressed.

Sets the pressed state of the toggle button.

Parameters
b:boolean

Doc from javax.swing.ButtonModel.setPressed.

whether or not the button should be pressed

Annotations
@SuppressWarnings:deprecation
setSelectedback to summary
public void setSelected(boolean b)

Overrides javax.swing.DefaultButtonModel.setSelected.

Implements javax.swing.ButtonModel.setSelected.

Sets the selected state of the button.

Parameters
b:boolean

true selects the toggle button, false deselects the toggle button.