Adding a text component

Text components are available for simple text and for enhanced text and editing panes. VisualAge provides text beans from the com.sun.java.swing, com.sun.java.swing.text, and java.awt package.

You can add a text bean to enable text input or provide a label.


Bean Description
JLabel (Swing) or Label (AWT) A label, usually to identify another component
JPasswordField (Swing) A text field for sensitive data
JTextArea (Swing) or TextArea (AWT) A multiline text area
JTextField (Swing) or TextField (AWT) A single-line text field

Adding a label graphic

You can add a graphic to a JLabel bean. Select a graphic file for the icon property in the label's property sheet. Use the horizontalTextPosition property to specify a LEFT, CENTER, or RIGHT position of text relative to the graphic. The default choice is RIGHT. Set the iconTextGap property to specify the space between the icon and label text.

Defining keyboard access to an input field

For Swing text input components, you can define keyboard shortcuts to place the focus in the input field or area.

Aligning text

Select an alignment choice for the horizontalAlignment or alignment property in the text component's property sheet. The alignment options are LEFT, CENTER, and RIGHT.

Selecting initial text

To select the initial text, set the selectionStart and selectionEnd properties in the text component's property sheet. These values are offsets from the beginning of the text, which is at offset 0. To select all text without determining the initial text length, specify selection from offset 0 to an offset that you consider to be larger than the initial text length.

Defining a minimum size for layout managers

Some layout managers use a minimum size for placement of components. To specify a minimum width for a text area, enter the width, in characters, for the columns property in the text component's property sheet. To specify a minimum height for a text area, enter the number of rows for the rows property.

Hiding input text

To hide input text, either use a JPasswordField bean or, from the text component's property sheet, specify an echo character for the echoChar property.

Preventing text modification in a text area

To prevent any input in a text area, set the editable property to False in the text component's property sheet.

Making a text area scrollable

An AWT TextArea implements scrolling, duplicating the capability of a ScrollPane. A JTextArea does not implement scrolling itself, but can become scrollable by placing it in a JScrollPane and using its scrolling capability.

Defining tool tip text for a text component

For Swing components, you can specify tool tip text, also known as fly-over text or hover help. Enter text for the toolTipText property in the component's property sheet.

Defining initial availability

By default, the component is enabled for user interaction. To initially disable the component, set the enabled property to False in the component's property sheet.

Synchronizing text

If you need to synchronize user input between two text components, do the following:

  1. Connect their text properties.
  2. Open the connection properties.
  3. Associate each end of the connection with the keyReleased(java.awt.event.KeyEvent) event.

If you need to synchronize text that you set by connection from another source, make connections to both text components.

Disabling a text component

To disable a text component when an event occurs, connect the event to the component's enabled property and set the connection parameter value to False.

Enabling a text component

To enable a text component when an event occurs, connect the event to the component's enabled property and set the connection parameter value to True.

For examples, see the LayoutManagers class in the com.ibm.ivj.examples.vc.swing.layoutmanagers and com.ibm.ivj.examples.vc.layoutmanagers packages and the Amortization class in the com.ibm.ivj.examples.vc.swing.mortgageamortizer and com.ibm.ivj.examples.vc.mortgageamortizer packages. These examples are shipped in the IBM Java Examples project.


Related procedures
Visual bean basics
Using VisualAge beans in visual composition
Adding the IBM Java Examples project

Related references
Text beans
LayoutManagers sample
MortgageAmortizor sample