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


Week 2
At a Glance

  Application Controls
  Dialogs
  Fonts and Texts
  Drawing
  Images
  Image Processing
  Animation

Day 8
Application Controls

There’s a big difference between adding controls to applets and adding controls to applications. When you add controls to applets, you have to manually type the code, figure out the size, and adjust the properties of the controls. In applications, there’s a toolbox that already contains all the controls. You simply drag these controls onto your form, and the code for the control is automatically added to your program. You can also edit the properties of these application controls very easily with the Properties window.

Today, we’ll cover these topics:

  Label, Edit, and RichEdit controls
  Button controls, including CheckBox and RadioButton controls
  ComboBox, ListBox, and CheckedListBox controls
  ListView and TreeView controls

We’ll talk about each of these controls, and then we’ll group them and create some sample programs to demonstrate how to use them.

Label, Edit, and RichEdit Controls

These three controls are grouped together because they all deal with text presentation to users and text input from users.

Label Control

Label controls are encapsulated by the Label class. These controls display a string of text that the user cannot edit. You can use a label to display status information about your program. You can also use a Label control to identify a control that doesn’t itself have a built-in label. When a Label control gets the focus, it passes that focus on to the next control in the tab order. Therefore, a Label control never actually gets and keeps the focus. Here’s the icon from the toolbox that represents a Label control.

To use a Label control, simply click on it in the Toolbox window and drag it over to your form. When it’s on your form, you can resize it and move it around. When a Label control is on the form and it’s selected, you’ll notice that the Properties window shows all the properties for that label. At least half of these properties are stock properties that pretty much come with any control. An example of two stock properties that most controls have is backColor and foreColor. The one property you’ll end up changing most is the font property. If you click in the Edit field of the font property, you’ll see a small button with an ellipsis come up. If you click this, it brings up the file selector dialog box, which allows you to set the font for this control. The textAlignment property is also very important. To change the text that’s in the label, you edit the text property. One of the most interesting properties I’ve found is the cursor property. This allows you to set the cursor type that the mouse cursor will change to when the mouse enters the area that the label occupies.

When you first change the label, go back to the source code and look for the declaration of your label. It’s in the gray area that you can’t edit, but you can view it. The declaration for your label should be right above the initForm() method. The next line of source code shows the declaration of your first label when you add it:

Label label1 = new Label();

Other source code must also be added for this label. A label with default properties has five methods added: the setLocation(), setSize(), setTabIndex(), setTabStop(), and setText() methods. However, these won’t be added to your source code until you close the Form window and reopen it. That’s the way it works! So if you add a label (or any other control, for that matter) to your form and you don’t see source code before the control, just close the window and reopen it. Then watch your source code magically appear. The following example shows the source code that was added after I closed my form and opened it again:

label1.setLocation(new Point(32, 32));
label1.setSize(new Point(176, 48));
label1.setTabIndex(0);
label1.setTabStop(false);
label1.setText("label1");

The Label class has methods that match the label properties you can see in the Properties window. For instance, there’s a getAutoSize() method, which gets the Boolean value indicating whether the size of the control automatically adjusts and the fonts change. That matches the autoSize property that you’ll see in the property window when a label is selected. So somewhere in the Label class you can see labels that match all the properties. If, however, you don’t see a method in the Label class that matches the property, you might have to check the control superclass that has methods that the Label and other controls inherit.

Many events are available to Label controls. In the Properties window, click on the icon that looks like a lightning bolt.

When you click the Events button in the Properties window, you see a new list. All the properties available to that control are shown. To add a handler for the event you want, simply double-click on the event name, and the event is added to your source code.

Let’s try a simple experiment. Add a mouseEnter() event and a mouseLeave() event. You’ll immediately see the source code for both of them in your form source code. In the mouseEnter() event handler method, we’ll set the foreground color of the label to aqua. In the mouseLeave() event handler method, we’ll set it back to black, which is the default color. The following source code shows both examples:

private void label1_mouseEnter(Object source, Event e)
{
    label1.setForeColor( Color.AQUA );
}

private void label1_mouseLeave(Object source, Event e)
{
    label1.setForeColor( Color.BLACK );
}

Go ahead and compile and run the program. As you move the mouse into the label area, the text changes to an aqua color. As you move the mouse outside the label area, the text changes to black.


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.