1 54 55 package com.mullassery.act.gui.components; 56 57 import java.awt.BorderLayout ; 58 import java.awt.Dimension ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.io.File ; 62 import javax.swing.JButton ; 63 import javax.swing.JFileChooser ; 64 import javax.swing.JPanel ; 65 66 import com.mullassery.act.gui.TextData; 67 68 73 public class TFileField extends JPanel implements TextData { 74 private JFileChooser jfc; 75 private File lastFile = new File ("."); 76 private final JButton jb; 77 private final TTextField jtf; 78 79 public TFileField() { 80 this(null); 81 } 82 83 public TFileField(String file) { 84 if (file != null && file.length() > 0) { 85 File f = new java.io.File (file); 86 file = f.getAbsolutePath(); 87 setLastFile(f); 88 } 89 jtf = new TTextField(file, 16); 90 jb = new JButton ("..."); 91 jb.addActionListener(new ActionListener () { 92 public void actionPerformed(ActionEvent ae) { 93 if (jfc == null) { 94 jfc = new JFileChooser (); 95 jfc.setApproveButtonText("Select"); 96 jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 97 } 98 jfc.setCurrentDirectory(lastFile); 99 if(getText() != null && getText().length() > 0) { 100 jfc.setSelectedFile(new File (getText())); 101 } 102 int ret = jfc.showOpenDialog(jtf.getParent()); 103 if (ret == JFileChooser.APPROVE_OPTION) { 104 jtf.setText("" + jfc.getSelectedFile()); 105 setLastFile(jfc.getSelectedFile()); 106 } 107 } 108 109 }); 110 setPreferredSize(new Dimension (200, 20)); 111 this.setLayout(new BorderLayout ()); 112 this.add(jtf, BorderLayout.CENTER); 113 this.add(jb, BorderLayout.EAST); 114 } 115 116 private void setLastFile(File f) { 117 lastFile = f; 118 if (!lastFile.isDirectory()) 120 lastFile = lastFile.getParentFile(); 121 } 122 123 public String getText() { 124 return jtf.getText(); 125 } 126 127 public void setText(String value) { 128 jtf.setText(value); 129 } 130 } 131 | Popular Tags |