|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
The checkedProperty property lets you determine whether the initial state is checked or unchecked. Another property you might find useful is the threeState property. This controls whether the check box supports the indeterminate state. As usual, when you add a control to your form, some default code is added to make your check box minimally functional. Heres the source code that is added: checkBox1.setLocation(new Point(40, 32)); checkBox1.setSize(new Point(120, 32)); checkBox1.setTabIndex(0); checkBox1.setText("First Checkbox"); The two methods youll probably use most will be the getChecked() method, which retrieves a Boolean value that indicates whether the check box is selected, and the setChecked() method, which sets a Boolean value that indicates whether the check box is selected. The event youll probably use more than any other event is the checkState Changed() method. Ive created a program that has a check box that handles this event. In the event, Ive simply set the text of a label in my form so that I know that the check box state has been changed. The source code for my checkStateChanged() method follows: private void checkBox1_checkStateChanged(Object source, Event e) { label1.setText( "The state of checkBox1 was changed." ); } RadioButton ControlA RadioButton control is encapsulated in a RadioButton class. This control is a small circle that has the given text displayed next to it, typically to its right. The control highlights the circle and sends a message to its parent window when the user selects the button. The control removes the highlights and sends a message when the button is next selected. The RadioButton icon that appears in the Toolbox is shown here: The two properties youll want to pay special attention to are the checked property, which lets you set the initial state of the button either on or off, and the textAlign property, which lets you determine whether the text is to the left or right of the button. Four lines of code are added each time you put a radio button on your form. The following example shows the default code that was added to my program when I added a single radio button directly to the form: radioButton1.setLocation(new Point(40, 48)); radioButton1.setSize(new Point(80, 32)); radioButton1.setTabIndex(1); radioButton1.setText("radioButton1"); The Button class has two methods you will use most frequently. The first is the getChecked() method. This retrieves a Boolean value that indicates whether the button is checked. The second, the setChecked() method, sets the button to either true or false. A true value makes the button checked; false makes it unchecked. The event you will most likely rely on will be the check changed event. The check changed event is triggered whenever the checked property changes value. If youre adding radio buttons directly to the form, all of these radio buttons will be part of the same group, so their parent will be the Form window. So if you add 10 radio buttons to the form, or 20, or 30, or 40, only one of those radio buttons will be selectable at any given time. GroupBox ControlThe problem with adding radio buttons to the Form window is that every radio button you add to the Form window is part of the same group. You need a way to separate radio buttons into different groups. The way youll do this is by using a GroupBox class. The GroupBox control is encapsulated into the GroupBox class. This is a rectangle that contains other controls, and in this case, well be using them to contain radio buttons. A group box has a border around it and displays the caption for the group in the upper-left corner of the group box. Besides visually setting a group of controls apart from another group of controls, a group box affects the behavior of the controls contained within. For instance, when we added radio buttons to the form, they were all in the same group. You could click only one at a time. But if you put a group box on the form and then add radio buttons within that group box, any radio buttons that were already on the form will not be affected by those radio buttons that are inside of the group box. The radio buttons inside of the group box become their own group. Only one of them can be selected at a time. Their behavior is completely independent from radio buttons that are either on the form or in other group boxes. Heres the GroupBox icon as it appears in the Toolbox: Probably the only property in the GroupBox control youll be interested in is the text property. This allows you to change the label text that appears in the upper-left corner of the group box. When you add a GroupBox control to your form, five lines of default code are added. These lines of code are called the setLocation(), setSize(), setTabIndex(), setTabStop(), and setText() methods. They are shown in the following example: groupBox1.setLocation(new Point(24, 128)); groupBox1.setSize(new Point(176, 72)); groupBox1.setTabIndex(3); groupBox1.setTabStop(false); groupBox1.setText("First Group"); The only method youll probably end up using with group boxes is the setText() method. Even this you probably wont use very often, because it will be rare for you to change the title text in a group box during program execution. Although several events are available to group boxes, it will be a rare occasion when you use them. ComboBox, ListBox, and CheckedListBoxy ControlsThese three controls are grouped together because they offer a list of choices to users. The three are similar in function, and in many cases, your choice will just depend on your personal preference or the situation in which you find yourself.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |