This is a program written in java that searches a file in the specific directory or in a Drive....
code::
//Search.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
class Search implements ActionListener,Runnable
{
JFrame mainFrame;
JPanel rightPanel,mainPanel;
JPanel leftPanel;
JTextField tf1,tf2;
JButton jb;
int sw;
int sh;
JTable table;
DefaultTableModel model;
JScrollPane sp;
static int count=0;
Vector v=new Vector();
JComboBox cb;
JButton up,down;
JLabel temp;
JButton browse;
JCheckBox specific;
String pathtolook="";
JLabel dynamic;
ImageIcon icon;
JLabel progresslb;
JLabel imglb;
ImageIcon i;
Search()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
sw=d.width;
sh=d.height;
mainFrame=new JFrame("Search");
leftPanel=new BackgroundPanel();
rightPanel=new JPanel();
jb=new JButton("search");
cb=new JComboBox();
File roots[]=File.listRoots();
for(int i=0;i cb.addItem(roots[i]);
//cb.getEditor().getEditorComponent().setBackground(Color.gray);
Vector v=new Vector();
v.add("Name");
v.add("Directory");
v.add("Size");
v.add("Type");
v.add("Date Modified");
String col[]={"Name","Directory","Size","Type","DataModified"};
sp=new JScrollPane();
table=new JTable();
model=new DefaultTableModel(col,0);
table.setModel(model);
//table.setShowGrid(false);
//table.setShowHorizontalLines(true);
//table.setShowVerticalLines(false);
sp.setViewportView(table);
model.setColumnIdentifiers(v);
leftPanel.setLayout(null);
JLabel lb1=new JLabel("All or Part Name of the File:");
lb1.setBounds(20,150,140,20);
leftPanel.add(lb1);
tf1=new JTextField();
tf1.setBounds(20,170,140,20);
leftPanel.add(tf1);
JLabel lb2=new JLabel("A word or Phrase in the File:");
lb2.setBounds(20,200,140,20);
leftPanel.add(lb2);
tf2=new JTextField();
tf2.setBounds(20,220,140,20);
leftPanel.add(tf2);
JLabel lb3=new JLabel("Look In:");
lb3.setBounds(20,250,140,20);
leftPanel.add(lb3);
cb.setBounds(20,270,140,20);
leftPanel.add(cb);
jb.setBounds(55,310,80,30);
leftPanel.add(jb);
i=new ImageIcon("sd.gif");
imglb=new JLabel(i);
imglb.setBounds(70,400,71,74);
leftPanel.add(imglb);
browse=new JButton("Specific folder search");
browse.setEnabled(false);
//browse.setContentAreaFilled(false);
browse.setBounds(10,500,150,20);
leftPanel.add(browse);
browse.addActionListener(new BrowseListener());
specific=new JCheckBox("Specific Search");
specific.addActionListener(new BrowseListener());
specific.setBounds(30,350,100,20);
leftPanel.add(specific);
specific.setContentAreaFilled(false);
icon=new ImageIcon("pb1.gif");
progresslb=new JLabel(icon);
leftPanel.setBounds(0,0,180,sh-60);
rightPanel.setBounds(180,0,sw-180,sh-60);
sp.setBounds(0,0,sw-185,sh-60);
//leftPanel.setBackground(Color.gray);
rightPanel.setBackground(Color.white);
rightPanel.setLayout(null);
rightPanel.add(sp);
mainPanel=new JPanel();
mainPanel.setLayout(null);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
//mainFrame.setLayout(null);
mainFrame.add(mainPanel);
mainFrame.setSize(sw,sh-30);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb.addActionListener(this);
}
class BrowseListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfilechooser = new JFileChooser();
jfilechooser.setFileSelectionMode(1);
if(e.getSource() == browse)
{
int i = jfilechooser.showOpenDialog(null);
if(i == 0)
{
pathtolook=jfilechooser.getSelectedFile().getAbsolutePath();
dynamic=new JLabel("Look In: "+jfilechooser.getSelectedFile().getAbsolutePath());
dynamic.setBounds(5,550,200,20);
leftPanel.add(dynamic);
leftPanel.updateUI();
}
}
if(e.getSource()==specific)
{
if(specific.isSelected())
{
cb.setEnabled(false);
browse.setEnabled(true);
}
else
{
cb.setEnabled(true);
browse.setEnabled(false);
leftPanel.remove(dynamic);
leftPanel.updateUI();
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(tf1.getText().equals("") && tf2.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter the filename to search", "What to search", 0);
}
else
{
progresslb.setBounds(10,600,160,13);
leftPanel.add(progresslb);
leftPanel.updateUI();
i=new ImageIcon("searchdog.gif");
imglb.setIcon(i);
if(specific.isSelected())
{
new Thread(this).start();
}
else
{
pathtolook=""+cb.getSelectedItem();
new Thread(this).start();
}
}
jb.setEnabled(false);
}
public void run()
{
addDataInTable(tf1.getText().equals("")?tf2.getText():tf1.getText(),pathtolook);
icon =new ImageIcon("pb2.gif");
progresslb.setIcon(icon);
i=new ImageIcon("sd.gif");
//imglb.setBounds(70,400,71,74);
imglb.setIcon(i);
JOptionPane.showMessageDialog(null,"Search Complete\n No. of Files and Directories found:"+count,"Complete",JOptionPane.PLAIN_MESSAGE);
}
public void addDataInTable(String dts,String rts)
{
File f=new File(rts);
File[] list=f.listFiles();
for(int i=0;i {
if(list[i].isDirectory() && !list[i].isHidden())
{
if(list[i].getName().contains(dts))
{
v=new Vector();
v.add(list[i].getName());
v.add(list[i].getParent());
v.add("A Directory");
v.add("directory");
Date date=new Date(list[i].lastModified());
v.add(date.toString());
model.insertRow(count,v);
count++;
}
addDataInTable(dts,list[i].getPath());
}
else
{
if(list[i].getName().contains(dts))
{
v=new Vector();
v.add(list[i].getName());
v.add(list[i].getParent());
v.add(""+(list[i].length()/1024)+"KB");
v.add("file");
Date date=new Date(list[i].lastModified());
v.add(date.toString());
model.insertRow(count,v);
count++;
}
}
}
}
public static void main(String... s)
{
new Search();
}
}
//BackgroundPanel.java
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class BackgroundPanel extends JPanel
{
Image image;
public BackgroundPanel()
{
try
{
image = new ImageIcon("back2.PNG").getImage();
}
catch (Exception e) { /*handled in paintComponent()*/ }
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
super.paintComponent(g);
if (image != null)
g.drawImage(image,0,0,null);
}
}
SearchTool
code::
//Search.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
class Search implements ActionListener,Runnable
{
JFrame mainFrame;
JPanel rightPanel,mainPanel;
JPanel leftPanel;
JTextField tf1,tf2;
JButton jb;
int sw;
int sh;
JTable table;
DefaultTableModel model;
JScrollPane sp;
static int count=0;
Vector
JComboBox cb;
JButton up,down;
JLabel temp;
JButton browse;
JCheckBox specific;
String pathtolook="";
JLabel dynamic;
ImageIcon icon;
JLabel progresslb;
JLabel imglb;
ImageIcon i;
Search()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
sw=d.width;
sh=d.height;
mainFrame=new JFrame("Search");
leftPanel=new BackgroundPanel();
rightPanel=new JPanel();
jb=new JButton("search");
cb=new JComboBox();
File roots[]=File.listRoots();
for(int i=0;i
//cb.getEditor().getEditorComponent().setBackground(Color.gray);
Vector
v.add("Name");
v.add("Directory");
v.add("Size");
v.add("Type");
v.add("Date Modified");
String col[]={"Name","Directory","Size","Type","DataModified"};
sp=new JScrollPane();
table=new JTable();
model=new DefaultTableModel(col,0);
table.setModel(model);
//table.setShowGrid(false);
//table.setShowHorizontalLines(true);
//table.setShowVerticalLines(false);
sp.setViewportView(table);
model.setColumnIdentifiers(v);
leftPanel.setLayout(null);
JLabel lb1=new JLabel("All or Part Name of the File:");
lb1.setBounds(20,150,140,20);
leftPanel.add(lb1);
tf1=new JTextField();
tf1.setBounds(20,170,140,20);
leftPanel.add(tf1);
JLabel lb2=new JLabel("A word or Phrase in the File:");
lb2.setBounds(20,200,140,20);
leftPanel.add(lb2);
tf2=new JTextField();
tf2.setBounds(20,220,140,20);
leftPanel.add(tf2);
JLabel lb3=new JLabel("Look In:");
lb3.setBounds(20,250,140,20);
leftPanel.add(lb3);
cb.setBounds(20,270,140,20);
leftPanel.add(cb);
jb.setBounds(55,310,80,30);
leftPanel.add(jb);
i=new ImageIcon("sd.gif");
imglb=new JLabel(i);
imglb.setBounds(70,400,71,74);
leftPanel.add(imglb);
browse=new JButton("Specific folder search");
browse.setEnabled(false);
//browse.setContentAreaFilled(false);
browse.setBounds(10,500,150,20);
leftPanel.add(browse);
browse.addActionListener(new BrowseListener());
specific=new JCheckBox("Specific Search");
specific.addActionListener(new BrowseListener());
specific.setBounds(30,350,100,20);
leftPanel.add(specific);
specific.setContentAreaFilled(false);
icon=new ImageIcon("pb1.gif");
progresslb=new JLabel(icon);
leftPanel.setBounds(0,0,180,sh-60);
rightPanel.setBounds(180,0,sw-180,sh-60);
sp.setBounds(0,0,sw-185,sh-60);
//leftPanel.setBackground(Color.gray);
rightPanel.setBackground(Color.white);
rightPanel.setLayout(null);
rightPanel.add(sp);
mainPanel=new JPanel();
mainPanel.setLayout(null);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
//mainFrame.setLayout(null);
mainFrame.add(mainPanel);
mainFrame.setSize(sw,sh-30);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb.addActionListener(this);
}
class BrowseListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfilechooser = new JFileChooser();
jfilechooser.setFileSelectionMode(1);
if(e.getSource() == browse)
{
int i = jfilechooser.showOpenDialog(null);
if(i == 0)
{
pathtolook=jfilechooser.getSelectedFile().getAbsolutePath();
dynamic=new JLabel("Look In: "+jfilechooser.getSelectedFile().getAbsolutePath());
dynamic.setBounds(5,550,200,20);
leftPanel.add(dynamic);
leftPanel.updateUI();
}
}
if(e.getSource()==specific)
{
if(specific.isSelected())
{
cb.setEnabled(false);
browse.setEnabled(true);
}
else
{
cb.setEnabled(true);
browse.setEnabled(false);
leftPanel.remove(dynamic);
leftPanel.updateUI();
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(tf1.getText().equals("") && tf2.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter the filename to search", "What to search", 0);
}
else
{
progresslb.setBounds(10,600,160,13);
leftPanel.add(progresslb);
leftPanel.updateUI();
i=new ImageIcon("searchdog.gif");
imglb.setIcon(i);
if(specific.isSelected())
{
new Thread(this).start();
}
else
{
pathtolook=""+cb.getSelectedItem();
new Thread(this).start();
}
}
jb.setEnabled(false);
}
public void run()
{
addDataInTable(tf1.getText().equals("")?tf2.getText():tf1.getText(),pathtolook);
icon =new ImageIcon("pb2.gif");
progresslb.setIcon(icon);
i=new ImageIcon("sd.gif");
//imglb.setBounds(70,400,71,74);
imglb.setIcon(i);
JOptionPane.showMessageDialog(null,"Search Complete\n No. of Files and Directories found:"+count,"Complete",JOptionPane.PLAIN_MESSAGE);
}
public void addDataInTable(String dts,String rts)
{
File f=new File(rts);
File[] list=f.listFiles();
for(int i=0;i
if(list[i].isDirectory() && !list[i].isHidden())
{
if(list[i].getName().contains(dts))
{
v=new Vector
v.add(list[i].getName());
v.add(list[i].getParent());
v.add("A Directory");
v.add("directory");
Date date=new Date(list[i].lastModified());
v.add(date.toString());
model.insertRow(count,v);
count++;
}
addDataInTable(dts,list[i].getPath());
}
else
{
if(list[i].getName().contains(dts))
{
v=new Vector
v.add(list[i].getName());
v.add(list[i].getParent());
v.add(""+(list[i].length()/1024)+"KB");
v.add("file");
Date date=new Date(list[i].lastModified());
v.add(date.toString());
model.insertRow(count,v);
count++;
}
}
}
}
public static void main(String... s)
{
new Search();
}
}
//BackgroundPanel.java
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class BackgroundPanel extends JPanel
{
Image image;
public BackgroundPanel()
{
try
{
image = new ImageIcon("back2.PNG").getImage();
}
catch (Exception e) { /*handled in paintComponent()*/ }
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
super.paintComponent(g);
if (image != null)
g.drawImage(image,0,0,null);
}
}
SearchTool