Java Utililty Methods JTextField Select

List of utility methods to do JTextField Select

Description

The list of methods to do JTextField Select are organized into topic(s).

Method

voidaddSelectAllListener(final JTextField field)
Automatically selects the field's text when it gains focus.
field.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
        field.selectAll();
});
voidaddSelectOnFocus(JTextField one)
Add a focusGained-listener to this textfield which selects all text once the component has gained focus.
final JTextField text = one;
text.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                text.selectAll();
...
JButtonbuttonFolderSelect(final JTextField text)
Creates a button that allows select a directory
final JButton boton = new JButton("Select...");
final JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
boton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        int action = f.showOpenDialog(boton);
        if (action == JFileChooser.APPROVE_OPTION) {
...
JPanelcreateSelected(final JTextField textField, final JButton... btns)
create Selected
final Dimension btnDim = new Dimension(18, 18);
initComponentHeight(textField);
final JPanel jp = new JPanel(new GridBagLayout());
jp.setBorder(textField.getBorder());
textField.setBorder(BorderFactory.createEmptyBorder());
jp.setPreferredSize(textField.getPreferredSize());
final GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
...
voidfocusAndSelectTextInTextField(JTextField textField)
focus And Select Text In Text Field
int length = textField.getText().length();
textField.setSelectionStart(0);
textField.setSelectionEnd(length);
textField.requestFocus();
JFileChoosernewFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)
new File Chooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(dialogTitle);
fileChooser.setFileSelectionMode(fileSelectionMode);
if (!textField.getText().trim().isEmpty())
    setCurrentDirectory(fileChooser, textField.getText().trim());
else if (null != globalLocation && !globalLocation.isEmpty())
    setCurrentDirectory(fileChooser, globalLocation);
return fileChooser;
...
voidrecipientHintSelected(String hintString, JTextField toList, boolean shiftKeyPressed)
recipient Hint Selected
if (shiftKeyPressed) {
    String currentList = toList.getText();
    if (currentList == null || currentList.length() == 0)
        toList.setText(hintString);
    else
        toList.setText(currentList + ", " + hintString);
} else
    toList.setText(hintString);
...
voidselectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
select All On Focus
jtf.addFocusListener(new FocusListener() {
    @Override
    public void focusLost(FocusEvent e) {
        if (recoverText != null) {
            String text = jtf.getText();
            if (text.isEmpty()) {
                jtf.setForeground(forTip);
                jtf.setText(recoverText);
...
voidselectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield)
This method selects the first entry in a jTree that start with the text that is entered in the filter-textfield.
DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
MutableTreeNode root = (MutableTreeNode) dtm.getRoot();
String text = textfield.getText().toLowerCase();
if (root != null && !text.isEmpty()) {
    for (int cnt = 0; cnt < dtm.getChildCount(root); cnt++) {
        MutableTreeNode child = (MutableTreeNode) dtm.getChild(root, cnt);
        String childtext = child.toString().toLowerCase();
        if (childtext.startsWith(text)) {
...
ComponentwrapWithFileChooser(final Component aParent, final JTextField aTextField, final int aFileSelectionMode)
wrap With File Chooser
return wrapWithFileChooser(aParent, aTextField, new JButton("..."), aFileSelectionMode);