JFileChooser  - only make certain directories selectable

I would like the user to be able to view all directories but only be able to choose ones that contain a certain file.
For example, most IDE's allow you to only select directories that cantain project file (they even manage to give project directories different icons).
HOW?
Here is what I have got so for...
private void openProjectMenuItemActionPerformed(java.awt.event.ActionEvent evt)
            System.out.println("open project");
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.addChoosableFileFilter(new javax.swing.filechooser.FileFilter()
                @Override
                public boolean accept(File f)
                    if(!f.isDirectory())
                        return false;
                    for(File file:f.listFiles())
                        if((file.getName().toLowerCase()).endsWith("jan"));
                            return true;
                    return false;
                @Override
                public String getDescription()
                    return "Return JAnnotate folders";
            if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
                System.out.println(jfc.getSelectedFile().getName());
    }

Thanks for your help.
This seems to work a little better for me but its still doesn't feel like the best solution.
import javax.swing.JFileChooser;
import utilities.MyFileChooser;
public class FileFilterEg {
    public static void main(String[] args)
        JFileChooser jfc = new MyFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if(jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            System.out.println(jfc.getSelectedFile().getName());
package utilities;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileView;
public class MyFileChooser extends JFileChooser
    public MyFileChooser()
        setFileView(new FileView()
            @Override
            public Icon getIcon(File f)
                if(fileSelectable(f))
                    return new javax.swing.ImageIcon(getClass().getResource("/images/janicon.png"));
                return super.getIcon(f);
    @Override
    public void approveSelection()
        File f=getSelectedFile();
        if(fileSelectable(f))
            super.approveSelection();
        else
            if(f.isDirectory())
                this.setCurrentDirectory(f);
    private boolean fileSelectable(File f)
        if(!f.isDirectory())
            return false;
        try
            for(String fileName:f.list())
                if((fileName.toLowerCase()).endsWith(".jan"))
                    return true;
        catch(NullPointerException e)// sometimes a NullPointerException is thrown.  Can't work out why.
            System.err.println("====================");
            System.err.println(e.getMessage());
            System.err.println("====================");
        return false;
}

Similar Messages

  • JFileChooser - show files and directories only select directory

    I need a JFileChooser that will display directories and zip files but only allow a directory to be selected.
    basically i need to be able to select a certain directory that contains a certain zip file, so to make it easier to find i want the zip files to show up so i can find the correct directory...
    I tried...
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.setFileFilter(new FileFilter() {
                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    else if (f.getName().endsWith(".zip")) {
                        return true;
                    return false;
                public String getDescription() {
                    return "testing";
            });but that only displays directories in the GUI.
    The javadoc for the setFileSelectionMode() says, "Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY. " ....which makes you think that setting the file selection mode only alters what can be SELECTED...however it alters what is displayed as well.
    ...any ideas?
    thanks

    This should display the files of interest provided the filter is tuned to do so, and allow you to select either the file of interest or the folder... If you select the file, then folder it is contained is returned.
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    MyFileFilter filter = new MyFileFilter();
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {              
      System.out.println(chooser.getCurrentDirectory());
    }

  • How to make users to select the date from calendar control my making the date field read only in date time control in external list in sharepoint 2010

    How to make users to select the date from calendar control only, by my making the date text field read only (don't want to let users type the date) in date time control in external list in sharepoint 2010. I am looking for a solution which can
    be done through sharepoint desginer / out of the box.
    thanks.

    Congratulate you got the solution by yourself. I am new to a
    WinForms calendar component, I feel so helpless on many problems even I'd read many tutorials. This question on the
    calendar date selection did me a great favor. Cheers.

  • How to customise a JFileChooser so that it can select Directories also?

    i need to customise the JFileChooser so that i can select the directories also. now when i click on the open button it opens the contents of the directories. but i want that if i have selected the directory then i can choose its name or i can select the file also
    plz help fast
    thanx in advance

    Use the setFileSelectionMode method, like this:
    chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
    or JFileChooser.DIRECTORIES_ONLY

  • Make certain fields in the Account Detail Page Read-Only based on a Checkbo

    Hello
    We would like to make certain fields on the Account Detail Page Read-Only based on a checkbox that is on the Admin Account Detail Page. The fields we are looking to make read-only are Account Name, Primary Address, Phone Number.
    Thanks

    Brian,
    I think you'd need to use dynamic page layouts for this. Look at the Account type values, if you are already using it then replicate those values you have so if you have Business and Personal make it Business Read-only and Personal Read-only.
    When you are ready to lockdown those fields change the Account type from Business to Business Readonly and have the page layout for this screen with Account type, Name, Primary address and phone number as read only. You will a separate page layout for yourself so that Account Type stays as writable for you.
    You will need to check if you can have Account Name as read only as it is a required field.
    cheers
    Alex

  • HT4927 Are there ways to only upload certain photos from my iphoto backup? It is really big and I don't want all the photos on my new computer.

    Are there ways to only upload certain photos from my iphoto backup? It is really big and I don't want all the photos on my new computer.

    There is a way to find and recover individual photos from a Time Machine backup. 
    1 - dowload and launch  Find Any File.
    2 - select the Time Machine hard drive in FAF's Find Items menu.
    3 - enter the full file name of the photo you want to recover into FAF and use the Find button.
    NOTE: it will take a while for FAF to retrieve and display the results of the search due to the nature of how TM makes its backups. 30 seconds or so is normal.
    4 - select the file in the Masters folder and use the Space bar to verify it's the one you want with Quickview.
    5 - drag the file from the FAF window to your Desktop.
    6 - repeat for each photo you need to restore.
    OT

  • How to make a JPanel selectable

    When extending a JPanel and overriding the paintComponent() method the custom JPanel can not be selected so that it gets for example KeyEvents.
    But if I make the new Class extend a JButton it gets of course selected and able to receive for example KeyEvents.
    My question is therefore; what does the JButton implement that a JPanel doesn’t so that a JButton gets selectable? Or in other words; how to make a JPanel selectable?
    Aleksander.

    Try this extended code. Only the first panel added can get the Focus.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Test extends JFrame
      public Test()
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel1 = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new BorderLayout());
        ImagePanel imgPanel = new ImagePanel();
        panel1.setFocusable(true);
        panel2.setFocusable(true);
        panel1.setPreferredSize(new Dimension(0, 50));
        panel2.setPreferredSize(new Dimension(0, 50));
        panel1.setBorder(BorderFactory.createLineBorder(Color.RED,     4));
        panel2.setBorder(BorderFactory.createLineBorder(Color.CYAN,    4));
        imgPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
        panel1.add(new JLabel("Panel 1"), BorderLayout.CENTER);
        panel2.add(new JLabel("Panel 2"), BorderLayout.CENTER);
        getContentPane().add(panel1, BorderLayout.NORTH);
        getContentPane().add(panel2, BorderLayout.SOUTH);
        getContentPane().add(imgPanel, BorderLayout.CENTER);   
        pack();
        panel1.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent ke){
                System.out.println("Panel1");}});
        panel2.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent ke){
                System.out.println("Panel2");}});
      public static void main(String[] args){new Test().setVisible(true);}
    class ImagePanel extends JPanel
      Image img;
      public ImagePanel()
        setFocusable(true);
        setPreferredSize(new Dimension(400,300));
        try{img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("Test.gif"), "Test.gif"));}
        catch(Exception e){/*handled in paintComponent()*/}
        addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent ke){
            System.out.println("ImagePanel");}});
      public void paintComponent(Graphics g)
        if(img != null)
          g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this);
        else
          g.drawString("This space for rent",50,50);
    }

  • I just bought a 2gb shuffle and i have more music than it can hold in my itunes acct. How can i sync only the certain playlists on the shuffle?

    I just bought a 2 gb shuffle and I have more music in my itunes than it can hold.  How can i sync only the certain playlists on the shuffle?

    Yes. Configure your Shuffle to sync only selected playlists from under its Music tab.  This article should be able to assist you with this.
    http://support.apple.com/kb/ht1719
    B-rock

  • Can you make certain social network applications available in all countries?

    I've recently been hearing a lot about the application Yik Yak, and I wanted to download it. However, when I went to download it, I found out it wasn't available for Singapore users, but only for United States users. This I find rather unfair because there are a lot of people all around the world who are unable to download certain applications or even purchase certain items from the itunes store, simply because they do not live in the United States. Is it possible, to make certain applications and even items accessible to the whole world rather than just the US?

    Contact the developer.
    http://www.yikyakapp.com

  • Text Mandatory only for Certain Materials in Sales Order

    Is there anyway so that I can make certain item text mandatory only for certain materials?
    I tried creating new item category group and new item category. And then create new Text procedure making this text mandatory and assigning to this new item category.
    But there are lot of different types of item categories assigned. So it does not look good to create new item category for each and everyone for this new item category group and material.
    Please guide me how to make the document incomplete for certain materials only when one text is not filled at item level
    Thanks in advance

    Hi,
    what you tried is correct and that is been followed in most of my projects wherever I worked.  That is the best way recommended also.
    If you try with incompletion procedures for item category also, you will endup doing multiple item categories anyway....
    First when I looked at the subject line, even before I opened your POSTING, I got the same resolution as you did....May be the optimal one but, not sure, if something else is possible other than by doing user exit.
    Regards

  • I have several issues that I hope you can help me out with. I am a professional photographer with a Mac with OS X 10.9.4: - I can only make ONE contact sheet in CS6 (from AUTOMATE) from pictures indicated in Bridge. If I want to make another contact sheet

    I have several issues that I hope you can help me out with. I am a professional photographer with a Mac with OS X 10.9.4:
    - I can only make ONE contact sheet in CS6 (from AUTOMATE) from pictures indicated in Bridge. If I want to make another contact sheet I need to shut down and restart CS6.
    - Camera RAW: if I change any setting in Camera RAW (except exposure), having first optimized the exposure, the exposure will automatically return to zero. Sometimes when I've set exposure and cropped the picture in Camera RAW Plug-In, the effects are lost just by opening the picture in CS6.
    - Sometimes if I crop a picture in Camera RAW, it may be impossible to open it again from Bridge!
    - I cannot do lens corrections in Camera RAW, at all. The Camera RAW comment is that "it cannot load the correct lens type". Why?

    Are you writing that when you select  a large number of images in the bridge  like a hundred thumbnails  when you then use Bridge menu Tools>Photoshop>Contact Sheet II that Photoshop only produces a single contact sheet for the first few thumbnails and fails to produce additional contact sheets for the other selected thumbnails? Photoshop contact sheet II script should create as many contact sheet as needed  for the numbers of thumbnails selected.  Perhaps the script is hung or you have exhausted  your machine resources.
    ACR Crop tool does not crop the RAW file it records crop settings to be used during the conversion process.    Camera RAW DATA is not altered.  The only changes made to RAW Files is ACR Metadata may be added.  Anything you do in ACR can be un-done in ACR.    If you can not open a RAW File in ACR the Files is either corrupt or from a new camera ACR does not support yet.  ACR should not corrupt RAW files.
    If you do not have a lens profile for ACR I think you should still be able to do lens correction manually in ACR.
    Some of what you write I don't follow could you post some screen captures of your problems often a picture helps.

  • If I select multiple images to apply a change to all Aperture it does so only with the first selected. Why?

    If I select multiple images to apply a change to all openings it does so only with the first selected. Why?

    You probably have Primary Selection only on.
    Aperture identifies the images you’ve selected by displaying them with a white border. When you select a group of images, the actively selected image, called the primary selection, appears with a thick white border and the rest of the selected images appear with thin white borders.
    Primary Only button: Click this button to make changes to the primary image selection only.

  • Is it possible to make certain rows mandatory in ALV custom Grid display?

    Hi experts,
                    Is it possible to make certain columns mandatory in the ALV custom grid control just as we give in a selection screen ?
    Thanks in advance
    regards,
    Ashwin

    fieldcat-key = 'X'.
    Regards,
    Amit

  • Can I make two differents selection screen in my program?

    Hi experts!!
    I don't know how to male this, the problem is that I need to make two differents selection screen in my program because I have two differents kinds of button,
    BUTTON1         BUTTON2          BUTTON3
    When I press button1 I received.
    BUTTON4         BUTTON5          BUTTON6
    Now If I press on button4, how can I pick up the event? the name or something associate to this button4?
    Thanks a lot and regards
    Rebeca

    Hi and thanks,
    I've tried with your code,  Velu Lakshmanan,  and I only received another screen with the buttons... I need to put this button in the same screen, now I've this, but I can't receive the event associate to the second's buttons,
    SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    SELECTION-SCREEN SKIP.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON  1(79) DTP USER-COMMAND DTP
    VISIBLE LENGTH 25.
    SELECTION-SCREEN PUSHBUTTON 28(79) EMP USER-COMMAND EMP
    VISIBLE LENGTH 25.
    SELECTION-SCREEN PUSHBUTTON 55(79) ABS USER-COMMAND ABS
    VISIBLE LENGTH 25.
    PARAMETERS: PROC_TYP TYPE I DEFAULT 1 NO-DISPLAY.
    SELECTION-SCREEN END OF LINE.
    * Informes de datos de tiempo.
    SELECTION-SCREEN SKIP.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-FED USER-COMMAND FED MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-DCH USER-COMMAND DCH MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-ACH USER-COMMAND ACH MODIF ID DT1.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-FII USER-COMMAND FII MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-PTB USER-COMMAND PTB MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-PTS USER-COMMAND PTS MODIF ID DT1.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-ACI USER-COMMAND ACI MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-FIM USER-COMMAND FIM MODIF ID DT1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-LDH USER-COMMAND LDH MODIF ID DT1.
    SELECTION-SCREEN END OF LINE.
    * Informes de empleados
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-LDP USER-COMMAND LDP MODIF ID EM1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-PDF USER-COMMAND PDF MODIF ID EM1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-LDT USER-COMMAND LDT MODIF ID EM1.
    SELECTION-SCREEN END OF LINE.
    * Informes de bajas
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-BAJ USER-COMMAND BAJ MODIF ID AB1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-VMA USER-COMMAND VMA MODIF ID AB1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-SDE USER-COMMAND SDE MODIF ID AB1.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN PUSHBUTTON 1(25)  TEXT-LDB USER-COMMAND LDB MODIF ID AB1.
    SELECTION-SCREEN PUSHBUTTON 28(25) TEXT-LDA USER-COMMAND LDA MODIF ID AB1.
    SELECTION-SCREEN PUSHBUTTON 55(25) TEXT-DHT USER-COMMAND DHT MODIF ID AB1.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN END OF BLOCK B1.
    * Pintamos los botones que necesitemos mostrar según la opción seleccionada
    AT SELECTION-SCREEN.
      CASE SSCRFIELDS-UCOMM.
        WHEN 'DTP'. PROC_TYP = 1.
        WHEN 'EMP'. PROC_TYP = 2.
        WHEN 'ABS'. PROC_TYP = 3.
      ENDCASE.
    AT SELECTION-SCREEN OUTPUT.
      CASE PROC_TYP.
    * Cuando el botón que ha seleccionado es "Datos de tiempo"
        WHEN 1.
          CALL FUNCTION 'ICON_CREATE'
            EXPORTING
              NAME   = ICON_OKAY
              TEXT   = TEXT-DTP
              INFO   = TEXT-AUS
            IMPORTING
              RESULT = DTP.
          EMP = TEXT-EMP.
          ABS = TEXT-ABS.
          LOOP AT SCREEN.
            IF SCREEN-GROUP1 EQ 'DT1'.
              SCREEN-INPUT = '1'. SCREEN-INVISIBLE = '0'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'EM1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'AB1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
          ENDLOOP.
        WHEN 2.
          CALL FUNCTION 'ICON_CREATE'
            EXPORTING
              NAME   = ICON_OKAY
              TEXT   = TEXT-EMP
              INFO   = TEXT-AUS
            IMPORTING
              RESULT = EMP.
          DTP = TEXT-DTP.
          ABS = TEXT-ABS.
          LOOP AT SCREEN.
            IF SCREEN-GROUP1 EQ 'DT1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'EM1'.
              SCREEN-INPUT = '1'. SCREEN-INVISIBLE = '0'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'AB1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
          ENDLOOP.
        WHEN 3.
          CALL FUNCTION 'ICON_CREATE'
            EXPORTING
              NAME   = ICON_OKAY
              TEXT   = TEXT-ABS
              INFO   = TEXT-AUS
            IMPORTING
              RESULT = ABS.
          DTP = TEXT-DTP.
          EMP = TEXT-EMP.
          LOOP AT SCREEN.
            IF SCREEN-GROUP1 EQ 'DT1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'EM1'.
              SCREEN-INPUT = '0'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN.
            ENDIF.
            IF SCREEN-GROUP1 EQ 'AB1'.
              SCREEN-INPUT = '1'. SCREEN-INVISIBLE = '0'. MODIFY SCREEN.
            ENDIF.
          ENDLOOP.
      ENDCASE.
    Help please... I don't know how can I make this.....
    If I use screen...how can I put this screen in the correct place?
    Thanks a lot
    Regards,
    Rebeca

  • Make a multiple selection of material in sale order

    In sale order, currently it supports only one line item selection of material in Approved product selection. How to make multiple line item selection from Approved product selection screen and add it it to sales order (order entry screen).

    Hi,
    For Item proposal you have to use sales doc type PV and it has to use in transaction VA51. Enter the materials you want and give some description at header and save it , After saving it will generate a number . Note the number and enter the number in customer master data ,sales area data in sales tab page for the field Item proposal. Then rise the sales order on the top of menu click the Icon item proposal , Here you have to select the items with or without quantities.
    Thank you,
    pranay

Maybe you are looking for

  • CD burner will not burn audio cds from iTunes

    I have an Iomega external CD-RW burner that has been working perfectly until now. When I try to burn a cd from a playlist in iTunes, after inserting the blank cd... it says that it is checking the playlist, then the status goes to preparing to burn..

  • Query on report display in table format on 7970G

    Hi, I know how to use the xml objects to display data on Cisco IP Phone using Ip Phone services. Now I want to generate report in table format on the phone.But in the xml objects available there is no option display data in table format . Should I ha

  • Rfc Adapter and Service

    Hi!! I try to sent a message from R3 (RFC) to XI and then to File Adapter. I configure all but I've problem with "Service" Rfc2XmbService. I don't understand how configure that!! 1) with which gateway I configure it?!?! And where?!? In XI System?!?!

  • H.264 on SAFARI(iPhone/iPad)

    Hello guys. I am a university student and I am doing a project regard of getting live stream from IP camera via vlc player and send it to a particular browser (iphone/ipad) using HTML5 video player (Projekktor and VideoJS). Basically I receive RTSP l

  • Which version lightroom for non-intel mac 10.5.8

    Which Version of Lightroom works best with a (non-Intel) Mac 10.5.8?  Thank You.