Group button java swing

How to Use the ButtonGroup Component

The ButtonGroup component manages the selected/unselected state for a set of buttons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time.

Initially, all buttons managed by a ButtonGroup instance are unselected.

How to Use ButtonGroup Features

You can use ButtonGroup with any set of objects that inherit from AbstractButton . Typically a button group contains instances of JRadioButton, JRadioButtonMenuItem , or JToggleButton . It would not make sense to put an instance of JButton or JMenuItem in a button group because JButton and JMenuItem do not implement the select/deselect button state.

In general, you will typically follow these steps to write code that uses a ButtonGroup component.

  1. Subclass JFrame
  2. Call ContextPane together with a layout manager
  3. Declare and configure a set of radio buttons or toggle buttons
  4. Instantiate a ButtonGroup object
  5. Call the add method on that buttongroup object in order to add each button to the group.
Читайте также:  Java simpledateformat text to date

For details and a code example, see How to Use Radio Buttons. It shows how to use a ButtonGroup component to group a set of RadioButtons set into a JPanel.

The ButtonGroup API

Commonly Used Button Group Constructors/Methods

Constructor or Method Purpose
ButtonGroup() Create a ButtonGroup instance.
void add(AbstractButton)
void remove(AbstractButton)
Add a button to the group, or remove a button from the group.
public ButtonGroup getGroup()
(in DefaultButtonModel )
Get the ButtonGroup , if any, that controls a button. For example:
ButtonGroup group = ((DefaultButtonModel)button.getModel()).getGroup();
public ButtonGroup clearSelection() Clears the state of selected buttons in the ButtonGroup. None of the buttons in the ButtonGroup are selected .

ButtonGroup Examples

The following demonstration application uses the ButtonGroup component to group radio buttons displaying on a Window.

Example Where Described Notes
RadioButtonDemo How to Use Radio Buttons Uses radio buttons to determine which of five images it should display.

Previous page: How to Use Buttons, Check Boxes, and Radio Buttons
Next page: How to Use Color Choosers

Источник

Group button java swing

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning «on» one of those buttons turns off all other buttons in the group. A ButtonGroup can be used with any set of objects that inherit from AbstractButton . Typically a button group contains instances of JRadioButton , JRadioButtonMenuItem , or JToggleButton . It wouldn’t make sense to put an instance of JButton or JMenuItem in a button group because JButton and JMenuItem don’t implement the selected state. Initially, all buttons in the group are unselected. For examples and further information on using button groups see How to Use Radio Buttons, a section in The Java Tutorial. 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 XMLEncoder .

Field Summary

Constructor Summary

Method Summary

Methods inherited from class java.lang.Object

Field Detail

buttons

Constructor Detail

ButtonGroup

Method Detail

add

remove

clearSelection

public void clearSelection()

getElements

public EnumerationAbstractButton> getElements()

getSelection

setSelected

Sets the selected value for the ButtonModel . Only one button in the group may be selected at a time.

isSelected

getButtonCount

public int getButtonCount()

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Group button java swing

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning «on» one of those buttons turns off all other buttons in the group. A ButtonGroup can be used with any set of objects that inherit from AbstractButton . Typically a button group contains instances of JRadioButton , JRadioButtonMenuItem , or JToggleButton . It wouldn’t make sense to put an instance of JButton or JMenuItem in a button group because JButton and JMenuItem don’t implement the selected state. Initially, all buttons in the group are unselected. For examples and further information on using button groups see How to Use Radio Buttons, a section in The Java Tutorial. 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 XMLEncoder .

Field Summary

Constructor Summary

Method Summary

Methods declared in class java.lang.Object

Field Detail

buttons

Constructor Detail

ButtonGroup

Method Detail

add

remove

clearSelection

public void clearSelection()

getElements

public EnumerationAbstractButton> getElements()

getSelection

setSelected

Sets the selected value for the ButtonModel . Only one button in the group may be selected at a time.

isSelected

getButtonCount

public int getButtonCount()

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Group button java swing

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning «on» one of those buttons turns off all other buttons in the group. A ButtonGroup can be used with any set of objects that inherit from AbstractButton . Typically a button group contains instances of JRadioButton , JRadioButtonMenuItem , or JToggleButton . It wouldn’t make sense to put an instance of JButton or JMenuItem in a button group because JButton and JMenuItem don’t implement the selected state. Initially, all buttons in the group are unselected. For examples and further information on using button groups see How to Use Radio Buttons, a section in The Java Tutorial. 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 XMLEncoder .

Field Summary

Constructor Summary

Method Summary

Methods inherited from class java.lang.Object

Field Detail

buttons

Constructor Detail

ButtonGroup

Method Detail

add

remove

clearSelection

public void clearSelection()

getElements

public EnumerationAbstractButton> getElements()

getSelection

setSelected

Sets the selected value for the ButtonModel . Only one button in the group may be selected at a time.

isSelected

getButtonCount

public int getButtonCount()

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью