Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Chooser Dialog box

 
sandhya kale
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..


How to open selected file from fileChooser dialog box.???
Is there any method to open a selected file???
 
Randall Twede
Ranch Hand
Posts: 4411
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(window);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
File file = fc.getSelectedFile();
.
.
.
catch (IOException ex)
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See: How to Integrate With the Desktop Class.
 
Randall Twede
Ranch Hand
Posts: 4411
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank You...Randall Twede..
Now i'll try for other file types..
 
sandhya kale
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Browsing URLs and Opening Files helped me...
Thank You Rob Camick.... for suggesting Desktop API..
 
sandhya kale
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got what i was looking for
here is the code that can easily opens any file from Browse button...




 
Randall Twede
Ranch Hand
Posts: 4411
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob for pointing it out and to Sandhya for demonstrating. im still stuck in java2 land
 
sandhya kale
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really...Thanks a lot Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic