350 likes | 442 Views
Chapter 7. Making Selections with Check Boxes and Option Buttons. Objectives. Allow the user to enter yes/no responses using check boxes. Combine check boxes in a group to act as option buttons. Test integer conditions with a switch statement. Incorporate Swing components into an applet.
E N D
Chapter 7 Making Selections with Check Boxes and Option Buttons
Objectives • Allow the user to enter yes/no responses using check boxes. • Combine check boxes in a group to act as option buttons. • Test integer conditions with a switch statement. • Incorporate Swing components into an applet.
Check Boxes and Option Buttons • Checkboxes and option buttons are based on the Checkbox class. • When you place check boxes in a group, the checkboxes turn into option buttons. • The checkboxes and option buttons give the user only the choice of yes/no or true/false or on/off. • If there are multiple checkboxes, each checkbox operates independently of other. So the user can choose, none or one or any number he wants.
Checkbox() Checkbox(String label) Checkbox (String label, CheckboxGroup groupName, boolean state) Checkbox(String label, boolean state, CheckboxGroup groupName) The Checkbox Component - Constructors
The Checkbox Component - Example Checkbox chkBold = new Checkbox(“Bold”); Checkbox chkItalic = new Checkbox(“Red”, null, true);
Creating Checkboxes • The default state (on or off) of a checkbox is off. • The groupName is only used for option buttons. • When you want to create checkboxes (no group name), set the groupName argument to null and set the initial state.
The CheckboxGroup - Constructor CheckboxGroup()
The CheckboxGroup - Example CheckboxGroup cbgColor = CheckboxGroup();
Creating Option Buttons • Option Buttons Example • //Declare the components • CheckboxGroup cbgColor = new CheckboxGroup(); • Checkbox optRed = new Checkbox(“Red”,cbgColor,false); • Checkbox optBlue = new Checkbox(“Blue”,cbgColor,false); • Checkbox optGreen = new Checkbox(“Green”,cbgColor,false); • Checkbox optBlack = new Checkbox(“Black”,cbgColor,false); • //add to the init • add(optRed) • add(optBlue); • add(optGreen); • add(optBlack); • In the init you add the components but not the group.
Checking the State of Check Boxes and Option Buttons • You can check if the option button is selected or checkbox is checked using the getState method. • For example chkItalic.getState() will return a Boolean value true if the checkbox is checked or false if the checkbox is unchecked.
Testing Multiple Checkboxes • If you have multiple checkboxes they all operate independent of each other. • Therefore, you will use separate if statements to check the state of each checkbox. • For option buttons they are dependent on each other. • Therefore, you will use if else if statements to check the state of each option button.
Check Boxes Option Buttons Check Boxes and Option Buttons
ItemListener • You can an ItemListener to each checkbox or option button that you want to check the change. • Every time the state of a component is changed an event is triggered. • Like ActionListener, you must implement the ItemListener interface and add an itemListener to one or more components. • Then write the code for the itemStateChanged method. • When the user clicks in a checkbox component, the code in the itemStateChanged method executed immediately.
ItemListener Continued • If you add item listeners to several components then you can use the getSource method to determine which component triggered the event. • You also need to determine if the object that triggered the event was selected or deselected. • You usually need to check the source of the event as well as its state true or false. • For example: • //Is the source the Red option button and is it selected? • if (eventSource == optRed && optRed.getState()) • { • setBackground(Color.red); • showStatus("Color: Red"); • }
The switch Statement • Whenever you want to test a single variable or expression for multiple values, the switch statement provides a flexible and powerful solution. • It is easier and cleared to read than nested if statements. • Java switch statement works only with int, char, shor, or byte expressions. • You can check for an integer or a single character. • The expression can be (intCount – 1). The body of the switch statement must be in braces.
The Switch Statement – General Format switch(expression) { case ConstantValue: statements; [case Constant value: statement(s);] … [default: statement(s);] }
The switch Statement - Example switch(intChoice) { case 1: HandleChoice1(); break; case 2: HandleChoice2(); break; case 3: HandleChoice3(); break; default: showStatus(“Choice must be 1, 2, or 3”); }
The Switch Statement • The default statement is optional and if you omit the default and none of the cases match then the executions passes through the switch statement without executing anything. • When the variable meets the condition of the case, it executes the body of that case but it does not jump out of the switch statement. • It goes and executes the next body and the next till the end of the switch statement (all the statements following are executed). • The break statement forces it out of the current block to the closing brace of the switch statement.
Swing Components • These newer components are less dependent on the platform and have more capabilities than the AWT classes. • Swing components are referred to lightweight components and AWT components are referred to heavyweight components. • Swing components build graphical components from the library classes rather than the underlying operating system. • They have three distinct “look and feel” options available. • You can select the Motif look, the Metal look or the Windows look.
Swing Components Continued • The default look is called the Basic “look and feel.” • The capabilities are buttons have keyboard shortcuts (often called hot keys or accelerator keys). • You can add icons to buttons and labels and choose the alignment of labels on check boxes, radio buttons. • Swing components are based on the Container class from the java.awt. package. Therefore, you still have to import the AWT package.
Multiple Panes • A big advantage of Swing components over AWT involves the layering of panels. • With AWT one component is hidden over another component or container. • With Swing you can achieve the visual effect of placing an item on top of another through use of multiple panes. • The basic pane is the Content Pane. Over that you can have the Layered Pane and/or the Glass Pane.
Setting the Content Pane • The Content Pane must be a container. • Way to set up an applet is to declare a panel, add components to the panel, and set the panel as the Content Pane. • Use the setContentPane method to set the panel as the Content Pane. • See Example: • //Declare Swing components • JPanel pnlPane = new JPanel(); //Panel for Content Pane • JLabel lblHello = new JLabel("Hello World") • //In the init method: • //Add component to the panel • pnlPane.add(lblHello); • //Make the panel the Content Pane • setContentPane(pnlPane);
Setting the Content Pane Continued • An alternative is to skip creating the panel and add the components directly to the Content Pane. • You can use the getContentPane method which returns the object that is the ContentPane. • For example : getContentPane().add(lblHello);
Using Swing Components • To use swing components, you must import both java.awt.* and javax.swing.*
Running Swing Applet in a Browser • Most browsers cannot run swing components. • Internet Explorer and Netscape Navigator can run swing components if they have a plug-in installed. • The plug-in has to installed only once on a computer, and then applet can run in either browser assuming the paths are set correctly. • The plug-in cause the browser to use the Java Runtime Environment in JDK folder rather than the browser JVM. • You will find the plug-in in the RunSwingApplet.htm.
Using Enhanced Properties of Swing Components • Some of the reasons for using Swing components rather than AWT components are: • Swing has a more complete set of components. • Swing components have more functionality. • Swing components require fewer system resources. • Swing components more nearly have the look and feel of the destination system. • With most Swing components, you can add icons, ToolTips, keyboard shortcuts, and control the look of the borders.
ToolTips • To add a Tooltip use the setToolTipText method. • Example : chkBold.setToolTipText("Change the Font Style label bold");
Keyboard Shortcuts • Keyboard Shortcuts are also called access keys or hot keys and allow the user to select an option of the keyboard or the mouse. • Use the setMnemonic method to set the single letter used for keyboard access. • Example: btnOK.setMnemonic('o'); • In this example, the O of OK on the button's label appears underlined. The user can select the button by pressing Alt + O (uppercase or lowercase).
Borders • To add a border to your component, you must import the border package. • import javax.swing.border.*;. • You can choose from among several types of borders. • Here are examples of some of the possibilities: • pnlChoices.setBorder(new BevelBorder(BevelBorder.RAISED)); • btnOK.setBorder(new EtchedBorder()); • btnOK.setBorder(new LineBorder(Color.black, 3));
Radio Buttons • One of the extra Swing components is a radio button, also called an option button. • Instead of using a check box in AWT, you can use the Swing JRadioButton. • You can create groups for radio buttons using the ButtonGroup class. • Examples: • ButtonGroup grpColor = new ButtonGroup(); • JRadioButton optRed = new JRadioButton ("Red"); • JRadioButton optBlue = new JRadioButton ("Blue"); • JRadioButton optGreen = new JRadioButton ("Green"); • JRadioButton optGray = new JRadioButton ("Gray");
Initializing Radio Buttons • When you sue radio button, you should initialize their stat (true or false) in the init method. • If you fail to initialize the buttons, they will appear deselected initially. • Java will generate a warning message.