This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser problem  RSS feed

 
Pepo moussa
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

 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • 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
Sheriff
Posts: 53773
127
  • 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
    Sheriff
    Posts: 53773
    127
    • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!