Results 1 to 8 of 8
  1. #1
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default JFileChooser with InputVerifier?

    Has anyone ever successfully used the JFileChooser with an InputVerifier? JFileChooser inherits setInputVerifier from JComponent but after setting a verifier, neither it's verify nor shouldYieldFocus methods ever seem to be invoked.

    I'm trying to force the user to name a .zip file. Am I barking up the wrong tree?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JFileChooser with InputVerifier?

    Unless I've misunderstood something, that looks like a job for a FileNameExtensionFilter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JFileChooser with InputVerifier?

    Quote Originally Posted by MGeorge View Post
    JFileChooser inherits setInputVerifier from JComponent but after setting a verifier, neither it's verify nor shouldYieldFocus methods ever seem to be invoked.
    The reason for that would be that the JFileChooser isn't the focus owner. Rather, one of its contained components has the focus.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: JFileChooser with InputVerifier?

    Quote Originally Posted by DarrylBurke View Post
    Unless I've misunderstood something, that looks like a job for a FileNameExtensionFilter.

    db
    FileNameExtensionFilter is a concrete implementation that extends FileFilter. It restricts the files shown by the chooser but pays no attention to what the user enters in the File Name text field. I want to dismiss the chooser only if the user has entered *mumble*.zip.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JFileChooser with InputVerifier?

    Quote Originally Posted by MGeorge View Post
    I want to dismiss the chooser only if the user has entered *mumble*.zip.
    Define 'dismiss'

    Better still, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that clearly demonstrates your approach to the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: JFileChooser with InputVerifier?

    Quote Originally Posted by DarrylBurke View Post
    Define 'dismiss'

    Better still, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that clearly demonstrates your approach to the problem.

    db
    Here is a simple app with a file chooser, a button to trigger it, and a text field to show the selection. Note the file chooser has an InputVerifier but the "verify called" trace is never printed.

    Java Code:
    import javax.swing.JOptionPane;
    
    /**
     * To change this license header, choose License Headers
        @Override
        public boolean verify(JComponent input) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author mag
     */
    public class Chooser extends javax.swing.JFrame {
    
        /**
         * Creates new form Chooser
         */
        public Chooser() {
            initComponents();
            chosen.setText("");
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            chooser = new javax.swing.JFileChooser();
            chooser.setInputVerifier(new javax.swing.InputVerifier() {
                public boolean verify(javax.swing.JComponent input) {
                    System.err.println("verify called");
                    if (chooser.getSelectedFile().getName().toLowerCase().endsWith(".zip")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            choose = new javax.swing.JButton();
            chosen = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            choose.setText("Choose...");
            choose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    chooseActionPerformed(evt);
                }
            });
    
            chosen.setEditable(false);
            chosen.setText("jTextField1");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(choose)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(chosen, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(125, 125, 125)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(choose)
                        .addComponent(chosen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(152, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void chooseActionPerformed(java.awt.event.ActionEvent evt) {                                       
            int returnVal = chooser.showOpenDialog(this);
            if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
                java.io.File file = chooser.getSelectedFile();
                chosen.setText(file.getAbsolutePath());
            }
        }                                      
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /** Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /** If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Chooser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Chooser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Chooser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Chooser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Chooser().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton choose;
        private javax.swing.JFileChooser chooser;
        private javax.swing.JTextField chosen;
        // End of variables declaration                   
    }
    Last edited by DarrylBurke; 09-30-2014 at 07:04 AM.

  7. #7
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: JFileChooser with InputVerifier?

    Hmmm... I didn't see the comment at the top of the source file until just now. I've become accustomed to ignoring NetBeans' comments. Looks like JFileChooser may not (yet) support InputVerifier. The JComponent API is silent as to whether or not all of it's subclasses are required to support it. I wonder how this got by the JCK team?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JFileChooser with InputVerifier?

    1. I've edited the code and added a few '*'s in the comment openers to overcome a known formatting bug on this forum.

    2. You need to learn more Java before I can even start to explain this to you. That's not a criticism or a slur, just a statement of fact.

    3. The way to actually learn something is to forgo the visual designer, which is an advanced tool, and write your GUI code yourself. It'll be a small fraction of the generated code.

    4. The commented out "Not suported yet" has everything to do with your IDE and your user actions in that IDE, and nothing at all to do with the JDK team.

    5. If you need to verify the name of the selected file before closing the color chooser dialog you need to write your own dialog code. JFileChooser#showOpenDialog(...) won't cut it.

    db

    Oh, and that's the last NB generated code I'm willing to read. I'm out of here unless you can post a SSCCE that actually is an SSCCE. It takes only a few lines of code to create one.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Creating a selective InputVerifier
    By oinca in forum Advanced Java
    Replies: 3
    Last Post: 08-15-2011, 06:53 PM
  2. JFileChooser
    By snus in forum Java Applets
    Replies: 11
    Last Post: 12-25-2010, 07:46 PM
  3. jFileChooser
    By ArneBassez in forum AWT / Swing
    Replies: 7
    Last Post: 12-01-2010, 04:38 AM
  4. JfileChooser Help
    By simmi in forum AWT / Swing
    Replies: 9
    Last Post: 08-04-2009, 02:24 AM
  5. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •