How to create a Check Box Button in Java

A check box is a two-state button that can be clicked on or off. In Figure 8.1, the component labeled "Trade Customer" is an example of a check box button. To create a check box button, first declare the check box button object to be of type JCheckBox. For example,

JCheckBox tradeButton;



Next invoke the javax.swing.JCheckBox constructor, as follows:

tradeButton = new JCheckBox("Trade Customer", false);

This creates a check box button labeled "Trade Customer" with the initial state set to off. We could have used an alternative constructor JCheckBox ("Trade Customer") because this sets the initial state to off by default. There is also the constructor JCheckBox(), which creates an unlabeled check box button with the initial state set to off.

No comments:

Post a Comment