Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileChooser.java
More file actions
Latest commit
executable file
·125 lines (106 loc) · 3.29 KB
/
Copy pathFileChooser.java
File metadata and controls
executable file
·125 lines (106 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjava.util.Properties;
importjava.io.*;
importjava.net.*;
/**
* A class to make working with a file chooser easier
* for students. It uses a JFileChooser to let the user
* pick a file and returns the chosen file name.
*
* @author Barb Ericson ericson@cc.gatech.edu
*/
publicclassFileChooser
{
/////////////////////// methods /////////////////////////////
/**
* Method to get the full path for the passed file name
* @param fileName the name of a file
* @return the full path for the file
*/
publicstaticStringgetMediaPath(StringfileName)
{
Stringpath = null;
Stringdirectory = getMediaDirectory();
booleandone = true;
// get the full path
path = directory + fileName;
returnpath;
}
/**
* Method to pick an item using the file chooser
* @param fileChooser the file Chooser to use
* @return the path name
*/
publicstaticStringpickPath(JFileChooserfileChooser)
{
Stringpath = null;
/* create a JFrame to be the parent of the file
* chooser open dialog if you don't do this then
* you may not see the dialog.
*/
JFrameframe = newJFrame();
frame.setAlwaysOnTop(true);
// get the return value from choosing a file
intreturnVal = fileChooser.showOpenDialog(frame);
// if the return value says the user picked a file
if (returnVal == JFileChooser.APPROVE_OPTION)
path = fileChooser.getSelectedFile().getPath();
returnpath;
}
/**
* Method to let the user pick a file and return
* the full file name as a string. If the user didn't
* pick a file then the file name will be null.
* @return the full file name of the picked file or null
*/
publicstaticStringpickAFile()
{
JFileChooserfileChooser = null;
// start off the file name as null
StringfileName = null;
// get the current media directory
StringmediaDir = getMediaDirectory();
/* create a file for this and check that the directory exists
* and if it does set the file chooser to use it
*/
try {
Filefile = newFile(mediaDir);
if (file.exists())
fileChooser = newJFileChooser(file);
} catch (Exceptionex) {
}
// if no file chooser yet create one
if (fileChooser == null)
fileChooser = newJFileChooser();
// pick the file
fileName = pickPath(fileChooser);
returnfileName;
}
/**
* Method to get the directory for the media
* @return the media directory
*/
publicstaticStringgetMediaDirectory()
{
Stringdirectory = null;
booleandone = false;
FiledirFile = null;
// try to find the images directory
try {
// get the URL for where we loaded this class
ClasscurrClass = Class.forName("FileChooser");
URLclassURL = currClass.getResource("FileChooser.class");
URLfileURL = newURL(classURL,"images/");
directory = fileURL.getPath();
directory = URLDecoder.decode(directory, "UTF-8");
dirFile = newFile(directory);
if (dirFile.exists()) {
//setMediaPath(directory);
returndirectory;
}
} catch (Exceptionex) {
}
returndirectory;
}
}