Results 1 to 10 of 10
Thread: JfileChooser Help
- 07-31-2009, 06:54 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 29
- Rep Power
- 0
JfileChooser Help
Hi All,
I am using JfileChooser.I have OK button for approval.I want to make a check that when i click on OK button it checks for some condition and if that condition is true then only file chooser window should close.But as Approve option is on OK button everytime i click OK file chooser window gets close.
Could you please tell me how to put that check.
public File createFileSelectionBox(){
fileSelectionDialog = new JFileChooser();
fileSelectionDialog.setDialogTitle ("FileSelectionBox");
// Choose only files, not directories
fileSelectionDialog.setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES);
int result = fileSelectionDialog.showDialog(null,"OK");
if (result == JFileChooser.CANCEL_OPTION) {
return null;
} else if (result == JFileChooser.APPROVE_OPTION) {
file = fileSelectionDialog.getSelectedFile ();
}
return file;
}
Thanks in Advance,
Simmi
- 07-31-2009, 08:18 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
This could be one of the ways, but there might be better ways too
Java Code:public File createFileSelectionBox(){ JFileChooser fileSelectionDialog = new JFileChooser(); fileSelectionDialog.setDialogTitle ("FileSelectionBox"); File file=null; // Choose only files, not directories fileSelectionDialog.setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES); int result = fileSelectionDialog.showDialog(null,"OK"); if (result == JFileChooser.CANCEL_OPTION) { return null; } else if (result == JFileChooser.APPROVE_OPTION) { file = fileSelectionDialog.getSelectedFile (); //if the selected file is not gif, we once again call the method if(!file.getName().endsWith("gif")){ createFileSelectionBox(); } } return file; }
- 07-31-2009, 08:25 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 29
- Rep Power
- 0
Thanks dswastik.
I have one more issue. I want whenever I open jfilechooser the current directory should get displayed in fileName.
I tried with
fileSelectionDialog = new JFileChooser(new File("."));
But it didnot work.In the begining it is still blank.
What needs to be done.
Thanks Simmi.
- 07-31-2009, 08:31 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
What you are doing should work, and even if use the no-arg constructor it should not be blank, it should display the user's default directory, which is typically My Documents in windows.
- 07-31-2009, 08:34 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Well, probably there is a small confusion. The directory can be displayed in the look in drop down. But how can you display a directory name in the File Name text box, I guess it can always be a file name?
- 07-31-2009, 10:11 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 29
- Rep Power
- 0
Actually I have used setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES), if I choose setFileSelectionMode ( JFileChooser.DIRECTORIES_ONLY) then it display current directory in the text box.
So is there any way to display current directory in file Text when mode is JFileChooser.FILES_AND_DIRECTORIES.
Thanks,
Simmi
- 07-31-2009, 10:27 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
After setting the selection mode, you can try this
fileSelectionDialog.setSelectedFile method
- 07-31-2009, 10:31 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
But the point is that text field is used to display the selected file. Any where if you click file open (ms-word,excel) that displays the selected file name with the complete path. Why do you have such a requirement?
- 08-01-2009, 11:19 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 29
- Rep Power
- 0
Ya Thats right.But i needed to get the absolute path of the selected file.
But now i dont need it.
Thanks alot.:)
- 08-04-2009, 02:24 AM #10
From a "File" object you can get the absolute path among other things.
The web that offers Java utilities (classes) which make programming Swing applications an easier task: JMC Java utilities
Similar Threads
-
Regarding issue in jfilechooser
By santhosh_el in forum AWT / SwingReplies: 5Last Post: 06-01-2009, 06:59 AM -
jfilechooser not appearing
By letter5 in forum New To JavaReplies: 5Last Post: 02-15-2009, 04:45 PM -
using jfilechooser
By jagadhguru in forum AWT / SwingReplies: 3Last Post: 10-30-2008, 12:17 PM -
Need JFileChooser Help
By Wraithier in forum New To JavaReplies: 4Last Post: 06-18-2008, 05:40 PM -
how to use JFileChooser
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks