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: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 v  a  2 s . c om
    doc.setDocumentFilter(filter);
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

public void clearCurrentSuggestion(JTextComponent comp) {
    if (toSetIn != null) {
        int selStart = comp.getSelectionStart();
        String text = comp.getText();
        comp.setText(text.substring(0, selStart) + text.substring(comp.getSelectionEnd()));
        comp.setCaretPosition(selStart);
        lastCompletions = null;/*from   w ww  .  ja  v  a  2s  .c o m*/
        lastShownCompletion = 0;
        lastCaretPosition = -1;
        toSetIn = null;
    }
}

From source file:de.unentscheidbar.validation.swing.trigger.DocumentChangeTriggerTest.java

@Override
protected void provokeTrigger(Iterable<JTextComponent> components) {

    for (final JTextComponent c : components) {
        runInEdt(new Callable<Void>() {

            @Override/*from   w  w  w. j  a v  a  2  s.c om*/
            public Void call() throws Exception {

                Document doc = c.getDocument();
                switch (rnd.nextInt(3)) {
                case 0:
                    doc.remove(doc.getStartPosition().getOffset(), doc.getLength() / 2);
                    break;
                case 1:
                    doc.insertString(rnd.nextInt(Math.max(1, doc.getStartPosition().getOffset())),
                            RandomStringUtils.randomAlphanumeric(1 + rnd.nextInt(10)), null);
                    break;
                case 2:
                    c.setText(UUID.randomUUID().toString());
                    break;
                default:
                    Assert.fail();
                }
                return null;
            }
        });

    }
    drainEventQueue();
}

From source file:ch.zhaw.iamp.rct.ui.GrammarWindow.java

private boolean readFileOrShowError(String file, JTextComponent target) {
    File source = new File(file);
    if (source.exists()) {
        try {//  w w w  . j a  va2s  . c om
            target.setText(FileUtils.readFileToString(source));
            target.setEnabled(true);
            target.requestFocus();
            return true;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "The file could not be read: " + ex.getLocalizedMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
            target.setEnabled(false);
        }
    } else {
        target.setText("");
        target.setEnabled(true);
        target.requestFocus();
    }

    return false;
}

From source file:com.aw.swing.mvp.cmp.pick.PickManager.java

/**
 * @param attrName La caja de texto txt<attrName> debe existir
 * @param pick/*from   w ww  .j a  va  2 s . c om*/
 * @return
 */
public Pick registerPick(String attrName, final Pick pick) {
    final String pickName = getPickName(attrName);
    getPicksInfo().add(new PickInfo(pick, attrName, pickName));
    picks.add(pick);
    if (pick instanceof PickImpl) {
        ((PickImpl) pick).setPresenter(presenter);
        pick.setMainAttribute(attrName);
        final JTextComponent jTextComponent = (JTextComponent) presenter.getIpView()
                .getComponent(getTxtPick(attrName));
        jTextComponent.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {

                if (e.isTemporary()) {
                    return;
                }

                // if the pick action is currently executing
                Boolean executingPick = (Boolean) jTextComponent
                        .getClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION);
                if ((executingPick != null) && (executingPick)) {
                    jTextComponent.putClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION, null);
                    //                        return;
                }
                System.out.println("XXX    Focus LOST   2");
                // if the focus will be directed to the pick Button
                Component cmp = e.getOppositeComponent();
                if (cmp instanceof JComponent) {
                    JComponent jComponent = (JComponent) cmp;
                    String actionName = (String) jComponent.getClientProperty(BindingComponent.ATTR_ACTION);
                    if (pickName.equals(actionName)) {
                        return;
                    }
                }

                if (!pick.isPickFilled()) {

                    jTextComponent.setText("");
                }
            }
        });
        jTextComponent.addKeyListener(new PickKeyListener(pick));
        jTextComponent.putClientProperty(PICK_NAME, pickName);
        jTextComponent.putClientProperty(PICK, pick);
        //            JButton jButton = (JButton) presenter.getIpView().getComponent(getBtnPick(attrName));
    }
    return pick;
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

