Java JTextField Focus attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)

Here you can find the source of attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)

Description

Attaches a listener to the specified label that directs the focus to the supplied text (resulting in an HTML form-like connection of the label and its input field).

License

Open Source License

Declaration

private static void attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.*;
import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

public class Main {
    /**/*from   w  w w.j  a  va 2s . com*/
     * Attaches a listener to the specified label that directs the focus to the
     * supplied text (resulting in an HTML form-like connection of the label and
     * its input field).
     */
    private static void attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField) {

        label.addMouseListener(new MouseInputAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                focusAccompanyingInput();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                focusAccompanyingInput();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // intentionally left empty
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                focusAccompanyingInput();
            }

            private void focusAccompanyingInput() {
                textField.grabFocus();
            }
        });
    }
}

Related

  1. _createJTextField(String name, FocusListener fl)
  2. addListenerToCommitOnFocusLost(final JTextField field)
  3. addNewFocusListenerForTextField(final JTextField textField, final Runnable r)
  4. clearOnFocus(final JTextField jtf, final String onlyInText)
  5. configureFocusActionKeys(JTextField textField, boolean primary)
  6. makeSureUserGetsFocus(final JTextField field)
  7. minimoCaracteres(JTextField txt, FocusEvent e, int pValor)