Register for EarthWeb's Million Dollar Sweepstakes!
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


The methods of most interest include the getCheckedIndices() method. This method returns an integer array that contains the zero-based indices of all items with selected check boxes. The getItemChecked() method returns a zero-based index of the item whose check box state you want to examine. The setItemChecked() method takes two arguments. The first argument is the index of the item whose check box state you want to set, and the second argument is the value that indicates the current state of the check box. The CheckedListBox control has most of the same properties that the ComboBox and the ListBox controls have. The one I use most often is the selectedIndexChanged() event.

I’ve written a program that combines a combo box, a list box, and a checked list box into one form. Listing 8.2 shows the event handlers for this program.

Listing 8.2 This Program Shows the Event Handlers That Use a Combo Box, a List Box, and a Checked List Box Control

1   private void listBox1_selectedIndexChanged(Object source, Event e)
2   {
3       label1.setText( "The selected item is " +
        ⇒listBox1.getSelectedItem() );
4   }
6   private void checkedListBox1_selectedIndexChanged(Object source,
    ⇒Event e)
7   {
8       label1.setText( "The selected item is " +
        ⇒checkedListBox1.getSelectedItem() );
9   }
10
11  private void comboBox1_selectedIndexChanged(Object source, Event e)
12  {
13      label1.setText( "The selected item is " + comboBox1.getText() );
14  }

When the program runs, you’ll see the three selection controls and a label at the top of the form that shows what selection you just made. The program is shown running in Figure 8.3.


Figure 8.3  This program combines a combo box, list box, and checked list box all on one form.

ListView and TreeView Controls

The icon for the ListView control is shown here:

There are several properties you’ll want to pay close attention to here. One property is the alignment property. This determines how items are aligned within the ListView control. The fullRowSelect property determines whether all subitems are highlighted within the item when they’re selected. As with some of the other controls that have an items property, the items property for the ListView control lets you add the items you want to appear in your ListView control.

When you add a ListView control to your form, four lines of source code are added to set the default ListView control properties. These are shown in the following example:

listView1.setLocation(new Point(32, 16));
listView1.setSize(new Point(208, 112));
listView1.setTabIndex(0);
listView1.setText("listView1");

You’ll find various methods in the ListView class to fulfill your programming needs. The getSelectedItems() method returns an array of currently selected list items. The insertItem() method takes four arguments. The first argument is an index for a new item you want to insert. The second argument is a string containing the text for the new item. The third argument is an integer that will be an index into the image list for this item. The fourth and final argument is an integer that will be a handle to external data to be stored by this item.

The ListView control has most of the same events that other controls have. There are, though, two events that are different for this control. The itemActivate event occurs when an item is activated. The itemDrag event occurs when the user begins dragging an item.

The TreeView control is encapsulated in a TreeView class. This control displays a generic Windows TreeView control. Here’s the icon that appears in the Toolbox for the TreeView control:

The TreeView control has various important properties that are different. The first and most interesting is the dock property. The dock property is in the Properties window. When you click the small button, it brings up a window displaying the docking options. These docking options are displayed graphically, as shown in Figure 8.4.


Figure 8.4  The dock property graphically shows the options available to you.

Another property you’ll find important is the hotTracking property. This determines whether nodes give feedback when the mouse is moved over them. The showLines property lets you set whether lines will be displayed between sibling nodes, and between parent and child nodes. The showPlusMinus property lets you determine whether plus/minus buttons are shown next to parent nodes. The showRootLines property lets you set whether lines are displayed between root nodes.

When a TreeView control is added to your form, four lines of default source code are added to your program. They are shown in the following example:

treeView1.setLocation(new Point(48, 168));
treeView1.setSize(new Point(120, 72));
treeView1.setTabIndex(1);
treeView1.setText("treeView1");

Now let’s look at some of the more important methods in the TreeView control. The getSelectedNode() method gets the currently selected node in the TreeView control. The insertNode() method inserts a node into the TreeView control. It inserts it as a root node at the specified position. There are various very important events for a TreeView control. The first one is the afterExpand event. This happens when a node has been expanded. The afterLabelEdit event is triggered when the text of a node has been edited by the user. The afterSelect event happens when a selection has been changed. The beforeCollapse event occurs when a node is about to be collapsed. The beforeExpand event takes place when a node is just about to be expanded.

HScrollbar and VScrollbar Controls

In this section, I’m going to talk about these two controls together. The HScrollbar control is encapsulated in an HScrollbar class, and the VScrollbar control is encapsulated in a VScrollbar class. Both of these controls can be placed on a form. They allow users to scroll an item. The icons for the HScrollbar control (on the left) and the VScrollbar control (on the right) are shown here:


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.