From 543e4eda68693946955b229d4e53fe0b7aad06b4 Mon Sep 17 00:00:00 2001 From: Marc Halperin Date: Wed, 17 Jun 2015 14:52:28 -0700 Subject: [PATCH] Select all text in text field when given focus Selects all the text in the text field when it gains focus --- src/org/ohdsi/rabbitInAHat/FilterDialog.java | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/org/ohdsi/rabbitInAHat/FilterDialog.java b/src/org/ohdsi/rabbitInAHat/FilterDialog.java index 43404808..dfca4bea 100644 --- a/src/org/ohdsi/rabbitInAHat/FilterDialog.java +++ b/src/org/ohdsi/rabbitInAHat/FilterDialog.java @@ -4,6 +4,8 @@ import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -48,21 +50,25 @@ public FilterDialog(Window parentWindow){ // Add key listener to send search string as it's being typed sourceSearchField.addKeyListener(new SearchListener() ); + sourceSearchField.addFocusListener(new SearchFocusListener()); JLabel sourceLabel = new JLabel("Filter Source:",JLabel.TRAILING); JButton sourceClearBtn = new JButton("Clear"); contentPane.add(sourceLabel); contentPane.add(sourceSearchField); sourceClearBtn.addActionListener(this); sourceClearBtn.setActionCommand("Clear Source"); + sourceClearBtn.setFocusable(false); contentPane.add(sourceClearBtn); targetSearchField.addKeyListener(new SearchListener() ); + targetSearchField.addFocusListener(new SearchFocusListener()); JLabel targetLabel = new JLabel("Filter Target:",JLabel.TRAILING); JButton targetClearBtn = new JButton("Clear"); contentPane.add(targetLabel); contentPane.add(targetSearchField); targetClearBtn.addActionListener(this); targetClearBtn.setActionCommand("Clear Target"); + targetClearBtn.setFocusable(false); contentPane.add(targetClearBtn); layout.putConstraint(SpringLayout.WEST, sourceLabel, 5, SpringLayout.WEST, contentPane); @@ -181,6 +187,23 @@ public void keyTyped(KeyEvent event) { } + public class SearchFocusListener implements FocusListener { + + @Override + public void focusGained(FocusEvent e) { + // TODO Auto-generated method stub + JTextField jtf = (JTextField) e.getComponent(); + jtf.selectAll(); + } + + @Override + public void focusLost(FocusEvent e) { + // TODO Auto-generated method stub + + } + + } + }