private void cycle(JTextComponent comp, int increment) {
    assert (lastCompletions != null);
    assert (!lastCompletions.isEmpty());
    lastShownCompletion += increment;//www .  ja v a2 s .  co  m
    if (lastShownCompletion >= lastCompletions.size()) {
        lastShownCompletion = 0;
    } else if (lastShownCompletion < 0) {
        lastShownCompletion = lastCompletions.size() - 1;
    }
    String sno = lastCompletions.get(lastShownCompletion);
    toSetIn = sno.substring(lastBeginning.length() - 1);

    StringBuilder alltext = new StringBuilder(comp.getText());

    int oldSelectionStart = comp.getSelectionStart();
    int oldSelectionEnd = comp.getSelectionEnd();

    // replace prefix with new prefix
    int startPos = comp.getSelectionStart() - lastBeginning.length();
    alltext.delete(startPos, oldSelectionStart);
    alltext.insert(startPos, sno.subSequence(0, lastBeginning.length()));

    // replace suffix with new suffix
    alltext.delete(oldSelectionStart, oldSelectionEnd);
    //int cp = oldSelectionEnd - deletedChars;
    alltext.insert(oldSelectionStart, toSetIn.substring(1));

    LOGGER.debug(alltext.toString());
    comp.setText(alltext.toString());
    //comp.setCaretPosition(cp+toSetIn.length()-1);
    comp.select(oldSelectionStart, (oldSelectionStart + toSetIn.length()) - 1);
    lastCaretPosition = comp.getCaretPosition();
    LOGGER.debug("ToSetIn: '" + toSetIn + "'");
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

private void cycle(JTextComponent comp, int increment) {
    assert (lastCompletions != null);
    assert (lastCompletions.length > 0);
    lastShownCompletion += increment;/*  w  ww.  ja v a2  s  .co  m*/
    if (lastShownCompletion >= lastCompletions.length) {
        lastShownCompletion = 0;
    } else if (lastShownCompletion < 0) {
        lastShownCompletion = lastCompletions.length - 1;
    }
    String sno = lastCompletions[lastShownCompletion];
    toSetIn = sno.substring(lastBeginning.length() - 1);

    StringBuilder alltext = new StringBuilder(comp.getText());

    int oldSelectionStart = comp.getSelectionStart();
    int oldSelectionEnd = comp.getSelectionEnd();

    // replace prefix with new prefix
    int startPos = comp.getSelectionStart() - lastBeginning.length();
    alltext.delete(startPos, oldSelectionStart);
    alltext.insert(startPos, sno.subSequence(0, lastBeginning.length()));

    // replace suffix with new suffix
    alltext.delete(oldSelectionStart, oldSelectionEnd);
    //int cp = oldSelectionEnd - deletedChars;
    alltext.insert(oldSelectionStart, toSetIn.substring(1));

    //Util.pr(alltext.toString());
    comp.setText(alltext.toString());
    //comp.setCaretPosition(cp+toSetIn.length()-1);
    comp.select(oldSelectionStart, (oldSelectionStart + toSetIn.length()) - 1);
    lastCaretPosition = comp.getCaretPosition();
    //System.out.println("ToSetIn: '"+toSetIn+"'");
}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

/**
 * Start a new completion attempt (instead of treating a continuation of an existing word or an interrupt of the
 * current word)// www  .  j a  va2s .c  o m
 */
private void startCompletion(StringBuffer currentword, KeyEvent e) {
    JTextComponent comp = (JTextComponent) e.getSource();

    List<String> completed = findCompletions(currentword.toString());
    String prefix = completer.getPrefix();
    String cWord = (prefix != null) && (!prefix.isEmpty()) ? currentword.toString().substring(prefix.length())
            : currentword.toString();

    LOGGER.debug("StartCompletion currentword: >" + currentword + "'<' prefix: >" + prefix + "'<' cword: >"
            + cWord + '<');

    int no = 0; // We use the first word in the array of completions.
    if ((completed != null) && (!completed.isEmpty())) {
        lastShownCompletion = 0;
        lastCompletions = completed;
        String sno = completed.get(no);

        // these two lines obey the user's input
        //toSetIn = Character.toString(ch);
        //toSetIn = toSetIn.concat(sno.substring(cWord.length()));
        // BUT we obey the completion
        toSetIn = sno.substring(cWord.length() - 1);

        LOGGER.debug("toSetIn: >" + toSetIn + '<');

        StringBuilder alltext = new StringBuilder(comp.getText());
        int cp = comp.getCaretPosition();
        alltext.insert(cp, toSetIn);
        comp.setText(alltext.toString());
        comp.setCaretPosition(cp);
        comp.select(cp + 1, (cp + 1 + sno.length()) - cWord.length());
        e.consume();
        lastCaretPosition = comp.getCaretPosition();
        char ch = e.getKeyChar();

        LOGGER.debug("Appending >" + ch + '<');

        if (cWord.length() <= 1) {
            lastBeginning = Character.toString(ch);
        } else {
            lastBeginning = cWord.substring(0, cWord.length() - 1).concat(Character.toString(ch));
        }
    }

}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

/**
 * Start a new completion attempt/*  w w  w . j a  va2s.  com*/
 * (instead of treating a continuation of an existing word or an interrupt of the current word)
 */
private void startCompletion(StringBuffer currentword, KeyEvent e) {
    JTextComponent comp = (JTextComponent) e.getSource();

    String[] completed = findCompletions(currentword.toString(), comp);
    String prefix = completer.getPrefix();
    String cWord = (prefix != null) && (!prefix.isEmpty()) ? currentword.toString().substring(prefix.length())
            : currentword.toString();

    LOGGER.debug("StartCompletion currentword: >" + currentword + "'<' prefix: >" + prefix + "'<' cword: >"
            + cWord + '<');

    int no = 0; // We use the first word in the array of completions.
    if ((completed != null) && (completed.length > 0)) {
        lastShownCompletion = 0;
        lastCompletions = completed;
        String sno = completed[no];

        // these two lines obey the user's input
        //toSetIn = Character.toString(ch);
        //toSetIn = toSetIn.concat(sno.substring(cWord.length()));
        // BUT we obey the completion
        toSetIn = sno.substring(cWord.length() - 1);

        LOGGER.debug("toSetIn: >" + toSetIn + '<');

        StringBuilder alltext = new StringBuilder(comp.getText());
        int cp = comp.getCaretPosition();
        alltext.insert(cp, toSetIn);
        comp.setText(alltext.toString());
        comp.setCaretPosition(cp);
        comp.select(cp + 1, (cp + 1 + sno.length()) - cWord.length());
        e.consume();
        lastCaretPosition = comp.getCaretPosition();
        char ch = e.getKeyChar();

        LOGGER.debug("Appending >" + ch + '<');

        if (cWord.length() <= 1) {
            lastBeginning = Character.toString(ch);
        } else {
            lastBeginning = cWord.substring(0, cWord.length() - 1).concat(Character.toString(ch));
        }
    }

}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

/**
 * If user cancels autocompletion by//  www.j ava2s.  c  o  m
 *   a) entering another letter than the completed word (and there is no other auto completion)
 *   b) space
 * the casing of the letters has to be kept
 * 
 * Global variable "lastBeginning" keeps track of typed letters.
 * We rely on this variable to reconstruct the text 
 * 
 * @param wordSeperatorTyped indicates whether the user has typed a white space character or a
 */
private void setUnmodifiedTypedLetters(JTextComponent comp, boolean lastBeginningContainsTypedCharacter,
        boolean wordSeperatorTyped) {
    if (lastBeginning == null) {
        LOGGER.debug("No last beginning found");
        // There was no previous input (if the user typed a word, where no autocompletion is available)
        // Thus, there is nothing to replace
        return;
    }
    LOGGER.debug("lastBeginning: >" + lastBeginning + '<');
    if (comp.getSelectedText() == null) {
        // if there is no selection
        // the user has typed the complete word, but possibly with a different casing
        // we need a replacement
        if (wordSeperatorTyped) {
            LOGGER.debug("Replacing complete word");
        } else {
            // if user did not press a white space character (space, ...),
            // then we do not do anything
            return;
        }
    } else {
        LOGGER.debug("Selected text " + comp.getSelectedText() + " will be removed");
        // remove completion suggestion
        comp.replaceSelection("");
    }

    lastCaretPosition = comp.getCaretPosition();

    int endIndex = lastCaretPosition - lastBeginning.length();
    if (lastBeginningContainsTypedCharacter) {
        // the current letter is NOT contained in comp.getText(), but in lastBeginning
        // thus lastBeginning.length() is one too large
        endIndex++;
    }
    String text = comp.getText();
    comp.setText(text.substring(0, endIndex).concat(lastBeginning).concat(text.substring(lastCaretPosition)));
    if (lastBeginningContainsTypedCharacter) {
        // the current letter is NOT contained in comp.getText()
        // Thus, cursor position also did not get updated
        lastCaretPosition++;
    }
    comp.setCaretPosition(lastCaretPosition);
    lastBeginning = null;
}