Toggle Buttons (1)

Source: Internet
Author: User

Now we have learned about the relatively simple functions of the Swing component JLabel and JButton. Now we will learn about some more active components, especially those that can be switched. These are called switchable components-JToggleButton. JCheckBox and JRadioButton-provide our users with a method to select from an option set. These options are either open, closed, allowed, or disabled. In a ButtonGroup, only one option in each group can be selected. To process the selected state, the component shares a common data model with ToogleButtonModel. Next, let's take a look at the data model, using the component Combination Mechanism of ButtonGroup, and a single component.

5.1 ToggleButtonModel class
The JToggleButton. ToggleButtonMode class is a public inline class of JToggleButton. This class customizes the DefaultButtonModel class behavior and implements the ButtonModel interface.

User-Defined behaviors affect the data models of all AbstractButto in the ButtonGroup component. The ButtonGroup will discuss it later. To put it simply, a ButtonGroup is the logical combination of the AbstractButton component. At any time, the selected Attribute of only one actbutton component in the ButtonGroup is set to true, and others must be set to false. This does not mean that only one selected component exists at any time in the combination. If multiple components in the ButtonGroup share one ButtonModel, multiple selected components can exist in the combination. If no component sharing model exists, you can select at most one component in the combination. Once a user has selected a component, the user cannot cancel the selection for interaction. However, by programming, We Can deselect all the combination elements.

JToggleButton. ToggleButtonModel is defined as follows:

Public class ToggleButtonModel extends DefaultButtonModel {
// Constructors
Public ToggleButtonModel ();
// Properties
Public boolean isSelected ();
Public void setPressed (boolean newValue );
Public void setSelected (boolean newvalue );
} The ToggleButtonModel class is JToogleButton and the subclasses JCheckBox and JRadioButton described in later sections. The JCheckBoxMenuItem and JRadioButtonMenuItem classes described in Chapter 6th define the default data model.

5.2 ButtonGroup class
Before describing the ButtonGroup class, we will first demonstrate its usage. The program in List 5-1 creates an object using ToggleButtonModel and places it in a combination. As demonstrated by the program, in addition to adding components to the screen container, we must add each component to the same ButtonGroup. This leads to a one-to-one add () method call for each component. In addition, the button combination container places the component in a column and uses a border with a title to combine the user IDs, although these are not necessary. Figure 5-1 shows the program output.

Package swingstudy. ch04;
 
Import java. awt. BorderLayout;
Import java. awt. EventQueue;
Import java. awt. GridLayout;
 
Import javax. swing. AbstractButton;
Import javax. swing. BorderFactory;
Import javax. swing. ButtonGroup;
Import javax. swing. JCheckBox;
Import javax. swing. JCheckBoxMenuItem;
Import javax. swing. JFrame;
Import javax. swing. JPanel;
Import javax. swing. JRadioButton;
Import javax. swing. JRadioButtonMenuItem;
Import javax. swing. JToggleButton;
Import javax. swing. border. Border;
 
Public class AButtonGroup {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
 
Runnable runner = new Runnable (){
Public void run (){
JFrame frame = new JFrame ("Button Group ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
 
JPanel panel = new JPanel (new GridLayout (0, 1 ));
Border border = BorderFactory. createTitledBorder ("Examples ");
Panel. setBorder (border );
 
ButtonGroup group = new ButtonGroup ();
Export actbutton into act1 = new JToggleButton ("Toggle Button ");
Panel. add (effecact1 );
Group. add (abstract1 );
 
Effecactbutton extends Act2 = new JRadioButton ("Radio Button ");
Panel. add (effecact2 );
Group. add (abstract2 );
 
Required actbutton restart act3 = new JCheckBox ("Check Box ");
Panel. add (effecact3 );
Group. add (abstract3 );
 
Export actbutton into act4 = new JRadioButtonMenuItem ("Radio Button Menu Item ");
Panel. add (effecact4 );
Group. add (abstract4 );
 
Export actbutton into act5 = new JCheckBoxMenuItem ("Check Box Menu Item ");
Panel. add (effecact5 );
Group. add (abstract5 );
 
Frame. add (panel, BorderLayout. CENTER );
Frame. setSize (300,200 );
Frame. setVisible (true );
 
}
};
EventQueue. invokeLater (runner );
}
 
}

As mentioned above, the ButtonGroup class represents the logical combination of the AbstractButton component. ButtonGroup is not a visualization component. Therefore, when using ButtonGroup, there is no visible content on the screen. Any actbutton component can be added to the combination by using the public void add (AbstractButton extends actbutton) method. Although any actbutton component can belong to a ButtonGroup, the combination only takes effect when the data model of the component is ToggleButtonModel. The result of a component with a ToggleButtonModel in the ButtonGroup is that after the component is selected, the ButtonGroup will cancel the selected component in the selected combination.

Although the add () method is usually the only method we need, the following class definition shows that it is not the only method in the ButtonGroup:

Public class ButtonGroup implements Serializable {
// Constructor
Public ButtonGroup ();
// Properties
Public int getButtonCount ();
Public Enumeration getElements ();
Public ButtonModel getSelection ();
// Other methods
Public void add (AbstractButton aButton );
Public boolean isSelected (ButtonModel theModel );
Public void remove (AbstractButton aButton );
Public void setSelected (ButtonModel theModel, boolean newValue );
} An interesting thing shown in the class definition above is that given a ButtonGroup, we cannot directly determine the selected actbutton. We can only directly query which ButtonModel is selected. However, getElements () can return the Enumeration of all the AbstractButton elements in the combination. Then we can use code similar to the following to traverse all the buttons to determine the selected button:

Enumeration elements = group. getElements ();
While (elements. hasMoreElements ()){
Using actbutton button = (using actbutton) elements. nextElement ();
If (button. isSelected ()){
System. out. println ("The winner is:" + button. getText ());
Break; // Dont break if sharing models -- cocould show multiple buttons selected
}
} Another interesting method of ButtonGroup is setSelected (). The two parameters of this method are ButtonModel and boolean. If the value of boolean is false, the selected request is ignored. If the ButtonModel is not a button model in the ButtonGroup, The ButtonGroup cancels the selected model so that no button in the combination is selected. The correct use of this method is to call the method using the model of the component in the combination and a new state of true. For example, if aButton is an actbutton and aGroup is a ButtonGroup, the method call is similar to aGroup. setSelected (aButton. getModel (), true ).

Next, let's take a look at the various components whose data model is ToggleButtonModel.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.