JFileChooser as a component?

Hey,
I need to have directory browsing capability but implementing a one from scratch is too much. Is there anyway to integrate JFileChooser into a component? Like to put it into my application in a panel rather than have it as a dialog???

Hey
Do you know how I could add keylistener and mouse listener to it?
I want to make it so that when the user presses del on keyboard then the selected file is deleted. And I want to add some right click actions as well.
Just adding listeners to JFileChooser doesn't seem to do the trick.
I would guess that if I take individual components of it and add the listeners to them, then that would work.
Any ideas?

Similar Messages

  • Using JFileChooser as a component

    How to know which button is pressed in a JFileChooser if I used it as a component instead of using its show...() method?
    I know I can use addActionListener to be notified when the user pressed a button, but the actionPerformed() is called even when the user pressed the cancel button. Is there a way to know which button is pressed?
    Thanks

    This is easy to do. Each ActionEvent has a command associated with it. This is so that complex components can use ActionListener to pass information. getActionCommand() the command, and then you need to compare it with something. JFileChooser.APPROVE_SELECTION and JFileChooser.CANCEL_SELECTION are static strings that you can compare with.
    so
    if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)
    open the file;

  • JFileChooser "File Name" field

    Hi,
    I am trying to use a JFileChooser as a component within a JDialog, and everything works fine, except that I do not understand how I can get the value of a filename typed into the "File Name:" field on the form. I've read the "JFileChooser" Java doco about five times and I can't see anything that would give me access to this field. GetName() appears to be a red herring.
    Anyone got any ideas?
    Thanks,
    Tim

    Thanks for your swift reply.
    Unfortunately perhaps I wasn't quite clear - I have added the JFileChooser as a component to a JDialog, not run it standalone via showDialog() or similar.
    I am trying to get the user to select a directory and type a file name that a new file can be saved into.
    Here is the code that displays the chooser:
       private JPanel saveFilePnl(String folder)
          JPanel panel = new JPanel(new BorderLayout());
          panel.add(new JLabel("<html>Select the folder to save the new sequence into,<br>" +
                               "and type in an appropriate File Name:<br>"),"North");
          saveFileChooser.setCurrentDirectory(new File(folder));
          SimpleFileFilter filter = new SimpleFileFilter("xml", "Nucleotide Sequences"); 
          saveFileChooser.setFileFilter(filter);
          saveFileChooser.setControlButtonsAreShown(false);
          saveFileChooser.setBorder(BorderFactory.createCompoundBorder
                                (BorderFactory.createEtchedBorder(),
                                 BorderFactory.createEmptyBorder(8,8,8,8) ));
          panel.add(saveFileChooser, "Center");
          return panel;
    }This panel is added to a card on a CardLayout JDialog. After the user selects a folder and types in a filename I want to verify that they have done so before moving on to the next card. So I need to ensure that there is a name in the "File Name:" field on the chooser.
    Thanks,
    Tim

  • JFileChooser detecting switch between "List" and "Details" views

    I'm customizing a JFileChooser and would like to provide users a longer version of the file names when in Details mode (Unix showing links).
    This might be accomplished using the getDescription method of FileView, I don't know. My FileView getTypeDescription and getDescription methods are never called but getIcon is. I added @Override annotations to all method, so I think I really am overriding them.
    Thanks!

    Here's a solution. I still think there should be a more official way of doing this.
    public MyFileView(JFileChooser chooser)
              for ( Component c : chooser.getComponents())
                   if (c.getClass() == FilePane.class)
                        filePane = (FilePane) c;
         @Override
         public String getName(File f)
              if (filePane.getViewType() == FilePane.VIEWTYPE_DETAILS){
                   return "Long";
              return null;
         }

  • JFileChooser - List and Details

    Hi,
    In the JFileChooser Dialog, we have two buttons "List" and "Details" to view and select the files.
    The default is "List" mode, when the File Chooser dialog opens.
    In this mode, if we type in the first character of the file we want to select, then the FileChooser will automatically focus the first file in that directory starting with that letter.
    For Example: If I type in 's' in the "File Name" text filed, the focus is on the first file in that directory starting with letter 's' like "sample.txt".
    But this same feature is not available in the "Details" mode.
    Does anyone have an idea of how to incorporate this in the "Details" mode ?
    Also is there a way to make the "Details" mode as the default mode when the FileChooser dialog opens.
    I have tried many sources but could not find any information regarding this problem.
    Any help will be appreciated.
    Thanks in advance,
    Neeharika.

    Here's a solution. I still think there should be a more official way of doing this.
    public MyFileView(JFileChooser chooser)
              for ( Component c : chooser.getComponents())
                   if (c.getClass() == FilePane.class)
                        filePane = (FilePane) c;
         @Override
         public String getName(File f)
              if (filePane.getViewType() == FilePane.VIEWTYPE_DETAILS){
                   return "Long";
              return null;
         }

  • JFileChooser "List" and "Details" button disabled.

    Hi,
    The two buttons "List" and "Details" buttons on the top-right of my JFileChooser dialog box are both disabled.
    Dose anyone know how to enable them?
    Thanks
    slam

    Here's a solution. I still think there should be a more official way of doing this.
    public MyFileView(JFileChooser chooser)
              for ( Component c : chooser.getComponents())
                   if (c.getClass() == FilePane.class)
                        filePane = (FilePane) c;
         @Override
         public String getName(File f)
              if (filePane.getViewType() == FilePane.VIEWTYPE_DETAILS){
                   return "Long";
              return null;
         }

  • FileDialog - Multiselect

    Hello,
    Is it possible to select multiply files within a FileDialog, generated by Java?
    In other languages you only have to set the property "multiselect" to "true".
    Thanks,

    FileDialog, an AWT component cannot, but JFileChooser, a Swing component can.

  • Is There Any Component Similar To JFileChooser To Save Information In File

    Hi All,
    I have an application in JSP where I need to save the some information from my jsp page into a file.Like JFileChooser opens the required files, is there any component similar to JFileChooser to "save" the information into a file???
    Thanks In Advance,
    Anupama M

    That's easy :) You can write it yourself.
    Imagine you've a List<Dto> where each DTO represents one row. Create a StringBuilder (or StringBuffer if you're still not at Java 1.5) and append each row value to it. Eventually add separators (comma, semicolon, etc) to it. After each row, eventually add a newline. On the end, stream the contents of the StringBuilder to the HttpServletResponse#getOutputStream(). Don't forget to set the headers accordingly.

  • Adding Component to JFileChooser excludes other components

    I want to add a JComboBox component to a JFileChooser but when I do, the default components that are supposed to be in that space don't show up.
    For example, in the JFileChooser, there are several components (like the accept and cancel buttons) in the SOUTH area of the BorderLayout. When I add a custom combobox like so:
    JFileChooser fc = new JFileChooser();
    cb = new JComboBox();
    fc.add(cb, BorderLayout.SOUTH);
    dialogResult = fc.showSaveDialog();the combobox I added is the ONLY component which shows up in the SOUTH area.
    I have tried adding the combobox as an accessory, but it always shows up to the right of the file browser window. If I can control where the accessory shows up, that would be fine, but I don't see any way to do that.
    I don't understand why the other components wouldn't show up?
    Thanks in advance for any help.

    fc.add(cb, BorderLayout.SOUTH);
    This line replaces the components that were
    previously in that space - read the tutorial on
    BorderLayout. In addition, this code is very brittle
    and you can have no guarantee that it will work under
    different look-and-feels and in future versions of
    JDK, since you're making some far-fetching
    assumptions on the internal implementation of the
    file chooser dialog.Thanks very much for the help!
    Fundamentally, i did not realize that in BorderLayout you may only set ONE component to each area (NORTH, SOUTH, CENTER, etc.).
    Once I understood that, I grabbed the component at the bottom of the filechooser and placed it in a JPanel with the components I wanted to Add to the bottom.
    This worked perfectly!
    I am uncertain, however, about why you say the code is "brittle" and what you mean by making assumptions regarding the internal implementation.
    In any case, thanks again for your help!
    For anyone else who is interested, here is what the code looks like
    JFileChooser fc = new JFileChooser();
    JComboBox cb = new JComboBox();
    JPanel panel = new JPanel();
    BoxLayout box = new BoxLayout(panel,BoxLayout.Y_AXIS);
    BorderLayout layout = (BorderLayout) fc.getLayout();
    panel.setLayout(box);
    Component comp = layout.getLayoutComponent(BorderLayout.SOUTH);
    panel.add(comp);
    panel.add(cb);
    fc.add(panel,BorderLayout.SOUTH);

  • Changing LookAndFeel for a single component

    Is it possible to change the LookAndFeel for a single component (example FileChooser) ?
    regards,
    nirvan.

    In the case of a file chooser: you can use JFileChooser which will use the current look and feel or you can use the old AWT FileDialog which will use the system's look and feel. The FileDialog naturally has less things you can do with it.Thanks for thre reply. Actually I want to use windows look and feel for the JFileChooser dialog, because the metal look and feel does not display default icons associated with file type. Right now I am switching to the windows look and feel just before the JFileChooser code and then when file choosing is done, switching back to metal look and feel. Is that alright or does that have any potential implications ?
    regards,
    nirvan.

  • Problems with JFileChooser and Windows 2000 (can't see mydocuments contents

    Hi! I've an applet which has a JFileChooser component. In other Windows, I can select the MyDocuments folder and it goes there and list all the contents OK. But, in Windows 2000, when I go to MyDocuments, none of the contents is listed in the file chooser. I need to navigate through all the directories to get there (C:\documents and settings\user\my documents) and then, it shows its contents, but obviously, I don't want it to be this way because is difficult for the common user.
    Any idea of why is going on this?
    Thanks in advance!

    Hello, You need to use something like o=isp as the
    root DN, then o=yourorg.com goes beneath it.
    i.e.
    For the other questions on setup, just choose o=isp
    as base DN when you set up the directory server. When
    you run ims_dssetup.pl, choose o=internet as the DC
    tree base, then choose o=isp as the user/groups base
    suffix.
    When you are installing messaging server it will ask
    where to put the default organization, this is where
    you would choose o=abc.com, o=isp
    For more info on how this structure works please take
    a look at this link and it will all fit into place:
    http://docs.sun.com/source/816-6017-10/changes.htm#170
    8Alright, I got ims_dssetup to run successfully. But now when the ims 5.2 installation is about 50% done, I get the message:
    A serious problem occurred while installing the iPlanet Messaging Server Messaging SIE entry in LDAP (msg.cfgldap.sie.inf). It reported the following problem:
    The server configuration for the Messaging SIE entry in LDAP (msg.cfgldap.sie.inf) cannot be created.

  • 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.

  • JFileChooser/FileDialog TextField selections

    I have a program which will use file extension for file type associations.
    What I want is when a user views the Dialog to save a file, this file extension to be in the FileDialog/JFileChooser, but unselected. An example of this would be when the user selects 'save as', a FileDialog appears with a default name:
    filename.extension
    where 'filename' is selected/highlighted and '.extension' is not.
    This seems technically easy to do, but I can't find any documentation on how to do it. I know I can just add on an extension after the user sets the name and closes the dialog, but I'd rather not go that route.
    Thanks in advance
    Edited by: sierratech on Oct 17, 2008 12:29 AM

    this seems to work OK
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    class Testing
      JTextField tf;
      public void buildGUI()
        JFrame f = new JFrame();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser  fc = new JFileChooser(".");
        fc.setSelectedFile(new File("test.txt"));
        getChooserTextField(fc.getComponents());
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            tf.select(0,tf.getText().indexOf("."));
        fc.showSaveDialog(f);
        f.setVisible(true);
      public void getChooserTextField(Component[] comp)
        for(int x = 0; x < comp.length; x++)
          if(comp[x] instanceof JPanel) getChooserTextField(((JPanel)comp[x]).getComponents());
          else if(comp[x] instanceof JTextField)
            tf = ((JTextField)comp[x]);
      public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
    }

  • How to change the image of a selected File/Directory in JFileChooser while

    Hi all,
    I am trying to customize JFileChooser. Basically, I want to display a checked Icon for a selected directory(s) in JFileChooser
    I know that fileView class returns icon for directory/files, but I am not sure how to change their images on selection.
    your help is appreciated.
    Ramesh

    could you please anyone help me with this. I search all the web but I was not successful to find anything that would help me to create a custom renderer for JList component inside JFileChooser..
    if you can give me some reference websites that would be great.
    Ramesh

  • JFileChooser throws null pointer exception

    I'm using JFileChooser in my gui class. After I make my selection in the file chooser it sets off a null pointer exception. I'm running windows xp professiona. It was working perfectly fine till recently.
    My code for calling the chooser is:
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == chooser.APPROVE_OPTION){
          //setup the client now with the selected file to send
          client.setFile(chooser.getSelectedFile());
          jLabel5.setText(chooser.getSelectedFile().getName());
        }java.lang.NullPointerException
         at ftpresume.ClientGUI.jButton2_actionPerformed(ClientGUI.java:128)
         at ftpresume.ClientGUI_jButton2_actionAdapter.actionPerformed(ClientGUI.java:174)
         at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
         at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
         at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
         at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
         at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
         at java.awt.Component.processMouseEvent(Component.java:5100)
         at java.awt.Component.processEvent(Component.java:4897)
         at java.awt.Container.processEvent(Container.java:1569)
         at java.awt.Component.dispatchEventImpl(Component.java:3615)
         at java.awt.Container.dispatchEventImpl(Container.java:1627)
         at java.awt.Component.dispatchEvent(Component.java:3477)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
         at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
         at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
         at java.awt.Container.dispatchEventImpl(Container.java:1613)
         at java.awt.Window.dispatchEventImpl(Window.java:1606)
         at java.awt.Component.dispatchEvent(Component.java:3477)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

    I took a look at checking to see if the file is valid, exists, is readable and everything else. Looks like the file that's returned if fine. I'm not sure why my program crashes when I setup the file though. Here's my code from the client.setFile(File f);
    public void setFile(File f){
        if(f!=null){
          this.f = f;
          try {
            channel = new FileInputStream(f).getChannel();
            client.setupProgressBar(0,(int)f.length());
          }catch (FileNotFoundException e) {client.showError("FileNotFoundException: " + e);}
          catch(IOException e){client.showError("IOException: " + e);}
      }

Maybe you are looking for

  • Stock Transfer Report after Invoice

    Hello Experts Please spend one minute's time to read this. It may be long but its the exact scenario. I have got a requirement to generate a Stock Transfer Report for the bills passed (invoice receipt) for a particular date range. Note that the deliv

  • HT1338 I have a 2009 MacBook Pro running OS X 10.5.8. How do I upgrade to 10.6 or later??

    I have a 2009 MacBook Pro running OS X 10.5.8. How do I upgrade to 10.6 or later??

  • OIM - starting two process tasks simultaneously

    Hello, I have a process definition with many process tasks. If a certain trigger/event happens (eg. Change Last Name) I want to run two particular process tasks. How do I achieve this? My guess was to use the option to generate new tasks based on the

  • Include Javascript in XSL

    Hello, I have a javascript file that I want to include into my xsl file (the Javascript file has also some JSP code). The output of the xsl file should be JSP. I tried tons of sollutions and none of them worked. here some code of my .js file: functio

  • Simple test suite for regression testing

    Hi, I was wondering if anyone has a simple test suite available. I'm after these main areas: 1. arbitary script lanching (or just firing off ud32 files) 2. database comms (inserting test data, extracting final status) 3. comparison (ala diff) for reg