Example usage for javax.swing.text JTextComponent setText

List of usage examples for javax.swing.text JTextComponent setText

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent setText.

Prototype

@BeanProperty(bound = false, description = "the text of this component")
public void setText(String t) 

Source Link

Document

Sets the text of this TextComponent to the specified text.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textcomp = new JTextPane();
    textcomp.setText("Initial Text");
    textcomp.getDocument().addDocumentListener(new DocumentListener() {
        public void insertUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
            try {
                String str = evt.getDocument().getText(off, len);
                System.out.println(str);
            } catch (BadLocationException e) {
            }//from   w  ww. j a va2s .com
        }

        public void removeUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
        }

        public void changedUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
        }
    });
}

From source file:Main.java

public static void fillWith(String value, JTextComponent... fields) {
    for (JTextComponent field : fields) {
        field.setText(value);
    }//from  w w w .  j  a  v a  2 s.c o m
}

From source file:Main.java

public static void clear(JTextComponent... fields) {
    for (JTextComponent field : fields) {
        field.setText("");
    }/*from   www  .j a  va 2 s . c om*/
}

From source file:Main.java

/**
 * Pastes via the fake clipboard./* w ww  .j  a  v a  2 s  .  co  m*/
 */

static void fakePaste(JTextComponent tc) {
    String text = tc.getText();
    int caretPos = tc.getCaretPosition();

    tc.setText(text.substring(0, caretPos) + clipboard + text.substring(caretPos));
    tc.setCaretPosition(caretPos + clipboard.length());
}

From source file:Main.java

public static void appendText(JTextComponent jcomponent, String str) {
    StringBuilder stringBuilder = new StringBuilder(jcomponent.getText()).append("\n").append(str);
    jcomponent.setText(stringBuilder.toString());
}

From source file:Main.java

public static void refreshJTextComponent(JTextComponent textComponent) {
    // borrowed from metaphase editor
    int pos = textComponent.getCaretPosition();
    textComponent.setText(textComponent.getText());
    textComponent.validate();/*from w  ww.  j  av a2 s .  co  m*/
    try {
        textComponent.setCaretPosition(pos);
    } catch (IllegalArgumentException e) {
        // swallow the exception
        // seems like a bug in the JTextPane component
        // only happens occasionally when pasting text at the end of a document
        System.err.println(e.getMessage());
    }
}

From source file:Main.java

public static KeyListener maxLength(JTextComponent textComponent, int length) {
    return new KeyAdapter() {
        @Override/*from   w  w  w .  ja va 2 s  .  c o m*/
        public void keyTyped(KeyEvent e) {
            if (textComponent.getText().length() >= length) {
                e.consume();
                textComponent.setText(textComponent.getText().substring(0, length));
            }
        }
    };
}

From source file:Main.java

/**
 * Cuts via the fake clipboard./*w  w w  .  j  av a 2s.co  m*/
 */

static void fakeCut(JTextComponent tc) {
    String selection = tc.getSelectedText();
    if ((selection != null) && (selection.length() > 0)) {
        clipboard = selection;

        String text = tc.getText();
        int selectionStart = tc.getSelectionStart();
        int selectionEnd = tc.getSelectionEnd();

        tc.setText(text.substring(0, selectionStart) + text.substring(selectionEnd));
        tc.setCaretPosition(selectionStart);
    }
}

From source file:Main.java

/**
 * sets text of textComp without moving its caret.
 *
 * @param textComp  text component whose text needs to be set
 * @param text      text to be set. null will be treated as empty string
 *//* ww w  .jav a  2 s. c  o m*/
public static void setText(JTextComponent textComp, String text) {
    if (text == null)
        text = "";

    if (textComp.getCaret() instanceof DefaultCaret) {
        DefaultCaret caret = (DefaultCaret) textComp.getCaret();
        int updatePolicy = caret.getUpdatePolicy();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        try {
            textComp.setText(text);
        } finally {
            caret.setUpdatePolicy(updatePolicy);
        }
    } else {
        int mark = textComp.getCaret().getMark();
        int dot = textComp.getCaretPosition();
        try {
            textComp.setText(text);
        } finally {
            int len = textComp.getDocument().getLength();
            if (mark > len)
                mark = len;
            if (dot > len)
                dot = len;
            textComp.setCaretPosition(mark);
            if (dot != mark)
                textComp.moveCaretPosition(dot);
        }
    }
}

From source file:DnDDemo2.java

public boolean importData(JComponent component, Transferable transferable) {
    String colorMimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color";
    JTextComponent textComponent = (JTextComponent) component;
    try {//from www . j  av a  2  s . c om
        DataFlavor colorFlavor = new DataFlavor(colorMimeType);
        Color color = (Color) transferable.getTransferData(colorFlavor);
        String text = (String) transferable.getTransferData(DataFlavor.stringFlavor);
        textComponent.setForeground(color);
        textComponent.setText(text);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}