Example usage for javax.swing.text JTextComponent addFocusListener

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

Introduction

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

Prototype

public synchronized void addFocusListener(FocusListener l) 

Source Link

Document

Adds the specified focus listener to receive focus events from this component when this component gains input focus.

Usage

From source file:Main.java

public static void focusSelect(final JTextComponent tf) {
    tf.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            tf.selectAll();//from   w w w. j  av a 2 s .  c  o m
        }
    });
}

From source file:Main.java

public TextPrompt(String text, JTextComponent component) {
    this.component = component;
    document = component.getDocument();//from www  . ja va2  s.  com

    setText(text);
    setFont(component.getFont());
    setBorder(new EmptyBorder(component.getInsets()));

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

    component.add(this);
}

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

/**
 * @param attrName La caja de texto txt<attrName> debe existir
 * @param pick/*w w w.jav a 2s .com*/
 * @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:edu.ku.brc.af.ui.db.DatabaseLoginPanel.java

/**
 * Creates a focus listener so the UI is updated when the focus leaves
 * @param textField  the text field to be changed
 *//*from ww w.ja  v a 2  s  . c  om*/
protected void addFocusListenerForTextComp(final JTextComponent textField) {
    textField.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            updateUIControls();
        }
    });
}

From source file:net.sf.jabref.EntryEditor.java

/**
 * NOTE: This method is only used for the source panel, not for the
 * other tabs. Look at EntryEditorTab for the setup of text components
 * in the other tabs./*from w  w w  . j av  a 2 s . co m*/
 */
private void setupJTextFieldForSourceArea(JTextComponent ta) {
    setupSwingComponentKeyBindings(ta);

    // HashSet<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>(ta.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));          
    HashSet<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>();
    keys.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB"));
    ta.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);

    // keys = new HashSet<AWTKeyStroke>(ta.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
    keys = new HashSet<AWTKeyStroke>();
    keys.add(KeyStroke.getKeyStroke("shift pressed TAB"));
    ta.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys);

    ta.addFocusListener(new FieldListener());
}

From source file:edu.ku.brc.af.ui.forms.validation.FormValidator.java

/**
 * @param textField textField to be hooked up
 * @param id id of control/*w w  w. j a  va2  s.c  o m*/
 * @param isRequiredArg whether the field must be filled in
 * @param valType the type of validation to do
 * @param valStr the validation rule where the subject is its name
 * @param changeListenerOnly indicates whether to create a validator
 */
public void hookupTextField(final JTextComponent textField, final String id, final boolean isRequiredArg,
        final UIValidator.Type valType, final String valStr, final boolean changeListenerOnly) {

    fields.put(id, textField);

    UIValidator.Type type = isRequiredArg ? UIValidator.Type.Changed : valType;

    UIValidator uiv = null;
    if (StringUtils.isEmpty(valStr)) {
        if (valType != UIValidator.Type.None) {
            uiv = createValidator(textField, valType);
        }
    } else {
        uiv = changeListenerOnly ? null : createValidator(textField, type, valStr);
    }

    if (uiv != null) {
        DataChangeNotifier dcn = new DataChangeNotifier(id, textField, uiv);
        dcn.addDataChangeListener(this);

        dcNotifiers.put(id, dcn);

        if (type == UIValidator.Type.Changed || isRequiredArg || changeListenerOnly) {
            textField.getDocument().addDocumentListener(dcn);

        } else if (type == UIValidator.Type.Focus) {
            textField.addFocusListener(dcn);

        } else {
            // Do nothing for UIValidator.Type.OK
        }
    }

    addRuleObjectMapping(id, textField);
}

From source file:net.sf.jabref.gui.entryeditor.EntryEditor.java

/**
 * NOTE: This method is only used for the source panel, not for the
 * other tabs. Look at EntryEditorTab for the setup of text components
 * in the other tabs.//  w  w w.j  av a 2s  .c om
 */
private void setupJTextComponent(JTextComponent textComponent) {
    // Set up key bindings and focus listener for the FieldEditor.
    InputMap inputMap = textComponent.getInputMap(JComponent.WHEN_FOCUSED);
    ActionMap actionMap = textComponent.getActionMap();

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_STORE_FIELD), "store");
    actionMap.put("store", getStoreFieldAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_NEXT_PANEL), "right");
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_NEXT_PANEL_2), "right");
    actionMap.put("right", getSwitchRightAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_PREVIOUS_PANEL), "left");
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.ENTRY_EDITOR_PREVIOUS_PANEL_2), "left");
    actionMap.put("left", getSwitchLeftAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.HELP), "help");
    actionMap.put("help", getHelpAction());
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.SAVE_DATABASE), "save");
    actionMap.put("save", getSaveDatabaseAction());

    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.NEXT_TAB), "nexttab");
    actionMap.put("nexttab", frame.nextTab);
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.PREVIOUS_TAB), "prevtab");
    actionMap.put("prevtab", frame.prevTab);

    Set<AWTKeyStroke> keys = new HashSet<>(
            textComponent.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
    keys.clear();
    keys.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB"));
    textComponent.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
    keys = new HashSet<>(textComponent.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
    keys.clear();
    keys.add(KeyStroke.getKeyStroke("shift pressed TAB"));
    textComponent.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys);

    textComponent.addFocusListener(new FieldListener());
}

From source file:org.executequery.gui.text.TextUndoManager.java

/** Creates a new instance of TextUndoManager */
public TextUndoManager(JTextComponent textComponent) {

    this.textComponent = textComponent;
    document = textComponent.getDocument();
    document.addUndoableEditListener(this);

    // add the focus listener
    textComponent.addFocusListener(this);

    // retrieve the undo/redo actions from the cache
    undoCommand = ActionBuilder.get("undo-command");
    redoCommand = ActionBuilder.get("redo-command");

    // initialise the compound edit
    compoundEdit = new CompoundEdit();
}