/* * JFileField.java * * Created on January 9, 2008, 12:08 PM */ package nl.eelkespaak.gui; import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; /** * This component provides a text field for the manual input of a file, as well * as an associated 'Browse' button, allowing the user to browse for a file and * have its location show up automatically in the text field. * * @author Eelke Spaak * @see javax.swing.JFileChooser */ public class JFileField extends javax.swing.JPanel { private String extensionFilter; private String fileTypeName; private int fileSelectionMode = JFileChooser.FILES_ONLY; /** Creates new form JFileField */ public JFileField() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // private void initComponents() { textField = new javax.swing.JTextField(); browseButton = new javax.swing.JButton(); browseButton.setText("..."); browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). addGroup(layout.createSequentialGroup().addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE). addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED). addComponent(browseButton))); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE). addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(browseButton))); }// private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(fileSelectionMode); if (extensionFilter != null && fileTypeName != null) { FileNameExtensionFilter filter = new FileNameExtensionFilter( fileTypeName, extensionFilter.split(",")); chooser.setFileFilter(filter); } try { File f = new File(textField.getText()); if (f.exists()) { if (f.isDirectory()) { chooser.setCurrentDirectory(f); } else { chooser.setSelectedFile(f); } } } catch (Exception ignore) { } int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { textField.setText(chooser.getSelectedFile().getPath()); } } /** * Returns the value of the text field. * * @return the value of the text field */ public String getText() { return textField.getText(); } /** * Sets the value of the text field. * * @param the value to put in the text field */ public void setText(String text) { textField.setText(text); } /** * Returns the extension filter (a comma-separated string of extensions) * that the JFileChooser should use when browsing for a file. * * @return the extension filter */ public String getExtensionFilter() { return extensionFilter; } /** * Sets the extension filter (a comma-separated string of extensions) * that the JFileChooser should use when browsing for a file. * * @param extensionFilter the extension filter */ public void setExtensionFilter(String extensionFilter) { this.extensionFilter = extensionFilter; } /** * Returns the description of the file types the JFileChooser should be * browsing for. * * @return the file type description */ public String getFileTypeName() { return fileTypeName; } /** * Sets the description of the file types the JFileChooser should be * browsing for. * * @param fileTypeName the file type description */ public void setFileTypeName(String fileTypeName) { this.fileTypeName = fileTypeName; } /** * Returns the file selection mode to be used by the JFileChooser. * * @return the type of files to be displayed * @see javax.swing.JFileChooser#getFileSelectionMode() */ public int getFileSelectionMode() { return fileSelectionMode; } /** * Sets the file selection mode to be used by the JFileChooser. * * @param fileSelectionMode the type of files to be displayed * @see javax.swing.JFileChooser#setFileSelectionMode(int) */ public void setFileSelectionMode(int fileSelectionMode) { this.fileSelectionMode = fileSelectionMode; } /** * Implemented to make layout managers align the JFileField on the baseline * of the included text field, rather than on the absolute bottom of the * JPanel. * * @param w * @param h * @return */ @Override public int getBaseline(int w, int h) { return textField.getBaseline(w, h); } // Variables declaration - do not modify private javax.swing.JButton browseButton; private javax.swing.JTextField textField; // End of variables declaration }