JFileChooser Questions

Hi,
I have 2 questions concerning the use of JFileChooser:
1. When you use "Details" option to see the details of the files; the "Modified" column seems to show the time in "United States" format even if the local regional setting is something else. Is this the default behavior of the JFileChooser or is there something that I can do to change that?
2. It seems that you cannot just click on the column headers such as "Modified" to have the files sorted by that column like real Window's file chooser... Is that also the default behavior or is there something that I can do to change that?
Thanks....

you can read this
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
to know all the subtilities fo the JFileChooser

Similar Messages

  • JFileChooser question

    Hey Guys,
    My First post on the sun forums, so a big hello to you guys. I have a question in relation to best practice in querying something in one user interface, of another user interface. Suppose i have a class that consists on a TextField,JButton, and a JButton which opens a JFileChooser; i would like to return the value of the JTextField from the second GUI class to a calling gui class (gui1), once the OK button has been pressed. The second gui class consists of the following.
    package UserInterface;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    Class that allows for a local file to be chosen
    @author pthug
    public class ImageChooser extends JFrame {
    private JButton okButton;
    private JButton chooseFileButton;
    private JTextField locationTextField;
    private JPanel mainPanel;
    private JPanel locationPanel;
    private JFileChooser fc;
    private String address;
    //default constructor
    public ImageChooser() {
    super("Image Chooser");
    //set default size
    setSize(new Dimension(450,250));
    setResizable(false);
    //create components
    initComponents();
    //set layout - top to bottom
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    locationPanel.setLayout(new FlowLayout());
    //add components to frame
    addComponents();
    //pak components
    pack();
    //centre frame on screen
    setLocationRelativeTo(null);
    //close frame as default operation
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    //add action listeners to components
    createActionListeners();
    //show frame
    setVisible(true);
    //*adds all components*
    *private void addComponents() {*
    *locationPanel.add(locationTextField);*
    *locationPanel.add(chooseFileButton);*
    *mainPanel.add(locationPanel);*
    *mainPanel.add(okButton);*
    *this.add(mainPanel);*
    *//*create the components for interface
    private void initComponents() {
    okButton = new JButton("OK");
    chooseFileButton = new JButton("...");
    locationTextField = new JTextField(50);
    mainPanel = new JPanel();
    locationPanel = new JPanel();
    fc = new JFileChooser();
    address = new String("");
    //create action listeners for components
    private void createActionListeners() {
    //open File Choose Dialog
    chooseFileButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == chooseFileButton) {
    int returnVal = fc.showOpenDialog(ImageChooser.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    locationTextField.setText(file.getPath());
    } else {
    System.out.println("Open command cancelled by user." + "\n");
    //ok button action listener
    okButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    address = locationTextField.getText();
    //some more methods that determine if a web address has been typed in the JTextField or whether a
    //local file has been chosen using the JFileChooser which has not been written yet
    //@param args
    public static void main(String[] args) {
    ImageChooser co = new ImageChooser();
    }and a call the class from a the first gui class as such
    GUI 1
    addImageItem.addActionListener(new ActionListener() {
                 @Override
                      public void actionPerformed(ActionEvent arg0) {
                           imageChooser = new ImageChooser();
    ... remaining source of class remove as its not necessary to the questionhow can i query when the ok button has been pressed on the second gui class, so that the first gui knows when to take the value from the JTextField. Would i just put a method in the second gui which is a boolean that holds true or false whether its ok to take the value from the JTextField and query it like such...
    //                       while(imageChooser.isStillOpen()) {
    //                       }Hope that makes sense.
    Regards,
    KevJ
    Edited by: KevJ on 10-Nov-2009 17:05

    Thank you for your reply bharath.bravo, that has solved my question. Which i did as such, i provided a method in the second GUI class to enable me to add an ActionListener to the OK button. As such
          * this method allows other classes to add their action listeners to
          * the OK Button
          * @param e Action Listener from another class
         public void addActionToOKButton(ActionListener e) {
              okButton.addActionListener(e);
         }     I then added a accessor method, to get the value from the textfield; which i've implemented it as such:
    imageChooser.addActionToOKButton(new ActionListener() {
                                  @Override
                                  public void actionPerformed(ActionEvent arg0) {
                                                                                                                    //use imageChooser accessor method to get the value from the text field
                                       imageLocation = imageChooser.getLocationTextField();
                                                                                                                    //DEBUG statement
                                       System.out.println("gui one recieved: " + imageLocation);
                                                                                                                    //get rid of imageChooser window
                                       imageChooser.dispose();
                                  }For the sake of clarity, in your first suggestion. You mentioned that i could implement the WindowListener Interface in the first gui, and add it as a Listener to the second gui. Would i do that like this, and what would be a prefferable way of doing this out of the two methods you suggested.
    public class GUI extends JFrame implements WindowListener
         @Override
         public void windowClosed(WindowEvent arg0) {
              // TODO Auto-generated method stub
              //get value of jTextField using its public accessor method          
    add window listener to second gui, from the first GUI
    imageChooser.addWindowListener(this);Regards,
    KevinJ
    Edited by: KevJ on 11-Nov-2009 14:38

  • JFileChooser Question .... wo hoo!

    Hey there,
    I have a question regarding the JFileChooser class. I have managed to instantiate a file chooser and specify that it display directories only, I have also worked out how to get information when the "Attach" button (my custom button) or the "cancel" button is pressed. When the "Attach" button is pressed I return a file object that selects the user selected directory. I then print the directory to the standard output. Unfortunately the entire selection is not being printed, only the top level directory relevant to the selection is being printed. If you are familiar with JFileChooser then you will probably know what it looks like when it is instantiated. To refresh a couple of areas of significance to my program are the "Look in:" section and the "File Name:" section.
    I need my program to print the inforamtion in the "File Name:" section not the "Look in:" section as it is doing now. My code is as follows:
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    public class TestChooser extends JFrame
      public TestChooser()
        final JFileChooser fC = new JFileChooser(); //Final modifier needed
                                    //because this object is used in an  inner class.
        fC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        JButton button1 = new JButton();
        button1.setText("Activate File Chooser");
        button1.setBounds(50, 50, 50, 50);
        button1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int returnVal = fC.showDialog(TestChooser.this, "Attach");
            if(returnVal == JFileChooser.APPROVE_OPTION)
              File curDirectY = fC.getCurrentDirectory();
              /* The "Look In:" Label information is being returned,
                 instead of the complete directory & folder details
                 contained in "File Name:". Note that File Name has the
                 keyMnemonic of underscore N.
              String str = curDirectY.toString();
              System.out.println("Of course the selected Directory is "+str);
        this.getContentPane().add(button1);
      public static void main(String args[])
        JFrame frame = new TestChooser();
        frame.setBounds(100, 100, 300, 200);
        frame.setVisible(true);
    }I have noticed that the "File Name:" section has a Key Mnemonic of N so maybe that can be used in the actionListener, I'm just not sure how. If anybody can help me with retrieving the information from the "File Name:" section as opposed to the "Look in:" section it will be greatly appreciated.
    Regards
    Davo

    <snip>
    File curDirectY = fC.getCurrentDirectory();The above method is the wrong method to call. It returns the directory that the JFileChooser itself is pointing at. It knows nothing about what file or directory may have been selected. Use the getSelectedFile() method instead.
    Jim S.

  • JFileChooser question regarding the order of the files in a folder

    I was hoping someone had a way around this issue I am having with the JFileChooser. I have a folder that has filenames in it like text1.txt, text2.txt, and so forth all the way up to text300.txt. When the filechooser shows these files it shows the order of them as:
    text1.txt
    text10.txt
    text11.txt
    text2.txt
    text20.txt
    text21.txt
    and so forth.
    How come it doesn't list them like they would be in a folder window like:
    text1.txt
    text2.txt
    text3.txt
    text150.txt
    text151.txt
    text299.txt
    text300.txt
    Actually I know why JFileChooser does it this way, it is because it sorts them as Strings without taking into account the number at the end of a similar string.
    Anyway, I was wondering if anyone had some ideas of how I could alter the JFileChooser to list the contents of a folder like this in the "right" order. Hopefully that made sense. :) Thanks.

    Thank u for ur reply.... actually the thing is mine's one of the pre-ordered iphones n it's reaching me by 2moro i think. so are the pre-ordered iphone's already activated? or m i in trouble if it's not activated out of the box (if i have to activate it myself). ?

  • Save in JFileChooser question

    Hello everyone ...
    I have a problem that has many answers on this site, but i cannot understand them (damn newbie me ! :)) ).
    I use a JFileChooser to save a file and I want the extension of the file to be automatically appended to the filename. I use a filter for viewing files but i don't know how can I use a filter to save the files.
    Thanks in advance,
    Dan

    class MyExtensionFilter extends FileFilter {
    String myExtension = "";
    public MyExtensionFilter(String myExtension) {
    this.myExtension = myExtension;
    public boolean accept(File file) {
    return file.getName().endWith(myExtension);
    public string getDescription() {
    return myExtension;
    File chosenFile = myFileChooser.getSelectedFile();
    chosenFile = newFile(chosenFile.getAbsolutePath() + myFileFilter.getDescription();

  • How can i retrieve a computer name with a java code

    hello friends,
    please, can i get help on how i can rtrieve the name of a computer(any at all) using java - a code will be appreciated.
    i also wish to ask if you know how i can use the JFileChooser class, such that when i click on a file to select it and then click on the open button, the file will open / show in a TextArea which i have already created.
    Moreso, how can i use the JFileChooser to save something i've written in a TextArea, as file to a system.
    thanks in advance for your useful advice. quite grateful.

    'result' is a String I think.
    As for your JFileChooser question...
    Create your take the file returned from JFileChooser, open it in a BufferedReader (I'm assuming it's text as you want to display it in a JTextArea) then read the lines out.
    JFileChooser fc = new JFileChooser();
    fc.showOpenDialog(null);
    File file = fc.getSelectedFile();
    if (file != null) {
    try {
    BufferedReader in = new BufferedReader(new FileReader(file));
    String inputLine;
    String total = "";
    while ((inputLine = in.readLine()) != null)
    total+=inputLine;
    in.close();
    textarea.setText(total); // textarea = your JTextArea
    } catch (Exception e) {
    System.err.println("Error: "+e);
    }

  • JFrame window close buton

    How do I reference this (the "X" in the top right) from my frame?

    "That was no help at all"
    A response to a reply from another question this user
    posted:
    http://forum.java.sun.com/thread.jsp?thread=490792
    I suspect this individual will find it difficult to
    get advice if they don't lose the attitude!I read this persons response and decided to ignore it at the time - Whereas I would concede that my reply could have been construed as impolite (it wasn't intended that way at all), I find it very odd that people who present questions or problems here at these forums should be ungrateful for any answer they get, even if they're completely off track. As someone who frequently gets it wrong, I also manage to get it right (if you'll pardon me for saying so) with a reasonable % success rate, based on a limited amount of given information - ref: JFileChooser question earlier today here in Swing, I got real buzz out of the guess that I submitted when the OP of that thread told me that it worked. I think all of us frequently have to interpret a question and inevitably at times, those interpretations will be wrong. I would also concede that finding solutions to a variety of problems we encounter can be highly frustrating and, speaking for myself, I find it very easy to let indescretions as displayed by the OP wash over me IF they later apologise for it.
    The issue of a potential 'Forum Black-List' has been discussed before, though as I recall the idea falls apart given that it is very easy to get a new user log-in alias.

  • Newbie Question: Getting the file name from a JFileChooser

    Hi,
    I have a simple problem.
    I'd like to get the filename that a user types into a JFileChooser Save dialog before they save a file.
    How do I retrieve this name?
    Cheers,
    Chris Share

    Hey it's ok, you're new. But one thing you've got to learn is to RTFM (Read the .... Manual). You'll answer own your questions much faster that way.
    Take a look at the example at the top of:
    http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFileChooser.html

  • JFileChooser and date format questions

    I have a JFileChooser that currently works - I set the default directory and allow the user to select a file or enter a file name.
    I have been asked to modify this to prepopulate the file name field on the JFileChooser.
    How do I do that? It is not obvious which method to call to set the filename field.
    Also, the file name needs to be a timestamp "*yyyyMMddHHmmss*.dat"
    I tried creating the timestamp part with the following code, but it does not format as expected.
            String s = "";
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddddHHmmss");
            try
                java.util.Date now = new java.util.Date();
                java.util.Date dat = dateFormat.parse( now.toString() );
                s = dat.toString();
            }What is wrong with this?
    Thanks.

    I don't do Swing and this is also not the subforum for Swing questions. It has its own subforum.

  • JFileChooser and JTextArea question

    Hello NG !!!
    I Have to questions about the GUI with Swing:
    1.) JFileChooser: I have a JFrame with my JFileChooser (OpenDialog). How can I realize, that my OpenDialog shows me just the ".txt" files ?
    2.) JTextArea: I want use my JTextArea fro infromation the user about the result. How can I realize, that the JTextArea is deeper or has a frame ?
    Lot of thanks for your help !!!
    schaf77
    PS: Happy new Year !!!!

    1.) JFileChooser: I have a JFrame with my JFileChooser
    (OpenDialog). How can I realize, that my OpenDialog
    shows me just the ".txt" files ?Set a filefiler on your filechooser like this:
    myJFileChooser.setFileFilter(new MyFileFilter() );
    import java.io.File;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class MyFileFilter extends FileFilter {
        // Accept all directories and all txt files.
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            String extension = getExtension(f);
         if (extension != null) {
                if (extension.equals("txt") ) {
                        return true;
                } else {
                    return false;
            return false;
        private static String getExtension(File f) {
              String ext = null;
             String s = f.getName();
             int i = s.lastIndexOf('.');
             if (i > 0 &&  i < s.length() - 1) {
                   ext = s.substring(i+1).toLowerCase();
             return ext;
        // The description of this filter
        public String getDescription() {
            return "Textfile (*.txt)";
    2.) JTextArea: I want use my JTextArea fro infromation
    the user about the result. How can I realize, that the
    JTextArea is deeper or has a frame ?What do you mean?

  • 2 questions of JFileChooser,please !

    Dear all,
    I have have 2 questions about using JFileChooser:
    1. How to disable " Create a new folder"?
    2. How NOT to display the hidding files? It seems simply "chooser.setHiddingFilesEnabled(true)" dosn't take effect.
    Thanks a lot.
    Kevin

    1. How to disable " Create a new folder"?This I don't know, but I know that in your filter, you can filter out directories as an option
    2. How NOT to display the hidding files? It seems
    simply "chooser.setHiddingFilesEnabled(true)" dosn't
    take effect. what about chooser.setHidingFilesEnabled(false)?
    >
    Thanks a lot.
    Kevin

  • JFileChooser or FileDialog.... so many questions

    hi all,
    i have searched the forum to no avail so i need your help.
    i am wanting to use jfilechooser to get the directory listing and files over a tcp/ip connection. i send the getdirectory message to the server and it returns the directory. next, i send the getfiles message to the server and it returns the files in the directory. i can get the directory by overriding the setcurrentdirectory()method but how do i set the files in the filechooser. can it even be done this way. if so, what methods do i need to override to accomplish this task. any help will be greatly appreciated
    thanks

    I believe JFileChooser is only for file systems that the current JVM has access to (in other words, not remote file systems).

  • Quick question: JFileChooser mode

    Hello All,
    I have a bit of a problem regarding JFileChooser. My application has 1 main window, and subwindows which are created when certain actions are performed on the main window. I want to create a file chooser when I perform certain actions on one of the subwindows, and make that chooser modal with respect to the subwindow only. I want to be able to perform other operations on the main window, even if the file chooser is shown. Is this possible?
    Currently, I have a JFileChooser sublclass, and I tried to override the createDialog() method so that I can manipulate the shown dialog's modal property. So far this approach of changing the modal property does not get me close to the behavior I want. [ The dialog does not block when modal is set to false, and setting it to true makes the other windows inoperable. ]
    Does anyone have any idea regarding this problem? Please let me know.
    Thanks.
    Regards,
    Cocoh

    I am pretty sure that the modality (is that a word?) of a Dialog is by definition application wide. To do what you want you will probably have to manually enforce the single window modality. You could probably do this by adding a WindowFocusListener to your disabled window that will force focus to your quasi-modal window.
    Hope this helps,
    Josh Castagno
    http://www.jdc-software.com

  • Question regarding JFileChooser

    A user calls up a JFileChooser, navaigates through the file system and selects File "file" in Directory "directory."
    How do I set "directory" such tat the next time the the FileChooser is called up, opens up in "directory?"
    Many thanks in advance.

    JFileChooser(File currentDirectory)
    Constructs a JFileChooser using the given File as the path.
    Read the API before asking next time.

  • Question To JFileChooser - OpenDialog

    I use a JFileChooser to open a dialog for choosing a file:
    JFileChooser fc = new JFileChooser();
    int retVal = fc.showOpenDialog(m_main);
    In these dialog is a button for creating a new folder. But the new folder is always named "New Folder" and I cannot rename it in this dialog. Can I set any property to the JFileChooser so that I can rename the new folder ? Or if this is not possible, can I set that the button for creating a new folder is not visible ?
    Thanks for help, Nadine.

    Hi.
    The name "New Folder" is hardcoded in the FileSystemView, so theres no way to change it easily. Also no reference is saved for the new folder button in the ui.
    You can write your own ui (you can copy most things, just don't add the create button) or you search through the components in the FileChooser until you find the right button and remove it...
    Hope this helps

Maybe you are looking for