Here you can find the source of registerBrowseButtonListener(final JComboBox comboBox, final JButton button, final boolean chooseFile, final boolean isOpen, final FileFilter fileFilter, final File initialDirectory)
public static JFileChooser registerBrowseButtonListener(final JComboBox comboBox, final JButton button, final boolean chooseFile, final boolean isOpen, final FileFilter fileFilter, final File initialDirectory)
//package com.java2s; /********************************************************************************************** * * Asprise OCR Java API/*from w ww . j a v a 2s .c om*/ * Copyright (C) 1998-2015. Asprise Inc. <asprise.com> * * This file is licensed under the GNU Affero General Public License version 3 as published by * the Free Software Foundation. * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions * and limitations under the License. * * You should have received a copy of the GNU Affero General Public License. If not, please * visit <http://www.gnu.org/licenses/agpl-3.0.html>. * **********************************************************************************************/ import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; public class Main { public static JFileChooser registerBrowseButtonListener(final JComboBox comboBox, final JButton button, final boolean chooseFile, final boolean isOpen, final FileFilter fileFilter, final File initialDirectory) { if (!comboBox.isEditable()) { throw new IllegalArgumentException("The combo box must be editable."); } final JFileChooser fileChooser = new JFileChooser(); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { fileChooser.setCurrentDirectory(comboBox.getSelectedItem() == null || comboBox.getSelectedItem().toString().trim().length() == 0 ? initialDirectory : new File(comboBox.getSelectedItem().toString())); fileChooser .setFileSelectionMode(chooseFile ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY); if (fileFilter != null) { fileChooser.addChoosableFileFilter(fileFilter); } int ret = isOpen ? fileChooser.showOpenDialog(comboBox) : fileChooser.showSaveDialog(comboBox); if (ret == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); comboBox.getEditor().setItem(file.getAbsolutePath()); } } }; button.addActionListener(listener); return fileChooser; } }