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 textarea
. I made the following program. However my compiler shows error in endsWith()
statement and only the first file name is printed if I run the program without the endsWith()
.
Any help identifying the issue is appreciated. Thanks in advance.
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<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();
}
}
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.
There's no File.endsWith()
, you need to get the file's name:
flist[i].getName().endsWith(nm)
You might also want to make sure it's a file (not a directory) just in case.
You need to say file.getName().endsWith( suffix );
Also, if you press Search without first pressing Open, I noticed that "str" is not being defined. You will get a null pointer exception here. You might want to check for that.
EDIT **
Try the following code. I have modified it so that it works. There is a defect in the way you wrote the code that results in a defect, though. You'll see it I'm sure. Fixing that defect should not be too tough for you.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test extends Thread implements ActionListener
{
JFrame f;
JButton bop, bser;
JTextField tf, text;
JTextArea ta;
JLabel lab, lab1;
String str;
JScrollPane scrol;
File fl;
Test()
{
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 );
bser.setEnabled( false );
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();
if ( str != null && !str.trim().equals( "" ) )
{
tf.setText( str );
// Enable the search button
bser.setEnabled( true );
}
else
{
bser.setEnabled( false );
}
}
if ( ae.getActionCommand().equals( "Search" ) )
{
fl = new File( str );
File[] flist = fl.listFiles();
for ( int i = 0; i < flist.length; i++ )
{
String newline = System.getProperty( "line.separator" );
String nm = text.getText();
if ( flist[i].getName().toLowerCase().endsWith( nm.toLowerCase() ) )
{
if ( flist[i].isFile() )
{
ta.setText( flist[i].getName() + newline );
}
}
}
}
}
public static void main( String args[] )
{
new Test();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.