Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use them, and add a filter. For Example:</p> <pre><code> JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the Java 2 SDK, Standard Edition. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.setDescription("JPG &amp; GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); } </code></pre> <p>This will only show JPG and GIF files. Example stolen from <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html" rel="nofollow noreferrer">here</a></p> <p>Edit: Just so you know ExampleFileFilter implements the abstact class <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/filechooser/FileFilter.html" rel="nofollow noreferrer">FileFilter</a></p> <p>Edit: Since you know the name of the file, you could just have a button that says open and use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html" rel="nofollow noreferrer">Runtime</a>.getRuntime.exec('the file to be opened.doc") and that should open it in the appropriate application. </p> <p>For saving you will still want to prompt them to find out where they want to save it so you would still need the JFileChooser. I would still use a filter, and determine what the file extension will be dynamically if necessary and then do:</p> <pre><code> JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the Java 2 SDK, Standard Edition. String selectedFile = "The suggested save name."; chooser.setSelectedFile(selectedFile); ExampleFileFilter filter = new ExampleFileFilter(); String extension = "Do something to find your extension"; filter.addExtension(extension); filter.setDescription("JPG &amp; GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); //then write your code to write to disk } </code></pre> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload