• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

JFileChooser problem

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to use JFileChooser, my problem is it only reads files if it was in the project folder if it is anywhere else it does not read it ..any idea or is anything missing here thanks

 
Marshal
Posts: 71021
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use null as the Component you are showing the Open Dialogue over. You will have to find the name of the enclosing class and pass EnclosingClass.this, or EnclosingClass.this.openButton instead. That way you get the Open Dialogue centred over the Component you are using.
If you prefer, pass the identifier of a local variable, but you will probabaly have to declare that variable as final.

I presume you have gone through [URL=http://java.sun.com/javase/6/docs/api/javax/swing/JFileChooser.html]the API documentation for JFileChooser[url], and the "how to" link provided. You have obviously found the return value from the showOpenDialog() method, but the value does not mean that you have found a file; it means which button you clicked on the open dialogue. So you will have to put an if block round the bit about selected file. You might need to move the declaration elsewhere, so the File will still be in scope when you use it.

Can't see anything else wrong with your method at present. Try that and see whether it works. It is surprising that you can open files in the project folder and not elsewhere; there might be something else going wrong which you have missed.
 
Campbell Ritchie
Marshal
Posts: 71021
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggested enhancement:

  • Change the name of the Control class, and make it implement a Control interface.
  • Put all the methods you intend to call from the Listeners in the GUI class in the Control interface.
  •  
    Pepo moussa
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for your help ...it works fine now
     
    Campbell Ritchie
    Marshal
    Posts: 71021
    291
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ignore what I said about enhancements; it was intended for a different thread.

    Sorry
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic