Here you can find the source of attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)
private static void attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)
//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(); } }); } }