Toggle Buttons (3)

Source: Internet
Author: User

5.4 JCheckBox class

The JCheckBox class indicates switching components. By default, this component displays a check box icon near the text label for two status options. The check box uses an optional check mark to display the current status of the object, rather than holding the button down for example, JToggleButton. For JCheckBox, the icon displays the object status, while for JToggleButton, the icon is part of the label and is not usually used to display the status information. The two components of JCheckBox and JToggleButton are the same except for the UI components. Figure 5-5 shows the check boxes in a pizza scheduler.

JCheckBox consists of several parts. Similar to JToggleButton, JCheckBox uses a ToggleButtonModel to represent its data model. The User Interface delegate is CheckBoxUI. Although ButtonGroup can be used to combine check boxes, this is usually not suitable. When multiple JCheckBox components are located in one ButtonGroup, their behavior is similar to the JRadioButton component, but it looks like a JCheckBox component. For visualization, we should not place the JCheckBox component in the ButtonGroup.

Now we know the different parts of JCheckBox. Let's take a look at how to use it.

5.4.1 create a JCheckBox component

JCheckBox has eight constructors:

public JCheckBox()JCheckBox aCheckBox = new JCheckBox(); public JCheckBox(Icon icon)JCheckBox aCheckBox = new JCheckBox(new DiamondIcon(Color.RED, false));aCheckBox.setSelectedIcon(new DiamondIcon(Color.PINK, true)); public JCheckBox(Icon icon, boolean selected)JCheckBox aCheckBox = new JCheckBox(new DiamondIcon(Color.RED, false), true);aCheckBox.setSelectedIcon(new DiamondIcon(Color.PINK, true)); public JCheckBox(String text)JCheckBox aCheckBox = new JCheckBox("Spinach"); public JCheckBox(String text, boolean selected)JCheckBox aCheckBox = new JCheckBox("Onions", true); public JCheckBox(String text, Icon icon)JCheckBox aCheckBox = new JCheckBox("Garlic", new DiamondIcon(Color.RED, false));aCheckBox.setSelectedIcon(new DiamondIcon(Color.PINK, true)); public JCheckBox(String text, Icon icon, boolean selected)JCheckBox aCheckBox = new JCheckBox("Anchovies", new DiamondIcon(Color.RED,  false), true);aCheckBox.setSelectedIcon(new DiamondIcon(Color.PINK, true)); public JCheckBox(Action action)Action action = ...;JCheckBox aCheckBox = new JCheckBox(action);

Each constructor allows us to customize zero or up to three attributes: Tag, icon, or initial selected status. Unless otherwise specified, no labels are displayed by default, and the default/unselected icons of the check box are unselected.

If we initialize the icon in the constructor, the icon is used for the checkbox unselected status, and the same icon is used when the checkbox is selected. We must either initialize the selected Icon through the setSelectedIcon (Icon newValue) method, or ensure that the Icon is status-related and update itself. If the selected icon is not configured or the status icon is not used, the same icon will appear in the selected or unselected status. Generally, icons that do not change the visual appearance between the selected and unselected statuses are not required by JCheckBox.

5.4.2 JCheckBox attributes

After JCheckBox is created, we can modify its attributes. The two attributes of JCheckBox overwrite the JToggleButton action of its parent class. The third borderPaintedFlat attribute is introduced in JDK 1.3. Other attributes are inherited by the parent class JToggleButton.

JCheckBox attributes

Attribute name
Data Type

Accessibility

AccessibleContext
AccessibleContext

Read-Only

BorderPaintedFlat
Boolean

Read/write binding

UIClassID
String

Read-Only

The borderPaintedFlat attribute shows the view of the border of the check icon as two dimensions rather than three dimensions. By default, the borderPaintedFlat attribute is false, meaning that the border will be three-dimensional. Figure 5-6 shows the flat border. The first, third, and fifth borders are flat, while the second and fourth borders are not. You can choose to ignore these attributes. However, this attribute is very useful for component pasters, such as tables and trees, because they only display the status and do not show whether the component can be selected. Windows and Motif use this attribute, whereas Metal (and Ocean) does not.

As shown in the constructor list, if we select the constructor setting icon, the constructor only sets one icon for the unselected status. If you want the check box icon to display the actual correct status, you must use a status awareness icon or use setSelectedIcon () to associate a different icon with the selected status. There are two different visual states that most users want the JCheckBox to apply. Unless we have special reasons, it is best to follow the design conventions of the common user interface.

Figure 5-7 shows the fourth button at the bottom of the interface to demonstrate the usage of JCheckBox. The check box always displays the selected status. The status of selected Pizza, Calzone, Anchovies, and Crust is displayed.

List 5-3 shows three available methods for creating a JCheckBox component with different icons, one of which uses the status awareness icon. The last check box shows the usage of bad icons.

Package net. ariel. ch05;
 
Import java. awt. Color;
Import java. awt. Component;
Import java. awt. EventQueue;
Import java. awt. Graphics;
Import java. awt. GridLayout;
Import java. awt. Image;
 
Import javax. swing. AbstractButton;
Import javax. swing. ButtonModel;
Import javax. swing. Icon;
Import javax. swing. ImageIcon;
Import javax. swing. JCheckBox;
Import javax. swing. JFrame;
 
Import net. ariel. ch04.DiamondIcon;
 
Public class IconCheck

Related Article

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.