001    /* JAPI - (Yet anothr (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing;
022    
023    import java.awt.BorderLayout;
024    import java.io.File;
025    import javax.swing.JComponent;
026    import javax.swing.JTextField;
027    import javax.swing.JFileChooser;
028    
029    /** A class that displays a textfield for a file and a button for choosing the file.
030     * TODO
031     * @author <a href="mailto:Christian.Hujer@itcqis.com">Christian Hujer</a>
032     */
033    public final class FileField extends JComponent {
034    
035        /** The JTextField used to display the filename.
036         * @serial include
037         */
038        private final JTextField textField;
039    
040        /** Create a FileField.
041         * @param size Number of columns for the textfield part.
042         */
043        public FileField(final int size) {
044            textField = new JTextField(size);
045            final JFileChooserButton button = new JFileChooserButton(textField, JFileChooser.FILES_ONLY);
046            setLayout(new BorderLayout());
047            add(textField, BorderLayout.CENTER);
048            add(button, BorderLayout.LINE_END);
049        }
050    
051        /** Create a FileField.
052         * @param size Number of columns for the textfield part.
053         */
054        public FileField(final JFileChooser fileChooser, final int size) {
055            textField = new JTextField(size);
056            final JFileChooserButton button = new JFileChooserButton(fileChooser, textField, JFileChooser.FILES_ONLY);
057            setLayout(new BorderLayout());
058            add(textField, BorderLayout.CENTER);
059            add(button, BorderLayout.LINE_END);
060        }
061    
062        /** Return the selected file.
063         * @return selected file
064         */
065        public File getSelectedFile() {
066            return new File(textField.getText());
067        }
068    
069        /** Return the selected filename.
070         * @return selected filename
071         */
072        public String getSelectedFilename() {
073            return textField.getText();
074        }
075    
076    } // class FileField