Java JTextField Select newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)

Here you can find the source of newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)

Description

new File Chooser

License

Apache License

Declaration

public static JFileChooser newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField,
            String globalLocation) 

Method Source Code

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

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.JTextField;

public class Main {
    public static JFileChooser newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField,
            String globalLocation) {
        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;
    }/*from  w ww .j  a v a  2  s . c  om*/

    public static void setCurrentDirectory(JFileChooser fileChooser, String location) {
        if (pointsToFile(location))
            location = location.substring(0, location.lastIndexOf("\\"));
        File dir = new File(location);
        if (dir.exists())
            fileChooser.setCurrentDirectory(dir);
    }

    private static boolean pointsToFile(String location) {
        if (location.lastIndexOf("\\") < location.lastIndexOf("."))
            return true;
        return false;
    }
}

Related

  1. addSelectAllListener(final JTextField field)
  2. addSelectOnFocus(JTextField one)
  3. buttonFolderSelect(final JTextField text)
  4. createSelected(final JTextField textField, final JButton... btns)
  5. focusAndSelectTextInTextField(JTextField textField)
  6. recipientHintSelected(String hintString, JTextField toList, boolean shiftKeyPressed)
  7. selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
  8. selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield)
  9. wrapWithFileChooser(final Component aParent, final JTextField aTextField, final int aFileSelectionMode)