Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: JFileChooser doesn't work as it should!

  1. #1
    fioan89 is offline Member
    Join Date
    Sep 2011
    Rep Power

    Default JFileChooser doesn't work as it should!

    Hi.I stumbled over a problem a few days ago and can't see how to resolve it.
    I want to use JFileChooser to select folder(not files).So i done the code and everything work but if i select the folder "/home/john/Documents/" JFileChooser return me "/home/john/" if i select "/home/john/Documents/books/" ,JFileChooser return me "/home/john/Documents".The same thing happens for every path i choose:it returns me the upper folder of what i select.
    And the funny thing is that if try this codeSelect a directory with a JFileChooser - Real's Java How-to everything works ok but not my code although the difference is not that big.Here is my code :

    Java Code:
    import java.awt.Component;
    import javax.swing.*;
     * @author faur
    public class DirectoryChooser {
    	private static final long serialVersionUID = 1L;
    	JFileChooser folderBrowse;
    	private int returnVal;
    	private String lastLocation;
    	Component parent;
    	DirectoryChooser(Component parent,String lastFolder,String title){
    		lastLocation = lastFolder;
    		this.parent = parent;
    		folderBrowse = new JFileChooser(new File(lastFolder));
    		//choose only directories and disable file filter selection
    		returnVal = folderBrowse.showOpenDialog(parent);
    	public String getDirectory(){
    		if(returnVal == JFileChooser.APPROVE_OPTION){
    			return folderBrowse.getCurrentDirectory().toString();
    		else return lastLocation; 
    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.GridLayout;
    public class GUI extends JFrame implements SwingConstants{
    	private static final long serialVersionUID = 1L;
    	JButton folderBtn;
    	JTextField folderTxt;
    	JLabel jlBrowse;
    	String lastFolder;
    	GUI(String title){
    		lastFolder = System.getProperty("user.home");
    		setLayout(new GridLayout(1,3,5,5));
    	void init(){
    		jlBrowse = new JLabel("Path:"); 
    		folderTxt = new JTextField("                                              ");
    		folderBtn = new JButton("Browse");
    		folderBtn.addActionListener(new BtnActionListener());
    		add(jlBrowse); add(folderTxt); add(folderBtn);
    	class BtnActionListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			DirectoryChooser folderBrowse = new 
    										DirectoryChooser(GUI.this,GUI.this.lastFolder,"Choose directory...");
    				GUI.this.lastFolder = folderBrowse.getDirectory();
    Please if you have any ideas about what is wrong don't hesitate.I turned the internet upside down but didn't find any resolve.Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: JFileChooser doesn't work as it should!

    What did you think getCurrentDirectory() does? and why does JFileChooser have a method getSelectedFile()?

    Fubarable likes this.

Similar Threads

  1. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  2. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  3. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  4. my loop doesn't work.. pls help???
    By ashton in forum New To Java
    Replies: 5
    Last Post: 01-16-2009, 08:24 AM
  5. Synchronization Doesn't seem to work
    By sherinpearl in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:30 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