Chris Pollett >
Old Classes >
PIC20B

   ( Print View )

Enrollment info

Course Info: Homework Assignments: Practice Exams: PIC:
                            












HW1 Solutions Page

Return to homework page.


//
// Filename: Hw1.java
//
// Purpose: This program launches a JFrame with a source, expression, and
//                 matches textareas. When the user types into the source area
//                 some words, enters an expression in the expression area
//                 clicks check, those words which contain the expression are
//                 outputted to the matches area. Clicking reset erases the
//                 fields.

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

public class Hw1 extends JFrame
{
	private Container c;
	private JPanel buttonPanel, textPanel;
	private JTextArea source, matches;
	private JTextField expression;
	private JButton reset, check;

	//
	// Method Name: Hw1()
	//
	// Purpose: Constructor. Sets up the components within the JFrame.
	//

	public Hw1()
	{
		super("HW1");

		c=getContentPane();
		c.setLayout(new BorderLayout());
		c.setBackground(Color.white);

		textPanel =new JPanel();
		textPanel.setLayout(new GridLayout(6,1,5,5));

		textPanel.add(new JLabel("Source Text:"));

		source = new JTextArea(20,10);
		textPanel.add(new JScrollPane(source));

		textPanel.add(new JLabel("Expression:"));

		expression= new JTextField(20);
		textPanel.add(expression);


		textPanel.add(new JLabel("Matches:"));

		matches = new JTextArea(20,10);
		matches.setEditable(false); //user cannot change matches area 		textPanel.add(new JScrollPane(matches));
		c.add(textPanel, BorderLayout.CENTER);

		reset = new JButton("Reset");
		reset.addActionListener( new ActionListener ()
		      { // listener causes textareas to be erased when reset
		        // when reset button is clicked

			public void actionPerformed(ActionEvent e)
			  {
				source.setText("");
				expression.setText("");
				matches.setText("");
			  }
		       }
		);

		 check= new JButton("Check");
		 check.addActionListener( new CheckListener());
			//CheckListener will do the matching

		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.add(reset);
		buttonPanel.add(check);
		c.add(buttonPanel,BorderLayout.SOUTH);

		setSize(250,350);
		show();

	}

	//
	// Method Name:  main()
	//
	// Purpose: application entry point. Calls Hw1 constructor
	//                 and sets up JFrame. Sets up listener to listen
	//                 for close window events.

	public static void main (String args[])
	{
		Hw1 app = new Hw1();

		app.addWindowListener(
			new WindowAdapter()
                                          {
			public void windowClosing ( WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
	}

	//
	// Inner Class Name:  CheckListener
	//
	// Purpose:  Handles events from the Check button.
	//                 When an event is received. Its acctionPerformed
	//                 method scans the words  in the Source area adding
	//                 those words which  match the expression text to a StringBuffer.
        //                 Finally, it output this StringBuffer to the
	//                 matches area.

	private class CheckListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String expr = expression.getText();
			StringTokenizer words =
				new StringTokenizer(source.getText());
			StringBuffer output= new StringBuffer();
			String ckWord;

			while(words.hasMoreTokens())
			{
			    ckWord = words.nextToken();
			    if(ckWord.indexOf(expr)  >= 0)
			             output.append(ckWord+"\n");
			}
			matches.setText(output.toString());
 		}
	}
}