Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Open Dialog JFileChooser  RSS feed

 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay again I have this same project and Im trying to get the Open and save dialog boxes to show when the user clicks the appropriate menu item.

I have seen the example on Sun's website, but the menu action listener is another class then the one that implements the gui.

I get the error
JFileChooser cannot be applied to UNTFgui.
on this line of code
int returnVal = fc.showOpenDialog(UNTFgui.this);


 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried passing in "null" as the parameter to showOpenDialog, like the javadocs suggest?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only use an instance of Component or a subclass, and your UNTFgui class does not meet that requirement.

So either you have to create a component (like a applet, frame or panel), or just use null as Ulf said.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I use null will the file I open still open up inside the appropriate textArea?
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also how in the world do I add a filter so it will only open up a certain file extension. I have read through the Java Tutorial and it does not help, I have also looked at the API.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can also perform slightly other checks; use "name.regionMatches(true, name.length - 4, ".txt", 0, 4)" to check while ignoring the case.

There is one draw back: when saving, JFileChooser allows any typed in value. If that value does not include the correct extension, it is not added automatically but you have to do it yourself.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the "public void addChoosableFileFilter (FileFilter filter)" method? It would seem to do what you're asking.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright! I got it to work, even on a separate file!. Now I just have to have it actually read and write the file. Yah!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!