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


Edit Control

The Edit control is encapsulated into an Edit class. This control is a rectangular control into which users can enter text. Here’s the icon for the Edit control you’ll find in the Toolbox:

As with the Label control, the Properties window lists many stock properties that are common to most controls. Still, some properties are noteworthy and are ones you’ll use fairly often.

The first of these properties is the acceptsReturn property. You’ll use this for multiline Edit controls. This indicates whether return characters are accepted as input. The next property you’ll find interesting is the acceptsTab property. This is also for multiline Edit controls. It indicates whether tab characters are accepted as input. Another property that might make your life easier is the characterCasing property. When this is set to true, all characters that are typed are entered as either all uppercase or all lowercase, depending on where you have the control set. One property that might intrigue you (although you might not use it very often) is the passwordChar property. When this is set to true, the characters displayed are asterisks, as all password fields normally are. The last property I want you to take note of is the word wrap property. This is for multiline Edit controls. It indicates whether lines are automatically word-wrapped.

When you first add an Edit control to your form and bring your form back up, you have added four lines of code that set some minimal properties. The methods that are called are the setLocation(), setSize(), setTabIndex(), and setText() methods. You can see the source code that was added by default:

edit1.setLocation(new Point(32, 120));
edit1.setSize(new Point(176, 20));
edit1.setTabIndex(1);
edit1.setText("edit1");

The Edit control has many of its own methods, and most of these methods correspond to the actual properties in the Property window. The Edit class inherits many methods from the Control class. The two methods that are most commonly used are the getText() method and the setText() method. These are methods in the Control class. The following example shows how to get the text from an Edit control:

String strText;
strText = edit1.getText();

With your Edit control selected in the Form window, click the Events button in the Properties window. You’ll see a whole list of the events that are available for the Edit control.

Now, let’s create a program. The first thing we’ll do is add a textChanged() event handler in the control. Click on the textChanged() event. Then go to that method and edit it so that it looks like the following example:

private void edit1_textChanged(Object source, Event e)
{
    label1.setText( edit1.getText() );
}

When the program runs, every time you press a key or the edit text changes, the label above the Edit control also changes. This program is shown in Figure 8.1.


Figure 8.1  The label text changes every time the edit text changes.

RichEdit Control

The RichEdit control is encapsulated in a RichEdit class. The RichEdit control is a control that contains formatted text. It supports font selection, boldface, and other type attributes.

This control makes it a snap to create text editors and simple word processors. I remember the days in the mid-1980s when it was a big deal just to get word wrap to work in a DOS edit box. Now, the RichEdit control makes it a no-brainer to do word wrap with different fonts and text attributes, and it all works seamlessly in your application. Here is the RichEdit icon that appears in the Toolbox window:

Quite a few properties are associated with the RichEdit control. It has some of the same properties that are important to the Edit control, such as the acceptsReturn property and the acceptsTab property. Yet it adds a few additional ones that are specific to the RichEdit control. Some of the more interesting ones are the rightMargin property, which defines the right margin of the Edit control, and the selProtected property, which turns the contents of the currently selected text on and off.

Because the RichEdit control is so much more complicated than the Label and Edit controls, it needs to set more properties from the start to function properly. A total of six lines of source code are added to the program by default when you add a RichEdit control to your program. The following source code shows the default lines of code that have been added:

richEdit1.setFont(Font.DEFAULT_GUI);
richEdit1.setForeColor(Color.WINDOWTEXT);
richEdit1.setLocation(new Point(16, 16));
richEdit1.setSize(new Point(264, 192));
richEdit1.setTabIndex(0);
richEdit1.setText("richEdit1");

There are a few methods of note. These methods are ones you might find very useful and turn to often. The first one is the find() method. This searches the text in the RichEdit control for a given string. Another important method is the getCellText() method. This returns the currently selected text of a RichEdit control. If no characters are selected, it returns a string of zero length. The loadFile() method loads a file in RichText format into a RichEdit control.

Button, CheckBox, RadioButton, and GroupBox Controls

These four controls are grouped together because they are similar in nature. Buttons allow users to make quick selections, check boxes allow users to set items on and off, and radio buttons allow users to set items to one of several preset values. Group boxes allow you to group radio buttons into families.

Button Control

A Button control is encapsulated into a Button class. These are labeled buttons. The application can cause some connection to happen when the button is pressed. Here’s the icon for a Button control:

Most of the properties available to buttons are either stock properties or ones that are easy to figure out, but one of great interest is the dialogResult property. This tells you what result clicking the button will produce. The choices you have are OK, cancel, abort, retry, ignore, yes, and no. Those results will all be for dialog boxes, if the button happens to be placed in a dialog box.


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.