FileSearch in Java with User Interface

FileSearch in Java with User Interface:
This Program will help you to search a particular file in a particular drive (c,d,e or f etc).

Java Program:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class FileSearch extends javax.swing.JFrame {

	/**
	 * Creates new form SimpleUI
	 */
	List myArrayList = new ArrayList();

	public FileSearch() {
		initComponents();
	}

	private void initComponents() {

		mainPanel = new javax.swing.JPanel();
		labelName = new javax.swing.JLabel();
		textName = new javax.swing.JTextField();
		textName1 = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		textPassword = new javax.swing.JPasswordField();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2,
				2, 2));

		labelName.setText("File Name: ");

		jLabel1.setText("File Path: ");

		jButton1.setText("Search");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(mainPanelLayout);
		mainPanelLayout
				.setHorizontalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								mainPanelLayout
										.createSequentialGroup()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(labelName)
														.addComponent(jLabel1))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(textName)
														.addComponent(
																textName1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																195,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addComponent(jButton1)
										.addGap(0, 65, Short.MAX_VALUE)));
		mainPanelLayout
				.setVerticalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								mainPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(labelName)
														.addComponent(
																textName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																textName1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jButton1))
										.addContainerGap(15, Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));

		pack();
	}

	/**
	 * This is the method that is called when an action is performed. Over here
	 * I just simply show an error message if any of the text fields are empty
	 * or just show their names.
	 */
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String name = textName.getText();
		String password = textName1.getText();
		String cool = "";
		if ("".equals(password) || "".equals(name)) {
			JOptionPane.showMessageDialog(this, "Name or password is empty!",
					"Incorrect Input", JOptionPane.ERROR_MESSAGE);
		} else {
			FileSearch sget = new FileSearch();
			List filenamesList = sget.findFile(name, new File(password));
			for (Object filenames : filenamesList) {
				cool = cool + "\n" + (String) filenames.toString();

			}
			JOptionPane.showMessageDialog(null, "The File exist in : " + cool,
					"Javadomain.in", 1);
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {

		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(FileSearch.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(FileSearch.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(FileSearch.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(FileSearch.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new FileSearch().setVisible(true);
			}
		});
	}

	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel labelName;
	private javax.swing.JPanel mainPanel;
	private javax.swing.JTextField textName;
	private javax.swing.JTextField textName1;
	private javax.swing.JPasswordField textPassword;

	public List findFile(String name, File file) {
		File[] list = file.listFiles();
		String filenames = null;

		if (list != null)
			for (File fil : list) {
				if (fil.isDirectory()) {
					findFile(name, fil);
				} else if (name.equalsIgnoreCase(fil.getName())) {
					myArrayList.add(fil.getParentFile());
				}
			}
		return myArrayList;
	}
}

Output:

getting input filesearch

filesearch result

Download Jar:
FileSearch

Recommended Books:

325 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *