Example usage for javax.swing.text JTextComponent requestFocus

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

Introduction

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

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * /*w  w  w  .ja v a 2 s.  c o  m*/
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner//from  w ww.  j  a va  2s .  c om
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

public void focusLost(FocusEvent evt) {
    final JTextComponent c = (JTextComponent) evt.getSource();
    String s = c.getText();//from ww  w .j  a  v  a  2 s  .c om

    if (evt.isTemporary()) {
        return;
    }
    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            System.out.println("must only contain digits");
            c.requestFocus();
            break;
        }
    }
}

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

private boolean writeFileOrShowError(String file, JTextComponent source) {
    try {/*from w w w. j a va 2 s  . c  o m*/
        FileUtils.writeStringToFile(new File(file), source.getText());
        source.requestFocus();
        return true;
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(this, "The file could not be written: " + ex.getLocalizedMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
    }

    return false;
}

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 {/*from   w  w w. jav a2  s .  c  o m*/
            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.mindcognition.mindraider.ui.swing.concept.annotation.renderer.AbstractTextAnnotationRenderer.java

/**
 * Search annotation from the current carret position.
 *//*from w ww  . jav  a2  s.c o  m*/
public void searchAnnotation(String searchString, boolean again) {
    logger.debug("searchAnnotation() " + searchString); // {{debug}}
    JTextComponent textComponent;
    if (inViewMode()) {
        textComponent = viewer;
        logger.debug("Searching viewer..."); // {{debug}}
    } else {
        textComponent = editor;
        logger.debug("Searching editor..."); // {{debug}}
    }

    if (!again) {
        textComponent.setCaretPosition(0);
    }

    if (searchString != null && searchString.length() > 0) {
        Document document = textComponent.getDocument();
        try {
            int idx = textComponent.getDocument().getText(0, document.getLength()).indexOf(searchString,
                    textComponent.getCaretPosition());
            if (idx < 0) {
                // try it from the beginning
                idx = textComponent.getDocument().getText(0, document.getLength()).indexOf(searchString);
            } else if (idx > 0) {
                textComponent.setCaretPosition(idx);
                textComponent.requestFocus();
                textComponent.select(idx, idx + searchString.length());
            }
        } catch (Exception e) {
            // TODO no bundle!
            logger.debug(Messages.getString("ConceptJPanel.unableToSearch", e.getMessage()));
        }
    }
}

From source file:com.hexidec.ekit.EkitCore.java

/**
 * Method for finding (and optionally replacing) a string in the text
 *//*ww w  .  j  a  v  a  2 s . co m*/
private int findText(String findTerm, String replaceTerm, boolean bCaseSenstive, int iOffset) {
    JTextComponent jtpFindSource;
    if (sourceWindowActive || jtpSource.hasFocus()) {
        jtpFindSource = (JTextComponent) jtpSource;
    } else {
        jtpFindSource = (JTextComponent) jtpMain;
    }
    int searchPlace = -1;
    try {
        Document baseDocument = jtpFindSource.getDocument();
        searchPlace = (bCaseSenstive
                ? baseDocument.getText(0, baseDocument.getLength()).indexOf(findTerm, iOffset)
                : baseDocument.getText(0, baseDocument.getLength()).toLowerCase()
                        .indexOf(findTerm.toLowerCase(), iOffset));
        if (searchPlace > -1) {
            if (replaceTerm != null) {
                AttributeSet attribs = null;
                if (baseDocument instanceof HTMLDocument) {
                    Element element = ((HTMLDocument) baseDocument).getCharacterElement(searchPlace);
                    attribs = element.getAttributes();
                }
                baseDocument.remove(searchPlace, findTerm.length());
                baseDocument.insertString(searchPlace, replaceTerm, attribs);
                jtpFindSource.setCaretPosition(searchPlace + replaceTerm.length());
                jtpFindSource.requestFocus();
                jtpFindSource.select(searchPlace, searchPlace + replaceTerm.length());
            } else {
                jtpFindSource.setCaretPosition(searchPlace + findTerm.length());
                jtpFindSource.requestFocus();
                jtpFindSource.select(searchPlace, searchPlace + findTerm.length());
            }
        }
    } catch (BadLocationException ble) {
        logException("BadLocationException in actionPerformed method", ble);
        new SimpleInfoDialog(this.getWindow(), Translatrix.getTranslationString("Error"), true,
                Translatrix.getTranslationString("ErrorBadLocationException"), SimpleInfoDialog.ERROR);
    }
    return searchPlace;
}

From source file:org.executequery.gui.editor.autocomplete.QueryEditorAutoCompletePopupProvider.java

public void firePopupTrigger() {

    try {//from  w  ww .  j av a  2  s.co m

        final JTextComponent textComponent = queryEditorTextComponent();

        Caret caret = textComponent.getCaret();
        final Rectangle caretCoords = textComponent.modelToView(caret.getDot());

        addFocusActions();

        resetCount = 0;
        captureAndResetListValues();

        popupMenu().show(textComponent, caretCoords.x, caretCoords.y + caretCoords.height);
        textComponent.requestFocus();

    } catch (BadLocationException e) {

        debug("Error on caret coordinates", e);
    }

}