Java JTextField Select wrapWithFileChooser(final Component aParent, final JTextField aTextField, final int aFileSelectionMode)

Here you can find the source of wrapWithFileChooser(final Component aParent, final JTextField aTextField, final int aFileSelectionMode)

Description

wrap With File Chooser

License

Apache License

Parameter

Parameter Description
aParent the parent to the chooser dialog
aTextField the text field representing the file choice
aFileSelectionMode the argument to pass to javax.swing.JFileChooser#setFileSelectionMode .

Declaration

public static Component wrapWithFileChooser(final Component aParent, final JTextField aTextField,
        final int aFileSelectionMode) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    /**//from   w ww. j a  v a 2  s .  co  m
     * @param aParent            the parent to the chooser dialog
     * @param aTextField         the text field representing the file choice
     * @param dotDotDot          the chooser popup button
     * @param aFileSelectionMode the argument to pass to {@link javax.swing.JFileChooser#setFileSelectionMode}.
     */
    public static Component wrapWithFileChooser(final Component aParent, final JTextField aTextField,
            final JButton dotDotDot, final int aFileSelectionMode) {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(aTextField, BorderLayout.CENTER);
        pane.add(dotDotDot, BorderLayout.EAST);
        dotDotDot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser(aTextField.getText());
                chooser.setFileSelectionMode(aFileSelectionMode);
                int choice = chooser.showDialog(aParent, "Select");
                if (choice == JFileChooser.APPROVE_OPTION) {
                    aTextField.setText(chooser.getSelectedFile().getAbsolutePath());
                }
            }
        });
        return pane;
    }

    /**
     * @param aParent            the parent to the chooser dialog
     * @param aTextField         the text field representing the file choice
     * @param aFileSelectionMode the argument to pass to {@link javax.swing.JFileChooser#setFileSelectionMode}.
     */
    public static Component wrapWithFileChooser(final Component aParent, final JTextField aTextField,
            final int aFileSelectionMode) {
        return wrapWithFileChooser(aParent, aTextField, new JButton("..."), aFileSelectionMode);
    }
}

Related

  1. focusAndSelectTextInTextField(JTextField textField)
  2. newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)
  3. recipientHintSelected(String hintString, JTextField toList, boolean shiftKeyPressed)
  4. selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
  5. selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield)