Learning Java
Custom Search

Previous Next

Part B - File I/O - Listen for Button Clicks

In this part, we will add code to perform events when our buttons are clicked. We must implement the interface that contains our action routines in our class declaration. To use this interface we will need to import java.awt.event.*

    import java.awt.event.*;
    public class Searcher extends JFrame implements ActionListener {

We will add a listener to each of our button components so that an event is generated when the component is used.

    btnSearch.addActionListener(this);
    btnClear.addActionListener(this);
    btnExit.addActionListener(this);

Since we associated the ActionListener interface with our Searcher class, we have to handle all the methods in that interface. The ActionListener interface only has one method, actionPerformed(). So in our class, we override the actionPerformed() method. We get the source of the event with the getSource() method. Then we can test to see which of our buttons is the source of the event. If it was btnExit, we close the application using system.exit(0). If it was btnClear, we want to clear the text area txtResult. We will add in code to process btnSearch later.

We will add String str1 at the top of class so that it can be used anywhere in the code. It will accumulate what goes in txtResults and we will need to clear it in the action for btnClear.

    String str1 = "";
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == btnExit) {
	    System.exit(0);
	} else if (source == btnClear) {
	    txtResults.setText("");
	    return;
	}  
    }

Compile and test what we have so far.

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

public class Searcher extends JFrame  implements ActionListener {
    String str1 = "";
    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);
	btnSearch.addActionListener(this);
	btnClear.addActionListener(this);
	btnExit.addActionListener(this);
        pane.add(btnSearch);
        pane.add(btnClear);
        pane.add(btnExit);
        setContentPane(pane);
        setVisible(true);
    }

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

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == btnExit) {
	    System.exit(0);
	} else if (source == btnClear) {
	    txtResults.setText("");
	    str1 = "";
	    return;
	}  
    }
}

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved