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 packages. Although Swing and AWT beans can be mixed, it is inadvisable.

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

  1. Add one of the following text beans:


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

  2. Define initial characteristics of the text component.

    Defining label text or initial input text
    Enter text for the text property in the text component's property sheet. For a multiline text area, use \n to denote a new line.

    Adding a label graphic
    For a JLabel bean, you can add a graphic to the label. Select a graphic file for the icon property in the label's property sheet. Use the horizontalTextPosition property to specify the position of text relative to the graphic. The position choices are LEFT, CENTER, and RIGHT. 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.

    • To define an accelerator for a text field, specify the accelerator character, enclosed in single quotes, for the focusAccelerator property in the text component's property sheet. If you define tool tip text for the text field, the accelerator is displayed with the tool tip text. For example, if you specify 'a' as the focusAccelerator value, alt+A appears after tool tip text when the user moves the mouse pointer over the fext field.

    • To define a label mnemonic for a text field, specify the mnemonic character, enclosed in single quotes, for the displayedMnemonic property in the JLabel's property sheet.

    Aligning text
    Select an alignment choice for the horizontalAlignment or alignment property in the text component's property sheet. The alignment choices 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 specify an echo character for the echoChar property in the text component's property sheet.

    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 uses the scrolling capability of a JScrollPane in which it is placed. If you want a JTextArea to be scrollable, drop it in a JScrollPane.

    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 initially enabled for user interaction. To initially disable the component, set the enabled property to False in the component's property sheet.

  3. Provide runtime logic for the text component.

    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
Working with Beans Visually
Using VisualAge Beans in Visual Composition
Adding the IBM Java Examples project

Related references
Text Beans
LayoutManagers Sample
MortgageAmortizor Sample