• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

File Chooser Dialog box

 
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???
 
Ranch Hand
Posts: 4716
9
Scala 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)
 
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??
 
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.

 
Rancher
Posts: 3204
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See: How to Integrate With the Desktop Class.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala 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: 4716
9
Scala 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.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic