|
J Tutorial
Introduction to WFC Programming
Windows Foundation Classes for Java (WFC) is a set of class libraries
that works seamlessly with the Rapid Application Development (RAD) tool
Visual J++. The combination of this class framework and RAD tool make it
easy to build fast and powerful applications and components for the
Microsoft platform using the Java language. WFC brings the Java language
to the Win32 platform and the Dynamic HTML (DHTML) object model, which is
a W3C standard supported by Internet Explorer 4.0. This means you can use
Java to solve real problems today and know that you have a smooth
migration path into the future.
A goal of WFC is to provide complete and compelling component and
programming models for Java. Because Java provides a unique set of
advantages in terms of language features, it is the language of choice for
developing Windows and DHTML applications using this new programming
model.
WFC enables developers to quickly build rich Win32 applications and
ActiveX controls in Java by using the Visual J++ Forms Designer to drag
and drop controls onto a form, set properties, and generate event
handlers. Developers can also easily access data on a server from their
applications and deploy their applications as Windows .EXE files or
Internet URLs. Moreover, they can build their own WFC components by using
the designer or writing code directly in the editor. Finally, developers
can access the DHTML object model using Java classes.
The rich application model takes advantage of the J/Direct
technology to access the Win32 API. However, it makes it much easier to
program by taking care of details like Window message-handling procedures,
message pumps, messages, window handles, and so forth. The application
model is open, so experienced Win32 programmers can intermix J/Direct
calls with WFC classes to add any functionality available on the Win32
platform.
J/Direct Call Builder
Using the J/Direct Call Builder, you can quickly create J/Direct
calls to the Win32 API. The J/Direct Call Builder automatically inserts
Java definitions for the Win32 API elements into your code, along with the
appropriate @dll.import tags.
To access the Win32 API with the J/Direct Call Builder
- To open the J/Direct Call Builder, point to Other Windows on
the View menu and click J/Direct Call Builder.
- By default, the J/Direct Call Builder displays elements defined in
the following Win32 DLLs: advapi32.dll, gdi32.dll, kernel32.dll,
shell32.dll, spoolss.dll, user32.dll, and winmm.dll. (These DLLs are
specified by the WIN32.TXT file, which is selected under Source.)
- The Target box identifies the class to contain the J/Direct
calls. By default, a class named Win32 will be added to your project
and will contain these calls. (If your solution contains multiple Java
projects, the Win32 class is added to the first project.)
To specify a different target class, click the ellipses button (...).
- In the Select Class dialog box, select whether you want to
view your classes in Project View or Class View.
Project view displays a hierarchical list of the .java files in your
solution, where each file node lists the names of the classes
contained in that file. Class view displays a hierarchical list of
class names.
- Once you have chosen a view, select the name of the class to
contain the J/Direct calls. (To copy the calls to the Windows
Clipboard instead of to a class, select Clipboard.)
- Click OK.
- To filter the display of Win32 methods, structs, and constants,
select or clear the Methods, Structs, and Constants
options.
- Now select the method, struct, or constant that you want to insert.
(You can select multiple items using the SHIFT and CTRL keys.) Note
that a Win32 struct will be added as a nested class inside your class.
Note To search for an item by its initial characters, type the
characters into the Find box. The first item that matches these
characters is automatically selected.
- To insert the associated Java definition into your class, click Copy
To Target. (You can also double-click the method, struct, or
constant to insert it into your class.)
Creating a Windows Application with WFC
Using WFC, you can create Windows applications with
the Java programming language. You can also use the RAD features of the
Forms Designer to quickly drop controls onto your form, configure their
properties, and add event handlers.
Note Before you use the following procedure to create a Windows
application, close any projects that you may already have open. (On the File
menu, click Close All.)
To create a Windows application with WFC
- On the File menu, click New Project.
- On the New tab, expand the Visual J++ Projects folder
and click Applications. Then select the Windows Application
icon.
- In the Name box, enter a name for your project.
- In the Location box, enter the path where you want to save
your project, or click Browse to navigate to the folder.
- Click Open. A collapsed view of your project appears in Project
Explorer.
- In Project Explorer, expand the project node. A file with the
default name of Form1.java has been added to your project.
Note Renaming this file in Project Explorer does not rename the
associated class in the source code, and vice versa. You must manually
change all instances of the old name. (Note that you can create an empty
project and then add a form with the Form template. This two-step
process allows you to name the form before it is created.)
When you use the Windows Application template, your Java class extends
com.ms.wfc.ui.Form. To view the source code in the Text editor,
right-click Form1.java in Project Explorer and click View Code
on the shortcut menu. Program execution begins with the main method.
The code in the initForm method represents the modifications
you make in the Forms Designer.
Modifying the Form in the Forms Designer
The following procedures show how to add controls to your form in the
Forms Designer. This example populates a list box whenever a button is
clicked.
To add controls to the form
- To open your form in the Forms Designer, double-click Form1.java
in Project Explorer.
- In the Toolbox, select the WFC Controls tab. (If the Toolbox
is not displayed, click Toolbox on the View menu.)
- To add a list box to your form, click the ListBox control in
the Toolbox and then click the form.
- To add a button to your form, click the Button control in the
Toolbox and then click the form.
To set the property of a control
- Select the Button control on the form to show its properties in the
Properties window. (If the Properties window is not displayed, click Properties
Window on the View menu.)
- Find the button's text property and change the value to Add
Item.
To add an event handler
- You can also use the Properties window to add handlers to the events
of controls. Click the Events toolbar button in the Properties
window to display the events for the Button control.
- Find the click event and enter addItemClick for the
name of the method that will handle the event. When you press ENTER,
the Text editor opens to an empty event handler named
addItemClick.
- Inside the definition of the
addItemClick event
handler, add the following line of code:
listBox1.addItem("New string.");
Building and Running the Application
When you use the Windows Application template, an .exe file named ProjectName.exe
is automatically created when you build your project.
To build and run your application
- On the Build menu, click Build. Any compilation errors
or messages appear in the Task List. (Double-clicking
an error in the Task List moves the insertion point in the Text editor
to the error.) Correct the errors and rebuild your application.
- To run the application from the development environment, click Start
on the Debug menu. To run the application from the command
line, simply run the .exe file.
Click the Add Item button. A line of text appears in the list
box.
- To close the application, click the Windows Close button located in
the upper-right corner of the form.
Creating a Form
You can add a form to your application through the Project menu.
Objects that include components and controls can be dragged from the
Toolbox to the form. The form is persisted as a block of source code that
defines an instance of the WFC Form class. The default Form class
contains:
- A constructor that invokes the initForm method.
- A set of instance variables, one for each item on the form.
- An initForm method that specifies the default properties for the
items. You can change these defaults by setting item properties.
The Forms Designer generates this code based on values set in the
Properties window for each control and the form itself. Be careful when
modifying the initForm method because the Forms Designer also reads this
code when displaying the form. Also avoid conditionals, such as if,
while, and switch statements, and component methods. If you
modify the initForm code, restrict changes to properties that begin with set,
and restrict values to constants. Do not use variables to set values.
For example:
Correct setSize(10,100);
Incorrect x=10; y = 100; setSize(x,y);
To create a form
- Open an exiting project, or create a new project.
- In Project Explorer, select the name of your project.
- On the Project menu, click Add Form.
- In the Add Item dialog box, click either the New or Existing
tab. For this example, click the New tab.
- In the Add Item dialog box, click the Form icon.
This template creates a class capable of hosting controls.
- In the Name box, edit the name of the class file. The default
name is Form1.java.
- Click Open.
The form window is displayed, showing the form surface.
The form is automatically opened in design mode if you are creating a
new form from a template.
If the Forms Designer is not currently active, use the following
procedure to open it.
To view the form in the Forms Designer
- On the View menu, click Designer.
-or-
- Double-click the node in the Project Explorer or
right-click the form file name and then click View Designer on
the context menu. Your form now appears in the Forms Designer.
By default, controls are aligned to the form grid. Set grid options
using the Tools command on the Options menu. Form properties can be
directly modified in the property page for the form.
The Toolbox contains WFC Controls and can include other tabs containing
ActiveX and custom controls.
Once you have the basic form template, you are ready to add controls to
it.
Adding Controls to a Form
You can add controls to your form in design mode by selecting them from
the Toolbox.
To add a control
- In the Toolbox, click the control you want to add to your form and
drag it onto your form. The control appears on your form in its
default size.
-or-
- In the Toolbox, double-click the control to add it to the center of
your form. The control appears on your form in its default size. You
can then drag it to the desired location.
Note When a control is placed onto a form at design time, only the
class name for the control is added. Code for the control is not generated
until you close the form. If you modify an existing control in the
Toolbox, you must close and reopen the form or reload the project so that
the form picks up the updated class files for the control.
To resize a control
- Select the control you want to resize, and resize it with the sizing
handles.
Note You can also use SHIFT+ arrow keys to size the control.
To add event handler templates to a control
- Double-click a form or control to generate a method for the default
event of the form or control. The event handler method is created in
the Form class.
Note You can also add event handlers through the
events view in the Properties window.
Adding an Event Handler
You can use the Forms Designer to create event handlers. An
event handler is a routine in your code that determines which actions to
perform when an event occurs, such as the user's clicking a button.
For example, the event handler for the mouseDown event provides a
mouseEvent object that allows you to determine which mouse button was
pressed, where the mouse was positioned on the form, and which keys on the
keyboard were pressed during the click.
You can add an event handler using the events view in the Properties
window. A handler for the default event can also be added by
double-clicking. Moreover, you can assign an existing handler to the
events of other controls if the signatures of the events are the same.
To add an event handler using the events view of the Properties window
- Click the control (such as a button control) or form you want an event
handler created for.
- In the Properties window, click the events view button (indicated by a
thunderbolt symbol).
- In the list of available events, click an event (for example, click).
- In the box to the right of the event name, type the name of the
handler (for example, MyButton_click), and press ENTER.
A code template is generated in your code similar to the following:
private void MyButton_click(Object sender, Event e) {
}
In this example, sender is the source of the event and e
is an Event object that provides information about the event.
To add a handler for the default event
- Double-click a form or control to generate a handler method for the
default event of the form or control.
-or-
- In the events view in the Properties window, double-click the event
to create the handler for the default event.
To use to the same handler for events shared by multiple controls
- In the events view in the Properties window, click an event for the
first control.
- In the box to the right of the event name, type the name of the
handler and press ENTER.
- On the form, select the second control.
- In the Properties window, click the same event for the second
control.
- Click the drop-down arrow to view a list of existing handlers for
this type of event.
- Select the name of the event handler. Only the existing handler is
used. No new templates are created in the code.
Setting Properties with the Properties Window
Properties are attributes that define how a control appears and runs at
run time. Each form and each control has an associated custom property
page. Modifying the properties in the page (the Properties window) affects
the code that is generated for the control in the initForm method.
Properties include the appearance, behavior, and position for the control.
The background color, enable and disable state, show state, font, default
size and position, and so forth, can be set in the Properties window for
the form.
To edit the properties of a control or form
- On the form, click the control you want to edit; or to edit the
form, click the form itself.
The property page associated with the selected control is displayed
in the Properties window.
- In the Properties window, select the property for the control that
you want to modify. For example, select the font property to
change the font style for the name on a button control.
Note If you select multiple controls, a custom property page
appears that displays only the common properties.
Complex properties use common dialog boxes and editors for:
- Text input – size and strings (in-place string edit changes appear
in real-time)
- Combo box selection – cursors, enumerators and Booleans
- Font picker – common dialog box for selecting font characteristics
- Color picker – common dialog box with a palette for color
selection
- List items – window for creating initial list items
- Anchor and Dock editors – for setting position behavior
Click the node of compound properties to access subproperties.
Adding and Modifying a Menu
If you want your application to provide a set of commands to users,
menus offer a convenient and consistent way to group commands and an easy
way for users to access them. The menu bar appears immediately below the
title bar on the form and contains one or more menu titles. When you click
a menu title (such as File), a menu containing a list of menu items
appears. Menu items can include commands (such as New and Exit), separator
bars, and submenu titles. Each menu item the user sees corresponds to a
menu control property you define. Multiple menus can be added to one form.
To add a menu to your form
- Under WFC Controls in the Toolbox, double-click the MainMenu
control.
Note You can also select the MainMenu control and drag it
onto your form.
The MainMenu control is inserted onto your form, and the default
location for the first item on the menu bar is displayed. Two visual
markers (Type Here) appear directly to the right and below to
signify the next available editing location.
Note By default, the MainMenu property for the form is set
to MainMenu1.
- Type the name of your first menu caption in the default location
text box. (For example, type File to add the File menu.)
The menu caption is added to your menu, and the default location
text box moves to the next menu caption location. Continue typing your
menu captions until all your captions are complete.
Forms can have several menu items, such as File/Save and File/SaveAs,
that change depending on the state of the application. Menus can be
enabled or disabled, checked or unchecked, made visible or hidden using
properties for each menu item.
If you want to change the main menu used by a form, you change the menu
property for the form to a different menu name, for example, mainMenu2.
You can add access keys to your menu. Access keys provide a way
for the user to access the menu using only the keyboard. An access key
corresponds to an underlined letter on the menu.
To add access keys to a menu
- Click the menu caption in which you want to add an access key.
- Place the cursor immediately before the letter in the caption box
that you want to be your access key letter and type an ampersand
(&). For example, on the File menu, click File,
place your cursor before the "F" in File, and then
type an ampersand. You can also edit the caption title through the Text
property for the menu item.
Shortcut keys add accessibility to your menus. A shortcut key is a
keyboard combination, such as CTRL+C for copy, that invokes a particular
command.
To add a shortcut key to a menu item
- Click to the immediate right of the menu item caption to which you
want to add a shortcut key. A dropdown list appears.
- Select a key combination from the dropdown list.
-or-
In the Properties window, click the shortcut property and
select a key combination.
To remove a shortcut key
- Select a menu item.
- Set the shortcut property to none.
At times, you may want to give users various commands that they can
apply to the application. For example, there may be several available
toolbars in an application. The user can select which toolbar will be
displayed by clicking the toolbar name on the menu, and a check mark
appears next to the toolbar name.
To enable check marks on a menu
- Select a menu item.
- Click to the immediate left of the menu item name. A checkbox apears.
-or-
In the Properties window, click the checked property.
To move a menu
- Select the desired menu item caption.
- Drag the menu caption to the new position.
The menu caption moves and snaps to place. Other menu captions that
are affected by this change are moved as appropriate.
Note Moving top-level menu captions moves all submenus as well.
Submenus are not displayed during the move.
To modify a menu caption
- Click the menu caption you want to modify.
- In the text box containing the menu caption, make any necessary
changes.
Separator bars are used in menus to add a distinct line between groups
of commands.
To create a menu separator bar
- Click the menu caption that is above the location where you want to
add a separator bar.
- Press the HYPHEN (–) key and then press ENTER.
A separator bar appears directly below your menu caption.
-or-
Click the right mouse button, and then click Insert Separator
on the context menu.
Separator bars can be moved and deleted in the same manner as menu
captions.
To delete a menu item from your form
- Right-click the menu item caption you want to delete, and click Delete
on the context menu.
Note To delete the entire menu, select each menu caption and press
DELETE, or click the MainMenu control you want to delete and press DELETE.
Confirmation on deletion occurs only on menu captions with submenus.
To edit the name associated with a menu item
- Click the right mouse button over a menu item.
- Click Edit Names to toggle the Edit Names mode for the menu.
- Click on and edit the name associated with the menu item you wish to
modify (for example, menuItem1).
Context Menus
Context menus can be associated with controls on your form. These menus
appear when the user right-clicks the control.
To add a context menu to a form
- Under the WFC Controls in the Toolbox, double-click the ContextMenu
control.
Note Use any of the methods to add a control to a form. You can
also select the ContextMenu control in the Toolbox and drag the
control onto your form.
- To enter design mode, double-click the ContextMenu control.
To assign a context menu to a control
- Click the control you want to assign a context menu to.
- In the Properties window, add the name of the context menu to the contextMenu
property. By default, the first context menu is named contextMenu1.
Context menu controls can be edited in the same way as MainMenu
controls.
Adding Tool Tips to Controls on a Form (Visual J++)
ToolTips are text strings that appear as you "hover" over a
control with the mouse pointer. In Visual J++, a separate WFC control
enables ToolTip functionality for controls on the form. The time, delay,
and other properties can be set using the property page for each control
on the form.
To add ToolTips to controls
- In the WFC Toolbox, add a ToolTip control to your form.
- Edit the property for each control to add the desired text.
Visual J++ synchronizes visual representation of code changes and form
views (code generation from visual layout) in both directions. Information
about which methods (and classes) that are being modified is formatted
into the source code. Similarly, modifications to the source code are sent
to the Forms Designer.
The two-way editing does not simultaneously modify source code and
visual layout. The code that is used to create the visual layout is
disabled while the Forms Designer is in use. The layout is stored as code
when the Forms Designer window is closed or the form is saved. Editing of
the entire initForm function is locked until the Forms Designer window is
closed.
Edits in the Forms editor are not serialized as code until you click
Save on the File menu, or when the Forms editor is closed.
You can use the Forms Designer to edit classes that adhere to the
following syntax:
class isEditable extends [UserControl, Form, Design Page] {
Container components = new Container(this);
private void initForm() {
[form initialization code goes here]
}
}
The initialization code must adhere to certain parsing rules:
- Initialization code must reside in the initForm function.
- No conditional code will be parsed (no if, while, or switch
statements).
Finding Errors with Dynamic Syntax Checking
Visual J++ also provides dynamic syntax checking to assist you when
you're writing code within the Text editor. In addition to the information
Statement Completion provides, you receive visual clues
in the form of red squiggly lines and error tips as you build your
program's statements.
As you begin typing within a .java file, red squiggly lines appear
under code elements, such as class names, member names, and symbols. When
you see a red squiggly line, IntelliSense is telling you your code, as it
is currently written, has syntactic errors. As you continue typing, the
red squiggly lines may disappear as you finish the statement — depending
upon the correctness of the completed statement.
For each syntax error marked with a red squiggly line, a task related
to the syntax error will appear in the Task List. This
provides a list of items that you'll need to correct before you compile
your program.
To get error tips and error help from a red squiggly line
- Rest the cursor over the red squiggly line.
IntelliSense displays an Error Tip that best suits the context of
your source code. On occasion, the tip will simply be "Syntax
Error."
Tip If you do not see a red squiggly line under obvious syntax
errors, make sure you've enabled the options that support dynamic syntax
checking.
- While resting the cursor over a
Enabling/Disabling Dynamic Syntax Checking Options in the Text Editor
Within the development environment, you can enable or disable the
editor's dynamic syntax checking options.
To enable dynamic syntax checking options
- On the Tools menu, click Options to display the Options
dialog box.
- From the tree view in the leftmost pane of the Options dialog
box, expand the Text Editor node by clicking the plus ("+")
sign next to it.
- Select Java Tasks to display the property page for Tasks and Error
display.
- In the Tasks group of this property page, click the check box
for Check syntax as you type.
- In the Error Display group of the property page, click the
check box for Underline syntax errors as you type.
Visual J++ enables these dynamic syntax checking options by default.
Note Clearing the check box for Check syntax as you type
or Underline syntax errors as you type disables the respective
option.
Enabling/Disabling Statement Completion Options in the Text Editor
Within the development environment, you can enable or disable
Statement Completion's Auto list members and Parameter
information options.
To enable Statement Completion options
- On the Tools menu, click Options to display the Options
dialog box.
- From the tree view in the leftmost pane of the Options dialog
box, expand the Text Editor and then the Per Language
nodes by clicking the plus ("+") sign next to these
nodes.
- Select Java to display the Text Editor/Per Language/Java
property page.
- In the Statement Completion group of this property page,
click the check boxes for Auto list members and Parameter
information to enable these options.
Visual J++ enables Statement Completion's Auto list members
and Parameter information by default.
Note Clearing the check box for Auto list members or Parameter
information disables the respective option.
Creating Statements with Word Completion
For the examples in this topic, Word Completion provides visual clues
when you're typing the name of String and System
classes, and creating a new String object. Word Completion is available
for any Visual J++ project.
Note The code examples in the following scenario were created
with a Visual J++ Console Application project.
To select an item from a list provided by the Word Completion feature
- On the View menu, click Project Explorer")
to open Project Explorer.
- In Project Explorer click the plus ("+") sign to
the left of your project's name to expand your project.
- Highlight the .java file containing your project's
main()
method (Class1.java by default) and select View Code from the
shortcut menu.
Visual J++ opens the Text editor and loads your .java file. The file
is now ready for editing.
- To get Word Completion assistance for the
String class,
type an "S" between the braces of the application's main()
method and leave the cursor to the right of this letter.
Tip Notice the red squiggly line drawn under the letter. Since an
"S" has no significance within the context of this program,
IntelliSense gives a visual clue for the syntax error.
- On the Edit menu, click Complete Word to display a
list box of classes and other elements recognized by IntelliSense.
Tip You can use the keyboard shortcut, CTRL+SPACE, instead of
selecting the Complete Word option on the Edit menu, to
display this list box.
- With the list box still displayed, type a "T" after the
"S" of the statement you're building in your .java file.
Statement Completion now selects StackOverflowError in its
list — the first element that starts with an "ST" in the
context of this program.
- Now type an "R" after the "ST" in your statement
and Statement Completion moves the highlight bar to String in
the list box.
- Press the Tab bar to insert "
String"
into the statement you're building.
Pressing the Tab bar or any other non-alphanumeric key, such
as a period or opening parenthesis, places the selected item into a
statement at the location of the insertion point.
Tip Instead of typing the letters until Statement Completion
finds the element you need, you can use the up and down arrows to
highlight your selection.
To select a class name after the new operator
- Continue to build the statement by typing the following code after
"
String":
[String] myStr = new
- Type a space after "
new" and Statement
Completion automatically displays a list of class names available
within the scope of your program.
- Type "STR", or enough letters of "String" to
select the String item from the list.
- Press Tab and "
String" is inserted
after "new".
Your statement should look like this:
String myStr = new String
If you know how to partially spell the name of the class or item, use
the following procedure to speed up the process of typing and selecting
the correct spelling and capitalization.
To finish typing a word using the Word Completion feature
- In your .java file, move the cursor to a new line and type "SY".
- Press CTRL+SPACE (the Word Completion keyboard shortcut).
Statement Completion completes the word, "System",
and inserts it into your code since it is the only item that begins
with an "SY". If more than one item starts with these
letters, Statement Completion displays a list box.
Selecting an Overloaded Method
This example uses Statement Completion to help you select an overloaded
constructor method for the String class.
Note If you have completed the steps of Creating Statements
with Word Completion continue working through the following
procedure. If not, take a few minutes and complete the procedure before
selecting an overloaded constructor method for the String
object.
Before proceeding, make sure you have loaded a .java file into the Text
editor that contains the following code:
String myStr = new String
To select an overloaded method for a class
- With the insertion point immediately after the last "
String"
in the statement, type an opening parenthesis, "(".
A pop-up window appears displaying the signature of one of the
constructor methods for the String class. The spin control on the left
side of this window indicates how many overloaded constructor methods (11)
are available for the String class and which one is being displayed (1).
- Click anywhere on the spin control to display the signatures of the
other String constructor methods until you find one that takes a
String
argument.
In this example, constructor number 11 of 11 meets this criteria.
- After the opening parenthesis, type
"Hello World!");.
Your completed statement should look like this:
String myStr = new String("Hello World!");
Statement Completion also displays a class's public methods and
fields.
Building an Argument List with Parameter Info
This example uses Statement Completion's Parameter Info
feature to display information for a method's argument.
Note If you have completed the steps of Creating Statements with
Word Completion, Selecting an Overloaded Method, and Selecting
Methods from a Member List, continue with the following
procedure. If not, take a few minutes and work through those procedures
before selecting a method for the String class.
Before proceeding, make sure you have loaded a .java file into the
Text editor that contains the following code:
String myStr = new String("Hello World!");
if(myStr.equalsIgnoreCase
To select build an argument list with Parameter Info
- Continuing to build the
if statement, type an opening
parenthesis, "(", immediately after "myStr.equalsIgnoreCase".
Parameter Info displays the method's declaration with the single
parameter (String p1) in a bold-type font.
Note In this case, the method has only one parameter (String
p1). If you select a method that takes more than one argument,
IntelliSense displays argument types and positions within the method
declaration. When you type the opening parenthesis after the method's
name, IntelliSense bolds the first argument. As you add commas between
the arguments, IntelliSense bolds the type and position information for
the next argument that's needed to complete the method's call.
Tip If you do not get a pop-up window with parameter information
after you type the opening parenthesis, select Parameter Info
from the Edit menu or use the keyboard shortcut, CTRL+SHIFT+I. If
this continues to happen, make sure you've enabled the Statement
Completion feature.
- After the opening parenthesis, complete the statement by typing:
"hello world!"))
The completed if statement should look like the
following:
if(myStr.equalsIgnoreCase("hello world!"))
- Using the Statement Completion's Word Completion, Member
List, and Parameter Info options, complete this
example by adding the following code after the closing parenthesis of
the
if statement:
{
System.out.println("The strings are the same.");
}
else
{
System.out.println("The strings are different.");
}
return;
- Build the program.
- In the Text editor, place the cursor on "
return"
(the last statement of this program's code). Click the right mouse
button and select Run To Cursor from the shortcut menu.
- View the following results in JVIEW's console window:
The strings are the same.
Selecting Methods from a Member List
This example uses Statement Completion's Member list feature
to help you select a method of the String class. You may
also use this procedure to select class member variables from the Member
list.
Before proceeding, make sure you have loaded a .java file into the
Text editor that contains the following code:
String myStr = new String("Hello World!");
To select a class member from the Members List
- On a new line, type:
if(mystr.
- When you type the "." (dot operator) after
myStr,
Statement Completion displays a list of the methods belonging to the
String class.
Tip If you do not get a member list after you type the dot
operator, select List Members from the Edit menu or use the
keyboard shortcut, CTRL+J. If this continues to happen, make sure you've enabled
the Statement Completion feature.
- Type "equalsI" to highlight the
String
class's equalsIgnoreCase method in the Members List.
- Press Tab to insert the
equalsIgnoreCase method
after the dot operator for myStr.
Both lines of your code should look like the following:
String myStr = new String("Hello World!");
if(myStr.equalsIgnoreCase
Creating a Console Application
A console application has no graphical user interface. You can use the
nongraphical classes in either WFC or the Java
API to develop your application.
Note Before you use the following procedure to create a console
application, close any projects that you may already have open. (On the File
menu, click Close All.)
To create a console application
- On the File menu, click New Project.
- On the New tab, expand the Visual J++ Projects folder
and click Applications. Then select the Console Application
icon.
- In the Name box, enter a name for your project.
- In the Location box, enter the path where you want to save
your project, or click Browse to navigate to the folder.
- Click Open. A collapsed view of your project appears in Project
Explorer.
In Project Explorer, expand the project node. A file with the default
name of Class1.java has been added to your project.
Note Renaming this file does not rename the associated class in the
source code, and vice versa. You must manually change all instances of the
old name. (Note that you can create an empty project and then add a class
with the ClassMain template. This two-step process allows you to name the
class before it is created.)
Adding Code in the Text Editor
To view the source code that was generated, double-click Class1.java
in Project Explorer. Program execution begins with the main method.
To add code to the application
- In the Text Editor, add the following code after the
//TODO
comment inside the main method:
String str = "The quick brown fox jumped over the lazy yellow dog.";
System.out.println("The string is: " + str);
System.out.println("The length of the string is: " + str.length());
System.out.println("The substring from positions 10 to 20 is: " +
str.substring(10,20));
System.out.println("The uppercase string is: " +
str.toUpperCase());
Building and Running the Application
After you build your application, you can run it from either the
development environment or from the command line.
To build and run your application
- On the Build menu, click Build. Any compilation errors
or messages appear in the Task List. (Double-clicking
an error in the Task List moves the insertion point in the Text editor
to the error.) Correct the errors and rebuild your applet.
- To run the application from the development environment, click Start
on the Debug menu.
- To run the application from the command line, use JVIEW.
At the command prompt, type
jview Class1 from the
directory location of your project.
Task List Window
Helps you customize, categorize, and manage work associated with your
project by displaying your tasks, specially marked comments, named
shortcuts, warnings, errors detected while you type in the Text Editor
window, and errors detected while you are compiling your project. Some
errors, for example SmartEditor and Comment errors, are automatically
generated as you type or when you compile.
The Task List window is visible when you:
- Open an editor in the development environment the first time after
installation.
- Build a project if it has errors.
- Choose Task List from the Other Windows command on the View menu.
In the Task List window you can:
- Manually, add your own tasks into the "Click here to add a new
task" line at the top of the Task List.
- Track and manage errors in your code that are generated as you type
in the editor or compile.
- Track a comment, error, or named shortcut and jump to its location
in the editor.
- Sort Task List items by Priority, Category, Checked, Description,
File, or Line.
- Check or delete some Task List items.
The tasks and ways they are added to the Task List vary with the
specific development environment you are in.
Use F12 to cycle through the tasks currently displayed, one task at a
time. To cycle through the tasks in the reverse order, press SHIFT+F12.
Elements
Category
Specifies the categories a Task List item can be assigned. The
application you are using may have custom icons that differ from the
default development environment icons and appear next to the categories.
SmartEditor Errors
Continually updated as you introduce syntax errors. You only get
SmartEditor errors for files that are open. The task is deleted from Task
List when you fix the error. You cannot delete SmartEditor errors or edit
any field. These items are all high priority because the source code will
not compile successfully until they are resolved. SmartEditor errors
appear in your code with a red squiggly underline. Double-click a
SmartEditor error task and the file appears in an editor window with the
cursor at the site of the error.
Compiler Errors
Build errors and warnings, and deployment tasks that are automatically
added as you compile your project. Errors and deployment tasks have a high
priority, and warnings are listed with a normal priority. You can check
but not delete compiler error tasks. Double-click a Compiler Error task
and the file appears in an editor window with the cursor at the site of
the error.
Comments
Indicates where you have TODO, UNDONE, or HACK in the comments of your
code. You can add new comment tokens, edit their names, and change their
priority. Any new comment tokens must contain only characters that are
legal in a comment token. They are alphanumeric characters, $,
_, and (and) where the commas and parentheses are part of the
sentence, not part of the code. You cannot delete or edit the name of the
TODO token, but you do not have to use it. Comment tasks are initially
normal priority but you can change the priority for any tokens. When you
delete a comment in your code, the task is deleted from the Task List.
However, you cannot edit or delete a comment in your code by changing it
in the Task List. Double-click a Comments task and the file appears in an
editor window with the cursor at the site of the comment.
Named Shortcuts
Lists named shortcuts that you created in your text editor. The name of
the shortcut in the Task List is the line of text containing the insertion
point when you created the shortcut. The description is the name of the
shortcut. You can edit the shortcut name without changing the text in your
code. You can change the priority and checked state. You can remove a
named shortcut from the task list by putting the cursor in the appropriate
line of code in the Text Editor and choosing the Toggle Shortcut command
from the Text Editor shortcut menu.
User-defined Tasks
Tasks that you enter into the Description field at the top of the Task
List window. You can set the priority and checked state of User-defined
tasks. Stored on a per-user and per-solution basis.
Checked
Specifies whether or not you believe the Task List item has been
completed. For example, some tasks such as compiler and build tasks can be
checked automatically when completed. Some tasks such as Comments and
SmartEditor errors cannot be checked.
Description
Describes the item. The description can be generated automatically or
entered and edited manually.
Click here to add a new task
Allows you to add User-defined tasks and set their priority. Type the
name of the task in the description column, set the priority if you want,
and press ENTER. The new task appears at the top of the Task List.
File
If applicable, specifies the name of the file to which the item is
attached.
Line
If applicable, specifies the line number in the file where the item is
located.
Per Language Options (Text Editor – Options Dialog
Box)
Allows you to set the details for your text editor regardless of which
application you are using.
Choosing the Per Language option sets these options for all of the
editors in the development environment. To set the Per Language options
for a specific editor, choose the editor in the list below the Per
Language options and select the options. Changes to the specific editor
are effective only until you change the options again from the Per
Language entry. For example, if you want to have all of the URLs open the
browser and go to the Internet site in all of your editors in the
development environment, chose Per Language in the Text Editor options of
the Options dialog box, then select Enable single-click URL navigation. If
you want to Enable virtual space for only your HTML editors without
enabling it for your other editors, choose HTML under Per Language in the
Text Editor options of the Options dialog box, then click Enable Virtual
Space.
Note Application-specific options do not appear in the Options
dialog box until the application is loaded into memory. Therefore, you may
not see the same options you saw during your last session. Once you create
a project or use a command that uses a particular application, the options
appear in the Options dialog box and remain available as long as the
application remains in memory. Common options are available all of the
time.
CAUTION Clicking Cancel cancels all of the changes made in the
dialog box. If you made changes to more than one category, none of the
changes will be saved.
Options
Statement Completion
Auto List Members
Displays a box that contains information that would logically complete
the statement at the current insertion point while you are typing.
Parameter Information
Displays information about functions and their parameters while you are
typing.
Settings
Stream Selection
Selects text from the beginning of the first line to the end of the
last line, and all intervening lines.
Box Selection
Selects a box-shaped block of text defined by the first and last
characters selected, and includes all of the characters that fall within
the box shape.
Enable Virtual Space
Allows a selection to extend past the end of a line either vertically
or horizontally.
Display
Enable single-click URL navigation
When selected, displays URLs with underlines, changes the mouse cursor
as it moves over URL text, and opens the browser to the valid Internet
site when you click the mouse while the cursor is over URL text. When the
check box is cleared, URLs are not underlined, there is no change to the
mouse cursor when you move it over URL text, and you cannot open the
Internet site from the editor. You can open the Internet site only by
right-clicking the URL text and choosing the Open URL command on the
shortcut menu.
Setting Options for the Editor
You can change many of the default settings for the Text Editor to
conform to your preferences. Some settings are available on a per-language
basis, allowing you to configure the Text Editor differently for each
development language that you use. Other settings are global and apply to
all instances of the Text Editor regardless of the language engine.
Options can be set or changed in the Options dialog box.
To open the Options dialog box
- Choose Options from the Tools menu.
Options for the Text Editor are listed in the treeview pane of the
Options dialog box, under the Text Editor node. As you select a node in
the treeview pane, the corresponding options are shown in the right-hand
pane of the Options dialog box.
To set global options
- Select a first-level node beneath the Text Editor node. If
any second-level nodes appear beneath the node you have selected,
options set on the first-level node override any corresponding
per-language settings on the second-level node.
To set per-language options
- Select a second-level node beneath the Text Editor node. Any
settings made at this level supercede corresponding first-level
settings for the selected language only.
Note If you subsequently change an option setting on a
first-level node, your corresponding per-language settings (where
applicable) will be overridden
Java Format (Text Editor - Options Dialog Box)
Provides options for Java source code formatting.
With the Java Format section of the Text editor options you can:
- Determine if indenting will occur in the editor.
- Specify the format style for code braces.
- Specify how various code elements will be indented and aligned.
Options
Indenting
A group of options for determining the type of indentation support for
the Text editor. The following is a list of the options available for
indentation:
| Indentation mode |
Description |
| None |
The Text editor does not
automatically add indentation while you are entering code. Text goes
to the beginning of the line when a return is entered. |
| Block |
The text editor adds
indentation but keeps the code on the same indentation level until
you change it. |
| Smart |
This is the default setting.
The Text editor adds indentation and uses the context of the
indentation to determine the level automatically. |
Separate Smart Indent Actions In Undo List
When this option is selected, Undo commands (accessed by clicking the
Undo menu item from the Edit menu or by pressing CTRL+Z on the keyboard)
separates the automatic indentation caused by Smart indenting from other
keystrokes. This allows you to undo Smart indentation actions without
undoing the command that caused the indentation to occur. If the Smart
indentation option is not selected, this option is disabled.
Indent Braces With Contained Code
When this option is selected, a brace is indented to be in-line with
code that follows the brace. This option is not selected by default.
Format Open Brace
When this option is selected, open brace entries are formatted for you.
A list is provided next to this option to allow you to select the style of
open brace formatting. The following contains a list of the options for
open brace formatting:
| Open brace format |
Description |
| On separate line |
When this format is selected,
an open brace is automatically positioned on a separate line from
the method or code block that the open brace belongs to. |
| At end of line |
When this format is selected,
an open brace is automatically positioned on the same line as the
method or code block that the open brace belongs to. |
This option is not selected by default.
Indent Case Labels
When this option is selected, case labels are indented within the
case's switch block. This option is not selected by default.
Align Operands In Multi-Line Expressions
When this option is selected, the operands in a multi-line expression
are aligned one below the other. For example:
int total += var1 + var2
+ var3
+ var4;
When not selected, continuation lines are indented by one additional
tab stop. This option is selected by default.
Align Parameters
When this option is selected, the parameters in a multi-line method
declaration or call are aligned with the first parameter in each line of
the declaration or call. For example:
public native void method1 (String arg1, String arg2
String arg3,
String arg4);
When not selected, continuation lines are indented by one additional
tab stop. This option is selected by default.
Top
|