Open dialoug box by clicking on a button
I want to open the browse dialoug box by clicking on a button.
So that the user can easily browse a particular path that he need.
And by clicking on "ok" button on the dialoug box, i should get that particular path that he gone through the browse dialoug box into a textfield.
So that from the text field, i can take that wholepath into my program and use that path.
# 1
add action listener to your button and use JFileChooser.
using JFileChooser:
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
Message was edited by:
Yannix
# 2
//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);
In the above code
Please tell me what does the "aComponent" argument represent.
# 3
> //Create a file chooser
> final JFileChooser fc = new JFileChooser();
> ...
> //In response to a button click:
> int returnVal = fc.showOpenDialog(aComponent);
>
>
> In the above code
> Please tell me what does the "aComponent" argument
> represent.
button, textfield, etc.... those are components
read this:
http://java.sun.com/docs/books/tutorial/uiswing/components/componentlist.html
# 4
Here i need to take a particular path of the directory.
i used this code
public void actionPerformed(ActionEvent e){
if(e.getSource()==browse){
JDirectoryChooser chooser = new JDirectoryChooser();
int choice = chooser.showOpenDialog(c);
if(choice == JDirectoryChooser.CANCEL_OPTION) {
System.out.println("User Canceled");
}
else {
System.out.println("Dialog Selection: " + chooser.getSelectedFile).getAbsolutePath());
}
}
}
but i am getting some error:
"JDirectoryChooser cannot be resolved or is not a type"
it is asking to create a JDirectoryChooser class
# 5
> but i am getting some error:
> "JDirectoryChooser cannot be resolved or is not a
> type"
>
> it is asking to create a JDirectoryChooser class
did you even try to read the whole tutorial?
it is not JDirectoryChooser but JFileChooser.
use:
System.out.println(fileChooser.getSelectedFile().getPath());
# 6
try this:
JButton button = new JButton("open");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int retval = chooser.showOpenDialog(null);
if(retval == JFileChooser.APPROVE_OPTION)
System.out.println(chooser.getSelectedFile().getPath());
}
});
# 7
The code which u send was working fine and its ok.
But what i have to do is to restrict the user from selecting up to a directory not upto a file.
I mean the code which u send works fine and it is used to select a particular file in a particular path.
But what i need is to select some directory which is present in some other directory.
That path i need to take.
For example:
i am having a directory called data.
it is in d:\giri\test\data.
So i have to provide the user a GUI so that he should able to select that whole path from the open dialoug box and click on ok so that i can take that path( only this path d:\giri\test\data) and continue my program.
ofcourse the data folder contains some more directories and files also.
But the code which u sent is asking me to go furthur to open upto i select a single file and it is displaying all the whole path up to that file.
# 8
Try reading the docs that others have already supplied to you:
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
Search for "DIRECTORIES_ONLY".