Skip to content

Commit

Permalink
Select all text in text field when given focus
Browse files Browse the repository at this point in the history
Selects all the text in the text field when it gains focus
  • Loading branch information
Marc Halperin committed Jun 17, 2015
1 parent 7a8ca2a commit 543e4ed
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/org/ohdsi/rabbitInAHat/FilterDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

}

}


}

0 comments on commit 543e4ed

Please sign in to comment.