1 16 17 package swingwt.awt; 18 19 import swingwtx.swing.*; 20 21 import java.io.File ; 22 import java.io.FileFilter ; 23 24 30 public class FileDialog extends Dialog { 31 32 public static final int LOAD = 0; 33 public static final int SAVE = 1; 34 35 protected int mode = LOAD; 36 protected JFileChooser swingFileChooser = null; 37 protected Component parent = null; 38 protected String file = null; 39 protected String dir = null; 40 protected FileFilter filter = null; 41 42 public FileDialog(Frame parent) { this(parent, "Open", LOAD); } 43 public FileDialog(Frame parent, String title) { this(parent, title, LOAD); } 44 45 public FileDialog(Frame parent, String title, int mode) { 46 swingFileChooser = new JFileChooser(); 47 swingFileChooser.setTitle(title); 48 this.mode = mode; 49 this.parent = parent; 50 } 51 52 public void show() { 53 setVisible(true); 54 } 55 public void setVisible(boolean b) { 56 if (!b) return; 57 int result = 0; 58 if (mode == LOAD) 59 result = swingFileChooser.showOpenDialog(parent); 60 else 61 result = swingFileChooser.showSaveDialog(parent); 62 if (result == JFileChooser.APPROVE_OPTION) { 63 file = swingFileChooser.getSelectedFile().getAbsolutePath(); 64 dir = swingFileChooser.getSelectedFile().getPath(); 65 } 66 else { 67 file = null; 68 dir = null; 69 } 70 } 71 72 public String getDirectory() { 73 return dir; 74 } 75 76 public String getFile() { 77 return file; 78 } 79 80 public FileFilter getFilenameFilter() { 81 return filter; 82 } 83 84 public void setFilenameFilter(FileFilter f) { 85 filter = f; 86 } 87 88 public void setDirectory(String dir) { 89 swingFileChooser.setSelectedFile(new File (dir)); 90 } 91 92 public void setFile(String file) { 93 swingFileChooser.setSelectedFile(new File (file)); 94 } 95 96 public int getMode() { 97 return mode; 98 } 99 100 public void setMode(int mode) { 101 this.mode = mode; 102 } 103 } 104 | Popular Tags |