Creating the User Interface for the File Search Application


Creating the User Interface for the File Search Application

The Search.java file helps you create a user interface with a set of text boxes, labels, and buttons for the File Search application. End users can use this interface to specify a location and text pattern to search for specific files. The right pane of the File Search application interface contains an empty space where the file search results are displayed to the end user.

Listing 4-1 shows the contents of the Search.java file:

Listing 4-1: The Search.java File
start example
 /* Imports the required I/O classes. */ import java.io.File; /* Imports the required Util classes. */ import java.util.Vector; /* Imports the required AWT classes */ import java.awt.event.*; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Font; import java.awt.Color; import java.awt.*; /* Imports the required Swing classes */ import javax.swing.*; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableModel;  /* class Search - This is the main class of the application. This class initializes the  interface and loads all components, such as table, before displaying the result. Methods: listFiles() - This method searches for files that meet the specified search criteria. Usage() - This method displays an error message. Main() - This method creates the main window of the application and displays all the components and results. */ public class Search extends JFrame implements ActionListener, Runnable {    /* Declare objects of the JLabel class. */    JLabel labelText;    JLabel labelLook;    JLabel labelSearch;    JLabel labelResult;    /* Declare objects of the JTextField class. */    JTextField textText;    JTextField textLook;    /* Declare objects of the JButton class. */    JButton search;    JButton cancel;    JButton browse;    JButton help;    /* Declare objects of the JPanel class. */    JPanel panel;    JPanel paneLeft;    JPanel paneRight;    JPanel pane1;    JPanel pane2;    JPanel pane3;    JPanel pane4;    JPanel pane5;    JPanel p1;    JPanel p2;    JPanel p3;    /* Declare object of the JScrollPane class. */    JScrollPane scrollpane;    /* Declare String objects. */    static String strText;    static String strLook;    /* Declare an object of the JTable class. */    JTable table;    /* Declare and initialize a counter.*/    int count = 0;    int FLAG = 0;    /*    Declare and initialize an object of the Object class.    */    Vector vRow = new Vector();    Vector vCol = new Vector();    public FileList fl = new FileList(this);    public Help h;    /* Declare arrays of the String class.*/    DefaultTableModel model;    Thread thread;    int countFile;    /* Implement a constructor of the Search class. */    public Search()    {       try       {          /*          Initialize and set the look and feel of the application to Windows look and feel.          */          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");       }       catch(Exception e)       {          /*          If an error occurs while loading the Windows look and feel, an error is           displayed and the application is closed.          */          usage();       }       /* Set the size of the application frame. */       setSize(800, 600);       /* Set the Title of the application frame. */       setTitle("File Search Utility");       /*       Initialize a panel, containing two subpanels, for the application frame.       */       panel = new JPanel();       /* Set the layout of the frame as Grid layout. */       panel.setLayout(new GridLayout(0, 2));       /* Add the panel to the frame. */       getContentPane().add(panel);       /*       Initialize the other two subpanels.       Add the first subpanel (paneLeft) to the left side of the main panel.       Add the second subpanel (paneRight) to the right side of the main panel.       */       paneLeft = new JPanel();       paneRight = new JPanel();       panel.add(paneLeft);       panel.add(paneRight);       /*       Set the layout of the left subpanel as Border layout.       Initialize a new Grid panel.       Set the layout of the new panel as Grid layout.       Add this panel to the left subpanel.       */       paneLeft.setLayout(new BorderLayout());       pane5 = new JPanel();       pane5.setLayout(new FlowLayout(FlowLayout.RIGHT));       help = new JButton("Help");       help.addActionListener(this);       pane5.add(help);       paneLeft.add(pane5, BorderLayout.SOUTH);       /*       Set the layout of the left subpanel as Border layout.       Initialize a new Grid panel.       Set the layout of the new panel as Grid Layout.       Add this panel to the left subpanel.       */       pane3 = new JPanel();       pane3.setLayout(new GridLayout(6,0));       paneLeft.add(pane3, BorderLayout.NORTH);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize a new label and add this label to the panel.       Add this panel to the first grid of the new Grid panel.       */       p1 = new JPanel();       p1.setLayout(new FlowLayout(FlowLayout.LEFT));       labelLook = new JLabel("Search for file(s) in:");       p1.add(labelLook);       labelLook.setFont(new Font("Verdana",Font.BOLD,12));       pane3.add(p1);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize a text field and add it to the panel.       Initialize a button and add it to the panel.       Add this panel to the second grid of the new Grid panel.       */       pane1 = new JPanel();       pane1.setLayout(new FlowLayout(FlowLayout.LEFT));       textLook = new JTextField(25);       browse = new JButton("Browse");       browse.addActionListener(this);       pane1.add(textLook);       pane1.add(browse);       textLook.setFont(new Font("Verdana",Font.PLAIN,12));       pane3.add(pane1);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize a new label and add this label to the panel.       Add this panel to the third grid of the new Grid panel.       */       p2 = new JPanel();       p2.setLayout(new FlowLayout(FlowLayout.LEFT));       labelText = new JLabel("Containing text:");       p2.add(labelText);       labelText.setFont(new Font("Verdana",Font.BOLD,12));       pane3.add(p2);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize a text field and add it to the panel.       Add this panel to the fourth grid of the new Grid panel.       */       p3 = new JPanel();       p3.setLayout(new FlowLayout(FlowLayout.LEFT));       textText = new JTextField(33);       p3.add(textText);       textText.setFont(new Font("Verdana",Font.PLAIN,12));       pane3.add(p3);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize buttons and add these buttons to the panel.       Add this panel to the fifth grid of the new Grid panel.       */       pane2 = new JPanel();       pane2.setLayout(new FlowLayout(FlowLayout.LEFT));       search = new JButton("Search");       cancel = new JButton("Cancel");       pane2.add(search);       search.addActionListener(this);       pane2.add(cancel);       cancel.addActionListener(this);       pane3.add(pane2);       /*       Initialize a new panel.       Set the layout of this panel as Flow layout.       Initialize buttons and add these buttons to the panel.       Add this panel to the fifth grid of the new Grid panel.       */       pane4 = new JPanel();       pane4.setLayout(new FlowLayout(FlowLayout.LEFT));       labelSearch = new JLabel("Search Result(s): ");       labelSearch.setFont(new Font("Verdana",Font.BOLD,12));       labelResult = new JLabel("0 files.");       labelResult.setFont(new Font("Verdana",Font.BOLD,12));       pane4.add(labelSearch);       pane4.add(labelResult);       pane3.add(pane4);       /*       Set the layout of the right subpanel as Border layout.       Initialize an object of the table.       Initialize an object of the scroll panel.       Add the table to the scroll panel.       Add this scroll panel to the right subpanel.       */       paneRight.setLayout(new BorderLayout());       model = new DefaultTableModel();          vCol.addElement("File Name");       vCol.addElement("Start Position");       vCol.addElement("Path");       vCol.addElement("Size");       model = new DefaultTableModel(vRow, vCol);       table = new JTable(model);       scrollpane = new JScrollPane(table);       paneRight.add(scrollpane, BorderLayout.CENTER);       /* Pack the components of the frame. */       doLayout();       pack();       /*       addWindowListener: It contains the windowClosing() method.       windowClosing: It is called when the end user clicks the Cancel button on the window.        It closes the main window.       we parameter: An object of the WindowEvent class.       Return Value: NA       */       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             System.exit(0);          }       });       }    /*    actionPerformed(): This method is called when the end user clicks the Browse,     Search, or Cancel button.    ae parameter: An ActionEvent object that contains information about the event.    Return Value: NA    */    public void actionPerformed(ActionEvent ae)    {       /*       This is executed when an end user clicks the Search button.       */       if(ae.getSource() == search)       {          countFile = 0;          count = 0;          FLAG = 0;          model.setNumRows(0);          strText = textText.getText();          strLook = textLook.getText();          /*          If both the text field have the required text, the actionPerformed() method creates           a new instance of the Thread class, which further calls the listFiles() method.          */          if(strText.equals("") == false && strLook.equals("") == false)          {             try             {                thread = new Thread(this);                thread.start();                   }             catch(Exception e)             {           System.out.println("Error!" + e);             }          }          /*          Otherwise, display an Error message box.          The Error message box is displayed by calling the showMessageDialog()           method of the JOptionPane class.          showMessageDialog(): It is called by the system automatically, when required.          Parameter:          Parent Name: Name of the parent frame.          Display Message: Text to be displayed on the message box.          Title: Sets the title of the message box.          Message Box Option: Type of message box.          Return Value: NA          */          else          {             JOptionPane.showMessageDialog(this, "You must enter the searching text.",              "Alert Message", JOptionPane.WARNING_MESSAGE);          }       }       /*       This is executed when the end user clicks the Search button.       */       else if (ae.getSource() == browse)       {                    try          {             /* Initialize an object of the JFileChooser class. */             JFileChooser jfc = new JFileChooser();             /* Set the mode of the FileChooser box. */             jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);             jfc.showOpenDialog(this);             /*             Get the selected file using the getSelectedFile() method.              */             File file = jfc.getSelectedFile();             /*             Retrieve the value of the absolute path of the file. Store the value into a string.             */             String str = file.getAbsolutePath();             textLook.setText(str);          }          catch(Exception e)           {             +System.out.println("Error" + e);             }       }       /*       This is executed when an end user clicks the Search button.       */       else if (ae.getSource() == cancel)       {          /*          This hides and closes all the components of the application.          */          search.setEnabled(true);          FLAG = 1;             }       /*       This is executed when an end user clicks the Help button.       */       else if (ae.getSource() == help)       {          /* It shows the help utility. */          h = new Help();          h.show();       }    }    public void run()    {       fl.listFiles(strLook);    }    /*    This is the main method that creates an instance of the Search class and shows the main frame.    */    public static void main(String[] args)     {            Search s = new Search();       s.show();          }    /*     A utility method to display the invocation syntax and exit.    */    public static void usage()     {          System.err.println("Error");         System.exit(1);     } } 
end example
 

Download this Listing .

In the above listing, the main() method creates an instance of the Search class. This class generates the main window of the File Search application, as shown in Figure 4-2:

click to expand: this figure shows the file search utility window with two text boxes, search for file(s) in and containing text, in the left pane, and an empty space in the right pane.
Figure 4-2: The File Search Application User Interface

The text boxes in the above figure enable an end user to specify the location and the text pattern to search for specific files. The empty space in the right pane of the window displays the results of the file search.

When an end user clicks any button on the File Search Utility window, the File Search application invokes the actionPerformed() method. This method acts as an event listener and activates an appropriate class, based on the button that the end user clicks.

When an end user clicks the Browse button adjacent to the Search for file(s) in text box, the actionPerformed() method creates an instance of the JFileChooser class to open the File dialog box. This dialog box enables the end user to browse to a specific location, where the File Search application should search for the desired files.

After specifying the search criteria, when an end user clicks the Search button, the actionPerformed() method creates an instance of the Thread class and calls the start() method of this class. The start() method further calls the run() method of the Thread class, which calls the listFiles() method to search for files that meet the specified criteria.

The Search.java file declares an object of the JTable class. This object creates a tabular format to display the search results, returned by the listFiles() method, to the end user.

The Cancel button on the File Search Utility window helps an end user stop the file search process before it is complete. When an end user clicks the Cancel button, the actionPerformed() method sets the value of the FLAG variable, declared in the Search class, to 1 and stops the running instance of the Thread class.

If an end user clicks the Help button, the actionPerformed() method creates an instance of the Help class and calls the show() method of this class. The show() method opens the Help window, which displays the sequence of steps to search for files using the File Search application.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net