Learning Java
Custom Search

Previous Next

Part A - File I/O - Build the Look

In the first step, we will build the GUI interface, or what our application will look like. The objects in the application window frame are 3 text fields each with a label, 1 multi-line text area, 1 scroll pane, and 4 buttons (1 added later). The length of each text field is 40 characters.

  • txtRequest / lblRequest --- to enter the arguments for which the search is to be performed. Watch carefully that an extra space is not placed after your search argument, which causes it to not work as you expect.
  • txtDirectory / lblDirectory --- to enter the directory under which the search is to occur
  • txtFileSel / lblFileSel --- to enter the list of file types to be searched. Although it was designed to specify the selection of files ending in any of these files extension, it also works on finding files with the values anywhere in the name.
  • A multi-line text area, txtResults, to contain the results of the search.
  • A scroll pane, scrollp, to allow scrolling within the text area, txtResults.
  • btnSearch - to start the search once the needed information has been entered.
  • btnClear  - to clear the contents of the text area, txtResults.
  • btnExit   - to exit the program.

The application is called Searcher and is extended from the class JFrame as the outermost container. It contains the 2 methods Searcher which is its constructor and main which is where it gets instantiated with

    Searcher doSearch = new Searcher();

In the constructor method, we use super to give the Frame the title "File Search Application". We use setSize to give it a width of 475 and a height of 500. We call setDefaultCloseOperation so that the application is stopped whenever the window is closed using the X box in the upper right corner. The main container will be a panel called pane which will use the FlowLayout to place its objects. The multi-line text area called txtResults will have to be placed inside of the scroll pane, and then the scroll pane is added to pane, not txtResults. All the other objects are added to pane in the desired order before making pane visible.

The following is a listing of what this code might look like.

import java.awt.*;
import javax.swing.*;

public class Searcher extends JFrame {
    JLabel lblRequest = new JLabel("Enter search argument:");
    JTextField txtRequest = new JTextField(" ", 40);
    JLabel lblDirectory = new JLabel("Enter the directory:");
    JTextField txtDirectory = new JTextField(" ", 40);
    JLabel lblFileSel = 
		new JLabel("Enter file extensions, separate with ;");
    JTextField txtFileSel = new JTextField(".txt;.java;.htm", 40);
    JLabel lblResults = new JLabel("Results:");
    JTextArea txtResults = new JTextArea(15, 40);

    JButton btnSearch = new JButton("Search");
    JButton btnClear = new JButton("Clear");
    JButton btnExit = new JButton("Exit");

    public Searcher() {
        super("File Search Application");
        setSize(475, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT);
        pane.setLayout(flow1);
	JScrollPane scrollp = new JScrollPane(txtResults, 
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        pane.add(lblRequest);
        pane.add(txtRequest);
        pane.add(lblDirectory);
        pane.add(txtDirectory);
        pane.add(lblFileSel);
        pane.add(txtFileSel);
        pane.add(lblResults);
	pane.add(scrollp);
        pane.add(btnSearch);
        pane.add(btnClear);
        pane.add(btnExit);
        setContentPane(pane);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        Searcher doSearch = new Searcher();
    }
}

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved