File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File Chooser Dialog box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File Chooser Dialog box" Watch "File Chooser Dialog box" New topic
Author

File Chooser Dialog box

sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
Hello..


How to open selected file from fileChooser dialog box.???
Is there any method to open a selected file???
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(window);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
File file = fc.getSelectedFile();
.
.
.
catch (IOException ex)


SCJP
Visit my download page
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> How to open selected file from fileChooser dialog box.???

open how - if the selected file is abc.doc - open in word? or abc.xls - open in excel etc.
or do you just want the file contents read into java?
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
Michael Dunn wrote:> How to open selected file from fileChooser dialog box.???

open how - if the selected file is abc.doc - open in word? or abc.xls - open in excel etc.
or do you just want the file contents read into java?


No i don't want to read file contents..

I am developing one Application,in which a user can open any file of his interest by clicking on 'Browse' button...
I have designed code up to Browse and i got Browse window too after clicking on Browse button.
but it did not open a required file..If any one know please do inform.
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
Yaa I tried the same code as...


public class FileChooser {
public FileChooser(){
//JFileChooser file = null;
final JFileChooser fc = new JFileChooser();
try{
int openChoice = fc.showOpenDialog(window);
StringBuffer tracker = null;
if (openChoice == JFileChooser.APPROVE_OPTION){
//Put open file code in here
File openFile = fc.getSelectedFile();

//tracker.append("\nThe file selected is " + openFile.getName());
tracker.append("\nThe file's path is " + openFile.getPath());
tracker.append("hi");
System.out.println(tracker.toString());
JOptionPane.showMessageDialog(new javax.swing.JFrame(), tracker.toString());
}
}catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(new JFrame(), e.getMessage());
}
}
}
Firstly i give 'null' as a parameter to showOpenDialog() method but it did not work,
got 1 error when tried this line as..

int openChoice = fc.showOpenDialog(window);
what does 'window' parameter mean??
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
sandhya kale wrote:
Michael Dunn wrote:
but it did not open a required file..If any one know please do inform.


Who knows? Without posting your own code and exception/error stack trace, it's impossible to see where you are going wrong with.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
See: How to Integrate With the Desktop Class.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

the window parameter is a reference to the JFrame that created the FileChooser

ill post some longer code for you. it opens image files but it will be similar with other file types.
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24

Thank You...Randall Twede..
Now i'll try for other file types..
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
This Browsing URLs and Opening Files helped me...
Thank You Rob Camick.... for suggesting Desktop API..
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
Finally got what i was looking for
here is the code that can easily opens any file from Browse button...




Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

thanks Rob for pointing it out and to Sandhya for demonstrating. im still stuck in java2 land
sandhya kale
Greenhorn

Joined: Dec 07, 2011
Posts: 24
Really...Thanks a lot Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Chooser Dialog box