Results 1 to 10 of 10
  1. #1
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default 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

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    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;
    	}

  3. #3
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    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.

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    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.

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    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?

  6. #6
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    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

  7. #7
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    After setting the selection mode, you can try this

    fileSelectionDialog.setSelectedFile method

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    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?

  9. #9
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Ya Thats right.But i needed to get the absolute path of the selected file.
    But now i dont need it.

    Thanks alot.:)

  10. #10
    josemoncas's Avatar
    josemoncas is offline Member
    Join Date
    Jul 2009
    Location
    Madrid (Spain)
    Posts
    11
    Rep Power
    0

    Default

    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

  1. Regarding issue in jfilechooser
    By santhosh_el in forum AWT / Swing
    Replies: 5
    Last Post: 06-01-2009, 07:59 AM
  2. jfilechooser not appearing
    By letter5 in forum New To Java
    Replies: 5
    Last Post: 02-15-2009, 05:45 PM
  3. using jfilechooser
    By jagadhguru in forum AWT / Swing
    Replies: 3
    Last Post: 10-30-2008, 01:17 PM
  4. Need JFileChooser Help
    By Wraithier in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 06:40 PM
  5. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •