FileChooser problem

Hello,
I have a problem with filechooser filter. I read the documentation and I can set the filter. But I have 2 problems with it.
1. Directorys are not visible.
2. User can select the "All files" option too. But I don't want to allow it.
What can I do?
Thanks!
My code is:
        class MyFilter extends javax.swing.filechooser.FileFilter {
            public boolean accept(File file) {
                String filename = file.getName();
                return filename.endsWith(".java");
            public String getDescription() {
                return "*.java";
        String filename  = "";
        File f;
        JFileChooser fileChooser = new JFileChooser(new File(filename));
        fileChooser.addChoosableFileFilter(new MyFilter());
        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.showOpenDialog(parent);

1) in accept, includeif file.isDirectory() return true;2) callfileChooser.setAcceptAllFileFilterUsed(false);

Similar Messages

  • JFileChooser Win32

    Hello,
    I have read about the startup time problem with JFileChooser. Also in my company many people say 'Swing is slow', because they know, that the filechooser is slow. And this results in the view, that those people change to SWT.
    Because I know, that Swing is fast enough for all, I want to solve the filechooser problem. As found in the bug database (6372808), I add the following line to my install program:
    regsvr32 /u %windir%\system32\zipfldr.dll
    This works fine for the zip file problem under windows.
    But I have also watched, that the filechooser needs still 1-3 seconds to be opened. I have created a JNI for OS specific functions and added file selection into it. The user can select 'native dialogs' in my application properties and than the windows standard file dialog is used.
    I hope, I can help a lot of people with this problem. If somebody is interested, send me an email.
    Best regards,
    Axel F
    Mail: $axel.fix$online.de$

    Figured it out, thanks to another post.
    I added
    player.stop():
    to my playerClose() method.
    Seems to work fine now.
    //=======================================================
    // Close player and deallocate
    //=======================================================
    public static void playerclose()
    player.stop();
    player.close();
    player.deallocate();
    }

  • Iam  gettin a problem in displaying the HTM file in filechooser please help

    Dear Sir,
    Iam working on swing in my project.In tha iam using file chooser Iam gettin problem in displaying the file of types HTM or HTML it taks if it is htm ot html . in filechooser below files of type will be there in that also its not displaying the files of types HTM or HTML. in the code i used
    filter.addExtension("HTML",True);
    but its not working please any body can help me in solving the problem . If possible send me the piece of code also il be very thankful to you. please ill be waiting for the reply.
    regards,
    surya

    this is what I did:    private void dateiNameHolen()
            // create FileChooser
            final JFileChooser pv_fileChooser = new JFileChooser();
            pv_fileChooser.addChoosableFileFilter(new HTMLFileFilter());
            pv_fileChooser.setSelectedFile(new File(iv_dateiTextField.getText()));
            // open chooser and get selected file
            if (pv_fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                File lv_datei = pv_fileChooser.getSelectedFile();
                iv_dateiTextField.setText(lv_datei.getPath());
        }It uses my class HTMLFileFilter:import java.io.File;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class HTMLFileFilter extends FileFilter {
        // accept all htm and html
        public boolean accept(File f)
            if (f.isDirectory()) {
                return true;
            String dateiname = f.getPath().toLowerCase();
              if (dateiname != null)
                if (dateiname.endsWith("htm") || dateiname.endsWith("html"))
                    return true;
                else
                    return false;
            return false;
        // description of this filter
        public String getDescription() {
            return "HTML-Dateien (*.htm, *.html)";
    }hope this helps
    koem

  • Displaying the path of the file selected by a FileChooser in a TextField

    I'm just getting started with Swing. I developed a simple dialog box that has two text fields and two buttons. The objective is to open two files using those two buttons. I used FileChooser to select the files. Now, I want to display the path of the selected files in their corresponding TextFields. I use the following code to do that.
    final FileChooser fc1 = new FileChooser();
    final FileChooser fc2 = new FileChooser();
    final TextField tf1 = new TextField();
    final TextField tf2 = new TextField();
    private void button1ActionPerformed(ActionEvent evt)
                int returnVal1 = fc1.showOpenDialog(labelPhpFile);
                if (returnVal1 == JFileChooser.APPROVE_OPTION)
                    File file1 = fc1.getSelectedFile();
                    String fileName1 = file1.getName();               
                    String filePath1 = file1.getPath();
                    tf1.setText(filePath1);
    private void button2ActionPerformed(ActionEvent evt)
                int returnVal2 = fc2.showOpenDialog(labelPhpFile);
                if (returnVal2 == JFileChooser.APPROVE_OPTION)
                    File file2 = fc2.getSelectedFile();
                    String fileName2 = file2.getName();               
                    String filePath2 = file2.getPath();
                    tf2.setText(filePath2);
    }The above code works fine only for the first file. Mean the path of the file selected using fc1 is getting displayed in the tf1 TextField. But, the file selected using fc2 is not getting displayed tf2 TextField. Please help me.
    Thank you :)

    h1. The Ubiquitous Newbie Tips
    * DON'T SHOUT!!!
    * Homework dumps will be flamed mercilessly.
    * Have a quick scan through the [Forum FAQ's|http://wikis.sun.com/display/SunForums/Forums.sun.com+FAQ].
    h5. Ask a good question
    * Don't forget to actually ask a question. No, The subject line doesn't count.
    * Ask once
        - Don't Crosspost!
        - Two people answering one question independantly is a waste of there time.
    * Don't even talk to me until you've:
        (a) [googled it|http://www.google.com.au/] and
        (b) looked it up in [Sun's Java Tutorials|http://java.sun.com/docs/books/tutorial/] and
        (c) read the relevant section of the [API Docs|http://java.sun.com/javase/6/docs/api/index-files/index-1.html] and maybe even
        (d) referred to the JLS (for "advanced" questions).
    * [Good questions|http://www.catb.org/~esr/faqs/smart-questions.html#intro] get better Answers. It's a fact. Trust me on this one.
        - Lots of regulars on these forums simply don't read badly written questions. It's just too frustrating.
          - FFS spare us the SMS and L33t speak! Pull your pants up, and get a hair cut!
        - Often you discover your own mistake whilst forming a "Good question".
        - Many of the regulars on these forums will bend over backwards to help with a "Good question",
          especially to a nuggetty problem, because they're interested in the answer.
    * Improve your chances of getting laid tonight by writing an SSCCE
        - For you normal people, That's a: Short Self-Contained Compilable Example.
        - Short is sweet: No-one wants to wade through 5000 lines to find your syntax errors!
        - Often you discover your own mistake whilst writing an SSCCE.
        - Solving your own problem yields a sense of accomplishment ;-)
    h5. Formatting Matters
    * Post your code between a pair of {code} tags
        - That is: {code} ... your code goes here ... {code}
        - This makes your code easier to read by preserving whitespace and highlighting java syntax.
        - Copy&paste your source code directly from your editor. The forum editor basically sucks.
        - The forums tabwidth is 8, as per [the java coding conventions|http://java.sun.com/docs/codeconv/].
          - Indents will go jagged if your tabwidth!=8 and you've mixed tabs and spaces.
          - Lines longer than 80 characters should be wrapped.
          - Proper indentation illustrates program logic.
    * Post your error messages between a pair of {code} tags:
        - That is: {code} ... errors here ... {code}
        - To make it easier for us to find, Mark the erroneous line(s) in your source-code. For example:
            System.out.println("Your momma!); // <<<< ERROR 1
        - Note that error messages are rendered basically useless if the code has been
          modified AT ALL since the error message was produced.
        - Here's [How to read a stacktrace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/].
    * The forum editor has a "Preview" pane. Use it.
        - If you're new around here you'll probably find the "Rich Text" view is easier to use.
        - WARNING: Swapping from "Plain Text" view to "Rich Text" scrambles the markup!
        - To see how a posted "special effect" is done, click reply then click the quote button.
    If you (the newbie) have covered these bases *you deserve, and can therefore expect, GOOD answers!*
    h1. The pledge!
    We the New To Java regulars do hereby pledge to refrain from flaming anybody, no matter how gumbyish the question, if the OP has demonstrably tried to cover these bases. The rest are fair game.

  • Problem in using JFileChooser

    Hi all
    i m using JFile chooser
    here is the code:(at class level)
    private JFileChooser fileChooser = new JFileChooser(){
    protected void fireActionPerformed(String command) {
    if (command != null && command.equals(JFileChooser.APPROVE_SELECTION)) {
    super.fireActionPerformed(command);
    when i open the form, following exception arises.
    java.io.IOException
         at sun.awt.image.GifImageDecoder.readHeader(GifImageDecoder.java:337)
         at sun.awt.image.GifImageDecoder.produceImage(GifImageDecoder.java:123)
         at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:254)
         at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
         at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
    however it is not hindering my work, but why it is...
    Pls help

    Hi all,
    I think no one in the bord knows the answer.
    or the problem is so trivial that, no one want to reply.....
    whatever may be the reason....Pls help me , if some one know about this...
    Thanx,
    Manish ...

  • Using a filechooser in an applet

    Is it possible to use a filechooser for an applet save button? If so how can you do it? So far I have only been able to use the filechooser object with JFrame and not JApplet.

    Hi,
    I'm a newbie, but I just had a similar problem and the solution was in the html code to display the applet. here's the code:
    <applet width=700 height=500 code="ImageExample.class"> </applet>
    make sure the height and width here are big enough for your imaage.
    HTH
    Paul

  • Deployment problems that I don't know how to fix or trace - Please Help

    I have made a javafx application in netbeans where I import classes from the java library (specifically ones from java.io for writing/readning files). The program works great in the IDE, reading and writing files, but when I build it and run the .jnlp, It does not write any files or do anything that requires any of the imported functions. I have been trying for a few weeks now and have had no luck. The same thing happens when I create a brand new javafx desktop application and try to make it write a simple text file.
    My questions are these:
    1. Does someone know if I'm missing something? I tried checking the box that makes it a self signed jar and pack200 compression, no luck. This program is for running locally on the user's machine.
    2. I do not no how to check for exception traces the application may have thrown. I am using Mac OSX 10.6.6, and the latest Java and Netbeans. Can someone help me with this?
    My deadline is coming up and I don't want to rewrite the whole thing using something else. I was pleased with this javafx application until now. Please Help
    here is my test .fx file
    package savetest;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    public class Main {
        // <Generated Code by netbeans (unaltered or edited for this test)>
    function writeData(source : java.net.URI) {
            var output: BufferedWriter;
            try {
                def text = "Test Data";
                def file = new File(source);
                output = new BufferedWriter(new FileWriter(file));
                output.write(text);
                output.flush();
                output.close();
            } catch (e: IOException) {
                e.printStackTrace();
    function run (): Void {
        var design = Main {};
        var fileChooser : JFileChooser = new JFileChooser;
        var goalURI : java.net.URI;
        javafx.stage.Stage {
            title: "Main"
            scene: design.getDesignScene ()
         if (fileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(null)) {
           goalURI = fileChooser.getSelectedFile().toURI();
        writeData(goalURI);
    }

    Hello user,
    You can see the traces of your application from java console which will automatically appears when your application is running on browser. In MAC OSX the java console icon appears at top right bottom (tray icon) and you can view the console from their.
    Please trace the exception what you have got so that we could find the real problem.
    Thanks,
    narayan

  • How to get the full path instead of just the file name, in �FileChooser� ?

    In the FileChooserDemo example :
    In the statement : log.append("Saving: " + file.getName() + "." + newline);
    �file.getName()� returns the �file name�.
    My question is : How to get the full path instead of just the file name,
    e.g. C:/xdirectory/ydirectory/abc.gif instead of just abc.gif
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class FileChooserDemo extends JFrame {
    static private final String newline = "\n";
    public FileChooserDemo() {
    super("FileChooserDemo");
    //Create the log first, because the action listeners
    //need to refer to it.
    final JTextArea log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    //Create the open button
    ImageIcon openIcon = new ImageIcon("images/open.gif");
    JButton openButton = new JButton("Open a File...", openIcon);
    openButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    //this is where a real application would open the file.
    log.append("Opening: " + file.getName() + "." + newline);
    } else {
    log.append("Open command cancelled by user." + newline);
    //Create the save button
    ImageIcon saveIcon = new ImageIcon("images/save.gif");
    JButton saveButton = new JButton("Save a File...", saveIcon);
    saveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showSaveDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    //this is where a real application would save the file.
    log.append("Saving: " + file.getName() + "." + newline);
    } else {
    log.append("Save command cancelled by user." + newline);
    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);
    //Explicitly set the focus sequence.
    openButton.setNextFocusableComponent(saveButton);
    saveButton.setNextFocusableComponent(openButton);
    //Add the buttons and the log to the frame
    Container contentPane = getContentPane();
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(logScrollPane, BorderLayout.CENTER);
    public static void main(String[] args) {
    JFrame frame = new FileChooserDemo();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    frame.pack();
    frame.setVisible(true);

    simply use file.getPath()
    That should do it!Thank you !
    It takes care of the problem !!

  • How to keep filename in textfield when filter is changed in a filechooser

    Hi, guys,
    I met a problem to keep filename in textfield when filter is changed in a filechooser
    for example, a JFileChooser has two filters
    *.jpg
    *.png
    Before I open JFileChooser, I used setSelectedFile(File) to set "aaa.jpg" as default name in textfield.
    when I change the filter to "*.png, ", I want to change the text in textfield to "aaa.png", Actually, I used setSelectedFile(File), If there is an existed file named "aaa.png", this file will be selected on fileView, but the name will not be shown on the textfield. Anybody has ideas?
    Thanks in advanced.

    I really thank you for your answer, but it seems you misunderstand what I said because of my poor english..
    what I said is:
    after you open the filechooser, the filechooser has a combobox to choose file filter and a field to show filename.
    what I want is:
    when you open the filechooser, initial name in field is "test. jpg"
    change filter, "test.jpg" will be change to "test.gif" in the field.
    I don't want to close the filechooser.
    That's why I used a listener to listen JFileChooser.FILE_FILTER_CHANGED_PROPERTY event.
    and I modified your codes
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.File;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.filechooser.FileFilter;
    class Testing extends JFrame {
         String defaultFile = "";
         JFileChooser fc = new JFileChooser(".");
         ExtensionFileFilter eff;
         public Testing() {
              setLocation(200, 300);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              JButton btnShow = new JButton("Show Chooser");
              JPanel p = new JPanel(new GridLayout(1, 2));
              p.add(btnShow);
              getContentPane().add(p, BorderLayout.SOUTH);
              pack();
              fc.addPropertyChangeListener(new Listener(fc));
              fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
              fc.setAcceptAllFileFilterUsed(false);
              fc.setFileFilter(new ExtensionFileFilter(".gif"));
              fc.addChoosableFileFilter(new ExtensionFileFilter(".jpg"));
              defaultFile = defaultFile.equals("") ? "Test.jpg" : "Test.gif";
              fc.setSelectedFile(new java.io.File(defaultFile));
              btnShow.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent ae) {
                        fc.showOpenDialog(Testing.this);
         public static void main(String[] args) {
              new Testing().setVisible(true);
    class ExtensionFileFilter extends FileFilter {
         String fileTypes = "";
         java.util.List extensions = new java.util.ArrayList();
         public ExtensionFileFilter() {
         public ExtensionFileFilter(String ft) {
              fileTypes = ft;
         public String getDescription() {
              return fileTypes;
         public boolean accept(File f) {
              if(f.isDirectory()) return true;
              if (f.getName().endsWith(fileTypes)) {
                   return true;
              return false;
    class Listener implements PropertyChangeListener {
        JFileChooser fChooser = null;
        Listener(JFileChooser fChooser) {
            this.fChooser = fChooser;
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {
                 FileFilter fileFilter = (FileFilter) evt.getNewValue();
                if (fileFilter != null) {
                    String fileName = null;
                    if (fileFilter.getDescription().equals(".jpg")) {
                        fileName = "Test.jpg";
                    } else if (fileFilter.getDescription().equals(".gif")){
                         fileName = "Test.gif";
                    String newFileName = "C:\\" + fileName;
                    fChooser.setSelectedFile(new File(newFileName));
    }Thanks again.
    Message was edited by:
    JadeSword

  • Help! File in use problem when using Swing app

    Hi. I got a program that is pretty much a JFileChooser that prints to standard output the path of the file that I've chosen. If I invoke the DOS command:
    java JFileChooserDemo, I will get the following as expected from the program:
    "You chose a file named: C:\MyJava\Book.xls".
    But if I invoke the following DOS command:
    java JFileChooserDemo > output.txt, the file "output.txt" contains the following:
    "GetModuleHandleA succeed
    LoadLibrary returns baaa0000
    You chose a file named: C:\MyJava\Book.xls"
    So, if I try to open the file or try to modify "output.txt", I get an error message stating that the file is in use.
    What's weird is that, I THINK if I have a program that DOES'NT use Swing or anything GUI-related and prints to standard output with the DOS "> outfile" command, I won't get this problem. So in other words, I can duplicate this problem with a sample Swing application that prints to standard output. Try it yourself.
    In my original program, it does have an event handler that closes the JFileChooser and its container and exits the system via "System.exit(0);", but for some reason, something still has "locked" the output.txt file. I even did the ctr+alt+del and can't find anything that would lock the output.txt file. What's even weirder is that if I run the application again, but output to a different file like "output2.txt", this file is not locked, but the "output.txt" file still is. output2.txt also don't have contain the
    "GetModuleHandleA succeed
    LoadLibrary returns baaa0000" message.
    The only way I know of that would "unlock" output.txt is to re-boot my computer.
    So it seems I have to modify my program somehow because it appears the OS still thinks output.txt file is still in use.
    Someone may think why would I invoke the java interpreter with the DOS "> outfile" command. Well, the reason being, I got a different version of the program that executes a query and I want it to create a delimited text file that contains the resultset of the query, so that I can then import it to Access, Excel, or whatever. But, because of this problem, I can't modify the query result without having to either run the application again and created another file name with the same result or re-boot. Of course, this would be silly.
    Any help in how I can modify my program or any GUI-program so that if I invoke the DOS "> outfile" command, the outfile won't be "locked" would be greatly appreciated. Thanks. If you want my program, you can e-mail me or run any sample program from the Swing Tutorial that prints to standard output and just add the " > outfile" to the java interpreter command.
    -Dan
    [email protected]

    Oops sorry, forgot to add the code. Here it is:
    BTW, can I edit posts? Oh well...
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class FileChooserDemo2 extends JFrame {
    static private String newline = "\n";
    public FileChooserDemo2() {
    super("FileChooserDemo2");
    //Create the log first, because the action listener
    //needs to refer to it.
    final JTextArea log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    JButton sendButton = new JButton("Attach...");
    sendButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new ImageFilter());
    fc.setFileView(new ImageFileView());
    fc.setAccessory(new ImagePreview(fc));
    int returnVal = fc.showDialog(FileChooserDemo2.this,
    "Attach");
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    /** The next 2 lines I added, the rest of the code is original taken from the Swing Tutorial **/
    System.out.println("You chose a file named: " +
                             fc.getSelectedFile().getPath());
    log.append("Attaching file: " + file.getName()
    + "." + newline);
    } else {
    log.append("Attachment cancelled by user." + newline);
    Container contentPane = getContentPane();
    contentPane.add(sendButton, BorderLayout.NORTH);
    contentPane.add(logScrollPane, BorderLayout.CENTER);
    public static void main(String[] args) {
    JFrame frame = new FileChooserDemo2();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    frame.pack();
    frame.setVisible(true);

  • HELP!: File in use problem-"GetModuleHandleA succeed LoadLibrary returns ba

    Hi. I got a program that is pretty much a JFileChooser that prints to standard output the path of the file that I've chosen. If I invoke the DOS command:
    java JFileChooserDemo, I will get the following as expected from the program:
    "You chose a file named: C:\MyJava\Book.xls".
    But if I invoke the following DOS command:
    java JFileChooserDemo > output.txt, the file "output.txt" contains the following:
    "GetModuleHandleA succeed
    LoadLibrary returns baaa0000
    You chose a file named: C:\MyJava\Book.xls"
    So, if I try to open the file or try to modify "output.txt", I get an error message stating that the file is in use.
    What's weird is that, I THINK if I have a program that DOES'NT use Swing or anything GUI-related and prints to standard output with the DOS "> outfile" command, I won't get this problem. So in other words, I can duplicate this problem with a sample Swing application that prints to standard output. Try it yourself.
    In my original program, it does have an event handler that closes the JFileChooser and its container and exits the system via "System.exit(0);", but for some reason, something still has "locked" the output.txt file. I even did the ctr+alt+del and can't find anything that would lock the output.txt file. What's even weirder is that if I run the application again, but output to a different file like "output2.txt", this file is not locked, but the "output.txt" file still is. output2.txt also don't have contain the
    "GetModuleHandleA succeed
    LoadLibrary returns baaa0000" message.
    The only way I know of that would "unlock" output.txt is to re-boot my computer.
    So it seems I have to modify my program somehow because it appears the OS still thinks output.txt file is still in use.
    Someone may think why would I invoke the java interpreter with the DOS "> outfile" command. Well, the reason being, I got a different version of the program that executes a query and I want it to create a delimited text file that contains the resultset of the query, so that I can then import it to Access, Excel, or whatever. But, because of this problem, I can't modify the query result without having to either run the application again and created another file name with the same result or re-boot. Of course, this would be silly.
    Any help in how I can modify my program or any GUI-program so that if I invoke the DOS "> outfile" command, the outfile won't be "locked" would be greatly appreciated. Thanks. If you want my program, you can e-mail me or run any sample program from the Swing Tutorial that prints to standard output and just add the " > outfile" to the java interpreter command.
    -Dan
    [email protected]

    Sure, here it is:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class FileChooserDemo2 extends JFrame {
    static private String newline = "\n";
    public FileChooserDemo2() {
    super("FileChooserDemo2");
    //Create the log first, because the action listener
    //needs to refer to it.
    final JTextArea log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    JButton sendButton = new JButton("Attach...");
    sendButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new ImageFilter());
    fc.setFileView(new ImageFileView());
    fc.setAccessory(new ImagePreview(fc));
    int returnVal = fc.showDialog(FileChooserDemo2.this,
    "Attach");
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    /** The next 2 lines I added, the rest of the code is original taken from the Swing Tutorial **/
    System.out.println("You chose a file named: " +
                             fc.getSelectedFile().getPath());
    log.append("Attaching file: " + file.getName()
    + "." + newline);
    } else {
    log.append("Attachment cancelled by user." + newline);
    Container contentPane = getContentPane();
    contentPane.add(sendButton, BorderLayout.NORTH);
    contentPane.add(logScrollPane, BorderLayout.CENTER);
    public static void main(String[] args) {
    JFrame frame = new FileChooserDemo2();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    frame.pack();
    frame.setVisible(true);

  • Problem with JFileChooser when run using Netbeans

    I just want to state that I have NO problem writing the code that brings up this component. My problem is what happens when the JFileChooser component is loaded. My environment is Windows Vista, Java SE 1.6.0. -> Netbeans IDE 5.0. The code I use is as follows:
    public RegexParser()
    JFileChooser openfile = new JFileChooser();
    openfile.showOpenDialog(RegexParser.this);
    int returnVal = openfile.showOpenDialog(RegexParser.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    //This code gets the path of the file and uses as a parameter to parse data.
    filename = openfile.getSelectedFile().getPath();
    openfile.setVisible(false);
    ParseData(filename);
    The problem is when I try to select an option from the combobox labeled "Look In:" Every directory I select which is not the root of the drive will display NO FILES even though there are files in that directory.
    Notice: I have also run the same code using the cmd.exe and it works fine. I have also looked at: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html but have found nothing that has helped me.
    Can someone explain what is the problem? Is this a known bug in Netbeans? Is there any code that can be used as a workaround?

    Yes I copied the code to my machine and run it. The example program had problems when I executed using netbeans it had the problem.
    When I executed using Command Prompt there where no problems.

  • Problem in JFileChooser

    I have got a problem here. Here I have a JFileChooser, I want to add in
    a FileFilter, and user could only see some type of file. But as long as I
    try to get the extension....It wont compile, could any one teach me how
    to get the extension of a file in JFILECHOOSER???
    ============================================
    import java.io.*;
    import java.io.File.*;
    import javax.swing.filechooser.FileFilter;
    public class MainFrame extends JFrame implements ActionListener {
    public MainFrame() {.............}
    private boolean chooseFile()
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
    FileChoser filtering = new FileChoser("txt");
    fileChooser.setFileFilter(filtering);
    fileChooser.addChoosableFileFilter(filtering);
    int selection = fileChooser.showOpenDialog( this );
    if ( selection == JFileChooser.CANCEL_OPTION )
    return false;
    if (selection == -1) {
    return false;
    File tmpfile = fileChooser.getSelectedFile();
    filename = tmpfile.toString();
    setTitle( "Smiggin Holes 2010 - " + filename );
    return true;
    static class FileChoser extends javax.swing.filechooser.FileFilter
    String extension;
    String description;
    public FileChoser(String extension, String description){
    this.extension = extension;
    this.extension = description;
    // Accept all directories and all gif, jpg, or tiff files.
    public boolean accept(File f)
    if (f != null)
    if (f.isDirectory())
    return true;
    String ext = getExtension(f); //<<<<<<<<<<<THE PROBLEM IS HERE WHEN I TRY TO GET THE EXTENSION OF THE FILE. IT CANT COMPILE
    System.out.println("aasdfasdf"+ext);
    if ((ext!=null)&&(ext.equals(extension)))
    return true;
    return false;
    // The description of this filter
    public String getDescription() {
    return "Just Text Files";
    ======================================
    Could you tell me what can I do? To get the extension of the file selected by the user?
    And What do I have to change?
    Regards,

    Hi !
    in the FileChoser class, the public boolean accept(File f) method can be written like this :
    public boolean accept(File f) {
    if (f.isDirectory || f.getName().endsWith(".txt")) { // you can add other extensions
    return true;
    else {
    return false;

  • Renaming a file in a filechooser

    renaming a file in a filechooser -
    Single click or right click on a folder of a JFileChooser allows you to edit the new file name ina textfield. When the focus is lost from the text field the folder name is changed to the older one. But after editing the file name in the textfield and press 'Enter' the folder or file is updated with the new name. Can anybody tell me if there is a way to make it work on loosing focus ???
    Thanks in advance

    This sounds like a bug from the install. I use Mavericks and do not have this problem. I suggest reinstalling: http://support.apple.com/kb/PH13871

  • File chooser internationalization problem

    Hello All!
    I'm trying to have Armenian texts on Swing components.
            Locale hay = new Locale("hy", "HY");
            Locale.setDefault(hay);
            UIManager.put("FileChooser.openDialogTitleText", "&#1330;&#1377;&#1409;&#1381;&#1388;");
            UIManager.put("FileChooser.saveDialogTitleText", "&#1354;&#1377;&#1392;&#1381;&#1388;");
            UIManager.put("FileChooser.lookInLabelText", "&#1363;&#1398;&#1407;&#1408;&#1381;&#1388;");
            UIManager.put("FileChooser.saveInLabelText", "&#1354;&#1377;&#1392;&#1381;&#1388;");
            UIManager.put("FileChooser.upFolderToolTipText", "&#1348;&#1387; &#1348;&#1377;&#1391;&#1377;&#1408;&#1380;&#1377;&#1391; &#1358;&#1381;&#1408;&#1415;");
            UIManager.put("FileChooser.homeFolderToolTipText", "&#1329;&#1399;&#1389;&#1377;&#1407;&#1377;&#1383;&#1391;&#1408;&#1377;&#1398;");
            UIManager.put("FileChooser.newFolderToolTipText", "&#1357;&#1407;&#1381;&#1394;&#1390;&#1381;&#1388; &#1350;&#1400;&#1408; &#1354;&#1377;&#1398;&#1377;&#1391;");
            UIManager.put("FileChooser.listViewButtonToolTipText", "&#1361;&#1400;&#1410;&#1409;&#1377;&#1391;");
            UIManager.put("FileChooser.detailsViewButtonToolTipText", "&#1348;&#1377;&#1398;&#1408;&#1377;&#1396;&#1377;&#1405;&#1398;&#1381;&#1408;");
            UIManager.put("FileChooser.fileNameHeaderText", "&#1329;&#1398;&#1400;&#1410;&#1398;");
            UIManager.put("FileChooser.fileSizeHeaderText", "&#1342;&#1377;&#1406;&#1377;&#1388;");
            UIManager.put("FileChooser.fileTypeHeaderText", "&#1359;&#1381;&#1405;&#1377;&#1391;");
            UIManager.put("FileChooser.fileDateHeaderText", "&#1363;&#1400;&#1411;&#1400;&#1389;&#1396;&#1377;&#1398; &#1329;&#1396;&#1405;&#1377;&#1385;&#1387;&#1406;");
            UIManager.put("FileChooser.fileAttrHeaderText", "&#1344;&#1377;&#1407;&#1391;&#1377;&#1398;&#1387;&#1399;&#1398;&#1381;&#1408;");
            UIManager.put("FileChooser.fileNameLabelText", "&#1350;&#1399;&#1400;&#1409;&#1387; &#1329;&#1398;&#1400;&#1410;&#1398;");
            UIManager.put("FileChooser.filesOfTypeLabelText", "&#1350;&#1399;&#1400;&#1409;&#1398;&#1381;&#1408;&#1387; &#1359;&#1381;&#1405;&#1377;&#1391;&#1398;&#1381;&#1408;");
            UIManager.put("FileChooser.openButtonText", "&#1330;&#1377;&#1409;&#1381;&#1388;");
            UIManager.put("FileChooser.openButtonToolTipText", "&#1330;&#1377;&#1409;&#1381;&#1388; &#1336;&#1398;&#1407;&#1408;&#1406;&#1377;&#1390; &#1350;&#1399;&#1400;&#1409;&#1384;");
            UIManager.put("FileChooser.saveButtonText", "&#1354;&#1377;&#1392;&#1381;&#1388;");
            UIManager.put("FileChooser.saveButtonToolTipText", "&#1354;&#1377;&#1392;&#1381;&#1388; &#1336;&#1398;&#1407;&#1408;&#1406;&#1377;&#1390; &#1350;&#1399;&#1400;&#1409;&#1384;");
            UIManager.put("FileChooser.directoryOpenButtonText", "&#1354;&#1377;&#1392;&#1381;&#1388;");
            UIManager.put("FileChooser.directoryOpenButtonToolTipText", "&#1354;&#1377;&#1392;&#1381;&#1388; &#1336;&#1398;&#1407;&#1408;&#1406;&#1377;&#1390; &#1350;&#1399;&#1400;&#1409;&#1384;");
            UIManager.put("FileChooser.cancelButtonText", "&#1332;&#1377;&#1380;&#1377;&#1408;&#1381;&#1409;&#1398;&#1381;&#1388;");
            UIManager.put("FileChooser.cancelButtonToolTipText", "&#1332;&#1377;&#1380;&#1377;&#1408;&#1381;&#1409;&#1398;&#1381;&#1388; &#1350;&#1399;&#1400;&#1409; &#1336;&#1398;&#1407;&#1408;&#1396;&#1377;&#1398; &#1333;&#1408;&#1391;&#1389;&#1400;&#1405;&#1400;&#1410;&#1385;&#1397;&#1377;&#1398; &#1354;&#1377;&#1407;&#1400;&#1410;&#1392;&#1377;&#1398;&#1384;");
            UIManager.put("FileChooser.newFolderErrorText", "&#1350;&#1400;&#1408; &#1354;&#1377;&#1392;&#1400;&#1409;&#1387; &#1357;&#1407;&#1381;&#1394;&#1390;&#1396;&#1377;&#1398; &#1357;&#1389;&#1377;&#1388;");
            UIManager.put("FileChooser.acceptAllFileFilterText", "&#1330;&#1400;&#1388;&#1400;&#1408; &#1350;&#1399;&#1400;&#1409;&#1398;&#1381;&#1408;");Only DialogTitleText is working. And if I'm trying to internationalize other Swing components: buttons and so on, the only problem with JFrame title is.
    I'm working on WindowsXP, and java version is 1.6.0_17.
    Thanks in advance!
    Astghik

    The quick answer:
    You can't use JFileChooser unless you sign the applet.

Maybe you are looking for

  • HP C4780 will not connect to wireless router

    I have the HP C4780 printer and a MacBook Pro with Mountain Lion OS X 10.8.5. I have been able to install the printer software to my computer with the USB cable, and I am able to print as long as it is hooked up with the USB cable. I want to set it u

  • DMVPN Hub and Spoke behind NAT device

    Hi All, I have seen many documents stating about DMVPN Hub behind NAT or DMVPN Spoke behind NAT. But My case i involve in both situation. 1) HUB have a Load Balancer (2 WAN Link) ISP A & B 2) Spoke have Load Balancer (2 WAN Link) ISP A & B Now the re

  • Instance terminated by CKPT, pid = 944,What can I do? What is "pid=944"

    Tue Mar 12 22:11:36 2013 ********************* ATTENTION: ******************** The controlfile header block returned by the OS has a sequence number that is too old. The controlfile might be corrupted. PLEASE DO NOT ATTEMPT TO START UP THE INSTANCE w

  • Very slow laptop - Pavilion g6-2245ea

    Hi, My Pavilion g6-2245ea laptop has started to run extremely slowly, even during the most simple of tasks. I was hoping to get some advice on an upgrade.  It has a 1.7ghz  processor with 8gb of RAM and is running windows 8. Which of these should be

  • ISE NICs

    Do ISE 3355 and 3315 support etherchannel configuration? And I see that they have four gigabit ethernet NICs, but two of the NICs need add-on cards? Does that mean that only two (out of the available four NICs) are ready to be used without installing