Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


ComboBox Control

ComboBox is a list box combined with a Static Text control or an Edit control. The list-box portion of the control can be displayed at all times, or it can drop down when the user clicks the drop-down arrow. Shown here is the ComboBox icon as it appears in the Toolbox:

In the Properties window, you should take note of several things. The first one is the items property. This allows you to add text strings to the selection list of a combo box. All you have to do is click the button with the ellipsis, and it brings up a string list editor dialog box that allows you to enter text selections for your combo box. The next property you’ll probably want to note is the sorted property. This determines whether the combo box is sorted alphabetically. The style property controls the appearance and functionality of a combo box. The choices are simple: drop-down and drop-down list.

As with all other controls, when you put a ComboBox control on your form, some default source code that initializes the combo box is added. The following example shows a combo box that I added to my program:

comboBox1.setLocation(new Point(32, 48));
comboBox1.setSize(new Point(168, 21));
comboBox1.setTabIndex(1);
comboBox1.setText("");

Some of the methods you’ll find important are the addItem() method, which lets you add an item to the combo box; the findString() method, which finds the first item in the combo box that starts with a specified string; the getSelectedIndex() method, which retrieves the zero-based index of the currently selected item in the combo box; and the getSelectedText() method, which retrieves the text for the currently selected item. These methods will be the ones you use most frequently. With your combo box selected on the form, you can find the events available for a combo box by clicking the lightning-bolt button in the Properties window. Of these events, the one I use most often is the selectedIndexChanged() event. If you double-click on it, it generates a method in your program that you can use. The following source code shows a method I created for my program that gets whatever text has been selected in a combo box and sets the label text of the Form window to reflect the selected text:

private void comboBox1_ selectedIndexChanged(Object source, Event e)
{
   label1.setText("The selected item is " + comboBox1.getSelectedItem() );
}

ListBox Control

The ListBox control is encapsulated in a ListBox class. This displays a list from which the user can select one or more items. The ListBox control icon as it appears in the Toolbox window is shown here:

The most important properties you’ll be interested in with the ListBox control start with the items property. The items property is just like the items property in a combo box. When you click the small button with the ellipsis, it brings up the string box editor dialog box. You can use this dialog box, then, to add any selection strings to your list box that you want. The multiColumn property might also be important to you. This determines whether values should be displayed in columns horizontally. Also, as with the combo box, the sorted property is one you might find important too.

When you add the list box to your form, five default methods are added to your source code. These default methods set up the control so that it has basic functionality. These five methods are shown in the next example:

listBox1.setLocation(new Point(32, 96));
listBox1.setSize(new Point(168, 108));
listBox1.setTabIndex(2);
listBox1.setText("listBox1");
listBox1.setUseTabStops(true);

Some of the methods you found useful in the ComboBox control, you’ll find useful in the ListBox control as well. The addItem() method is important. This adds an item to the list box. The findString() method operates the same way as the findString() method of the combo box. It finds the first item in the list box that starts with a specified string. The getSelectedIndex() method returns the zero-based index of the currently selected item in a single selection list box. The getSelectedIndices() method contains an integer array containing the zero-based indices of all currently selected items. If you call this method in a single selection list box, it returns an array containing a single element. If there are no selected items in the list box, it returns an empty array. The setSelected() method selects or clears a selection for the specified item in a multiple selection list box. The ListBox control has many events you’ll find useful. The one I use most frequently is the selectedIndexChanged() method. This is the event that’s fired whenever the selectedIndex property for this control changes. The following example shows a method I added to my program that takes the item that was selected in a list box and displays it in a label on my form:

private void listBox1_selectedIndexChanged(Object source, Event e)
{
   label1.setText( "The selected item is " + listBox1.getSelectedItem() );
}

CheckedListBox Control

The CheckedListBox control is encapsulated in the CheckedListBox class. These controls display a list box containing items accompanied by check boxes that can be selected or cleared. The icon for the CheckedListBox control is shown here:

The CheckedListBox control offers two properties you’ll want to note. The first one is the CheckOnClick property. This determines whether the check box should be toggled on the first click of an item. The second property to take note of is the threeDCheckBoxes() property. This property indicates whether the checked values should be shown as flat or 3D check marks.

When you add a check box to your form, six lines of source code are generated to set the default properties of your checked list box. They are shown in the following example:

checkedListBox1.setLocation(new Point(216, 104));
checkedListBox1.setSize(new Point(72, 68));
checkedListBox1.setTabIndex(3);
checkedListBox1.setText("checkedListBox1");
checkedListBox1.setIntegralHeight(true);
checkedListBox1.setItems( new Object[]{} );


Previous Table of Contents Next


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.