title: Program to check whether extension of a file is .txt
body: <p>I wrote a java code having an awt text field and a button, where if I click on the button, I can browse a file using JFileChooser. It needs to check whether the file has ".txt" extension or not.I wrote the code below, but it is not getting verified.  </p>
  
  <p>Where am I going wrong?  Please help to determine where I am wrong.</p>
  
  <pre><code>         try{
              final JFileChooser chooser = new JFileChooser();
              chooser.showOpenDialog(null);
              chooser.addChoosableFileFilter(new FileFilter() {
              public String getDescription() {
                  return "*.txt";
              }
              public boolean accept(File filename)
              {
  
                  if(filename.getName().endsWith(".txt")){
                      return true;
                  }
                  else{
                  System.out.println("Browsed dest file extension must be .txt");
                  return false;
                  }}
              });
          catch(Exception ex)
          {
              JOptionPane.showMessageDialog(f,"Exception occurred");
          }
  </code></pre>