Example usage for javax.swing.text JTextComponent getDocument

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

Introduction

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

Prototype

public Document getDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:Main.java

public TextPrompt(String text, JTextComponent component) {
    this.component = component;
    document = component.getDocument();

    setText(text);/*from www .j  ava  2 s.  c  o  m*/
    setFont(component.getFont());
    setBorder(new EmptyBorder(component.getInsets()));

    component.addFocusListener(this);
    document.addDocumentListener(this);

    component.add(this);
}

From source file:Main.java

public void highLight(JTextComponent component, String patteren) {
    try {//w  ww.  j a  va 2  s . c  o  m
        Document doc = component.getDocument();
        String text = component.getText(0, doc.getLength());
        int pos = component.getCaretPosition();
        if (pos == doc.getLength()) {
            pos = 0;
        }
        int index = text.toUpperCase().indexOf(patteren.toUpperCase(), pos);
        if (index >= 0) {
            component.setSelectionStart(index);
            component.setSelectionEnd(index + patteren.length());
            component.getCaret().setSelectionVisible(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/*from w w w  . j a  v  a  2 s  . co m*/
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (Exception ble) {
        return;
    }
}

From source file:KeymapExample.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/*  w  ww .j a  v a2 s. c  om*/
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (BadLocationException ble) {
        return;
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/*  w  w  w  .jav  a 2s. c o  m*/
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (BadLocationException ble) {
        return;
    }
}

From source file:MyKeyListener.java

public void keyTyped(KeyEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();
    char ch = evt.getKeyChar();

    if (Character.isLowerCase(ch) == false) {
        return;/*from w  w w  .  jav a  2  s .  com*/
    }
    try {
        c.getDocument().insertString(c.getCaretPosition(), "" + Character.toUpperCase(ch), null);
        evt.consume();
    } catch (BadLocationException e) {
    }
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropValueCompletionItem.java

@Override
public void defaultAction(JTextComponent jtc) {
    try {//  www.java  2 s . c o m
        StyledDocument doc = (StyledDocument) jtc.getDocument();
        //Here we remove the characters starting at the start offset
        //and ending at the point where the caret is currently found:
        doc.remove(dotOffset, caretOffset - dotOffset);
        doc.insertString(dotOffset, getText(), null);
        //This statement will close the code completion box:
        Completion.get().hideAll();
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:TextCutPaste.java

/**
 * When the export is complete, remove the old text if the action was a move.
 *//*from  ww  w . ja v a  2  s.  co  m*/
protected void exportDone(JComponent c, Transferable data, int action) {
    if (action != MOVE) {
        return;
    }

    if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
        try {
            JTextComponent tc = (JTextComponent) c;
            tc.getDocument().remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
        } catch (BadLocationException e) {
            System.out.println("Can't remove text from source.");
        }
    }
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java

@Override
public void defaultAction(JTextComponent jtc) {
    try {/*from   w w  w.  j a v a  2  s.  c o  m*/
        StyledDocument doc = (StyledDocument) jtc.getDocument();
        // calculate the amount of chars to remove (by default from property start up to caret position)
        int lenToRemove = caretOffset - propStartOffset;
        if (overwrite) {
            // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed
            // the document state here is different from when the completion was invoked thus we have to
            // find again the offset of the equal sign in the line
            Element lineElement = doc.getParagraphElement(caretOffset);
            String line = doc.getText(lineElement.getStartOffset(),
                    lineElement.getEndOffset() - lineElement.getStartOffset());
            int equalSignIndex = line.indexOf('=');
            if (equalSignIndex >= 0) {
                // from property start to equal sign
                lenToRemove = lineElement.getStartOffset() + equalSignIndex - propStartOffset;
            } else {
                // from property start to end of line (except line terminator)
                lenToRemove = lineElement.getEndOffset() - 1 - propStartOffset;
            }
        }
        // remove characters from the property name start offset
        doc.remove(propStartOffset, lenToRemove);
        doc.insertString(propStartOffset, getText(), null);
        // close the code completion box
        Completion.get().hideAll();
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:de.codesourcery.eve.skills.ui.utils.IntTextFieldValidator.java

protected void attachFilter(JTextComponent comp) {

    // Casting is bad although seems to be 
    // the 'official way' as it's in the Swing tutorials as well....
    final AbstractDocument doc = (AbstractDocument) comp.getDocument();

    final String val = comp.getText();
    if (!isInteger(val) || !isWithinRange(val)) {
        comp.setText(Integer.toString(minValue));
    }/* ww w  .ja va 2 s.  c om*/
    doc.setDocumentFilter(filter);
}