1 package rero.dialogs.toolkit; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 import javax.swing.event.*; 8 9 import rero.dck.*; 10 11 import java.io.*; 12 13 public class FileField extends JPanel implements ActionListener 14 { 15 protected JTextField text; 16 protected SmallButton button; 17 protected JFileChooser chooser; 18 19 protected boolean directory; 20 21 public FileField(File value, boolean _directory) 22 { 23 text = new JTextField(); 24 button = new SmallButton(text.getBorder(), "Click to open a file chooser"); 25 26 button.addActionListener(this); 27 28 setLayout(new BorderLayout(2, 2)); 29 30 add(text, BorderLayout.CENTER); 31 add(button, BorderLayout.EAST); 32 33 text.setText(value.getAbsolutePath()); 34 35 directory = _directory; 36 } 37 38 public File getSelectedFile() 39 { 40 return new File(text.getText()); 41 } 42 43 public void actionPerformed(ActionEvent ev) 44 { 45 if (chooser == null) 46 { 47 chooser = new JFileChooser(); 48 } 49 50 if (directory) 51 { 52 chooser.setApproveButtonText("Select Directory"); 53 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 54 } 55 else 56 { 57 chooser.setApproveButtonText("Select File"); 58 } 59 60 chooser.setSelectedFile(new File(text.getText())); 61 62 if (chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION) 63 { 64 text.setText(chooser.getSelectedFile().getAbsolutePath()); 65 text.requestFocus(); 66 } 67 } 68 } 69 70 71 | Popular Tags |