AT&T Home | AT&T Labs | Research
AT&T Labs, Inc. - Research

The Yoix® Scripting Language

Home | What's New | Grammar | Documentation | Download | License | YChart | YDAT | YWAIT | Byzgraf | FAQs
replaceText (Object comp, int offset, int count, String str [, int adjust [, Pointer undo]]) yoix.swing
 
Updates the AWT or Swing component comp by replacing count character at offset in the text currently displayed by that component with str and returns the net number of characters that were added to the displayed text. If the component maintains a text insertion point (e.g., the caret when comp is a JTextArea, JTextField, TextArea or TextField) then replaceText also tries to position it at the end of newly updated text.

The optional adjust argument should be 1 when you want replaceText to try to position the caret (if there is one) at end of the newly updated text and 0 otherwise. The optional undo argument should be a pointer to an Array with room for at least three elements that replaceText uses to return information (two integers and a string) that can be used to undo the operation. The first element in the array is the offset where the operation took place, the second element is the number of new characters that were added at that offset, and the last element is a String that is the text that was deleted by the operation. The order of the elements in the undo array make it particularly easy to use with unroll and replaceText.

replaceText works with any AWT or Swing component that defines a field named text.
 
 Example:   The following example shows a frame containing a textarea and a button. Pressing the button replaces several character at the caret position in the displayed text.
import yoix.*.*;

JFrame f = {
    Dimension size = NULL;

    Array layout = {
        new JTextArea {
            String tag = "$_textarea";
            String text = "Spam, spam, spam, baked beans, sausage and spam";
            int    textwrap = TRUE;
            int    rows = 8;
            int    columns = 50;
            int    scroll = VERTICAL;
        }, CENTER,

        new JButton {
            String text = "Replace Spam";
            Array  undo[3];

            actionPerformed(ActionEvent e) {
                Object comp = root.components.$_textarea;

                replaceText(comp, comp.caret, 2, "XXX", TRUE, &undo);
                comp.requestfocus = TRUE;
                printf("undo=%O\n", undo);
            }
        }, SOUTH,
    };
};

f.visible = TRUE;
The line that requests the focus for the textarea is only included to make it easy to see the caret position.
 
 Return:   int
 
 See Also:   JTextArea, JTextField, JTextPane, TextArea, TextField, TextTerm, appendText, deleteText, insertText, unroll

 

Yoix is a registered trademark of AT&T Inc.