Results 1 to 2 of 2
- 09-13-2011, 12:29 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
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 java.io.File; 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)); folderBrowse.setDialogTitle(title); //choose only directories and disable file filter selection folderBrowse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); folderBrowse.setAcceptAllFileFilterUsed(false); returnVal = folderBrowse.showOpenDialog(parent); } public String getDirectory(){ if(returnVal == JFileChooser.APPROVE_OPTION){ System.out.println(folderBrowse.getCurrentDirectory()); return folderBrowse.getCurrentDirectory().toString(); } else return lastLocation; } }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.ThanksJava 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){ super(title); lastFolder = System.getProperty("user.home"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new GridLayout(1,3,5,5)); init(); } void init(){ jlBrowse = new JLabel("Path:"); jlBrowse.setHorizontalAlignment(CENTER); folderTxt = new JTextField(" "); folderBtn = new JButton("Browse"); folderBtn.addActionListener(new BtnActionListener()); add(jlBrowse); add(folderTxt); add(folderBtn); pack(); setVisible(true); } class BtnActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { DirectoryChooser folderBrowse = new DirectoryChooser(GUI.this,GUI.this.lastFolder,"Choose directory..."); GUI.this.lastFolder = folderBrowse.getDirectory(); GUI.this.folderTxt.setText(GUI.this.lastFolder); } } }
- 09-13-2011, 02:06 PM #2
Similar Threads
-
Jar doesn't work
By mad72584 in forum New To JavaReplies: 35Last Post: 08-07-2011, 05:22 PM -
why this doesn't work?
By hitesh_public in forum New To JavaReplies: 5Last Post: 08-09-2010, 08:07 AM -
Why doesn't this work?
By Corder10 in forum New To JavaReplies: 1Last Post: 07-04-2009, 10:33 PM -
my loop doesn't work.. pls help???
By ashton in forum New To JavaReplies: 5Last Post: 01-16-2009, 08:24 AM -
Synchronization Doesn't seem to work
By sherinpearl in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:30 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks