Program to list all files in a folder with user specified extension not working
<p>I am trying to make a program to which asks user to specify a file extension. It then lists all the available files with that extension in the <code>textarea</code>. I made the following program. However my compiler shows error in <code>endsWith()</code> statement and only the first file name is printed if I run the program without the <code>endsWith()</code>.</p>

<p>Any help identifying the issue is appreciated. Thanks in advance.</p>

<pre class="lang-java prettyprint-override"><code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class search extends Thread implements ActionListener
{
JFrame f;
JButton bop,bser;
JTextField tf,text;
JTextArea ta;
JLabel lab,lab1;
String str;
JScrollPane scrol;
File fl;

search()
{
    f=new JFrame("Search box");
    f.setLayout(null);
    f.setSize(820,700);

    bop=new JButton("Open");
    bop.setBounds(50,600,180,30);
    bop.addActionListener(this);
    f.add(bop);

    lab=new JLabel("Extension");
    lab.setBounds(340,570,180,30);
    f.add(lab);


    bser=new JButton("Search");
    bser.setBounds(510,600,180,30);
    bser.addActionListener(this);
    f.add(bser);    

    text=new JTextField();
    text.setBounds(280,600,180,30);
    text.addActionListener(this);
    text.setHorizontalAlignment(JTextField.CENTER);
    f.add(text);

    tf=new JTextField();
    tf.setBounds(25,50,750,40);
    tf.setFont(new Font("Lucida Console",Font.BOLD,20));
    tf.setHorizontalAlignment(JTextField.CENTER);
    f.add(tf);

    ta=new JTextArea();
    scrol=new JScrollPane(ta);
    //JScrollPane.setPreferredSize(750,450);
    scrol.setBounds(25,100,750,450);

    f.add(scrol);



    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public void actionPerformed(ActionEvent ae)
{
    if(ae.getActionCommand().equals("Open"))
    {
        FileDialog fd=new FileDialog(f,"Open Box",FileDialog.LOAD);
        fd.setSize(300,300);
        fd.setVisible(true);
        str=fd.getDirectory();
        tf.setText(str);                    
    }

    if(ae.getActionCommand().equals("Search"))
    {
        fl=new File(str);
        File[] flist=fl.listFiles();

        for (int i=0;i&lt;flist.length;i++)
        {
            String newline = System.getProperty("line.separator");
            String nm=text.getText();
            if(flist[i].endsWith(nm))
            {
                if(flist[i].isFile())
                {
                    ta.setText(flist[i].getName()+newline);
                }       
            }
        }               
    }
}   

    public static void main(String args[])
    {
        new search();
    }

}
</code></pre>

<p>the compiler is giving no error. its just that when i run the program... and feed in a directory path, as soon as i press search, only one file name is displayed in the textarea... and even if i dont enter anything in the textfield , and the if condition isnt satisfied, then also the same thing happens.</p>
