All pastes #188897 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-10-02 16:59 UTC
rendered paste body
package lab5;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ArrayView
{
	/**
	 * Sets up the layouts of the GUI, and populates it.
	 * Sets the dimensions of the GUI using <code>pack</code>
	 * and centers the frame on the screen. Sets the
	 * closing operation to terminate the program.
	 *
	 * @param model
	 */
	public ArrayView(ArrayData model) {
		this.model = model;
		frame.setLayout(new BorderLayout());
		leftPanel.setLayout(new GridLayout(0,1));
		for (int i = 0; i < fields.length; i++)
		{
			fields[i] = new JTextField(20);
			leftPanel.add(fields[i]);
		}

		centerPanel.add(sumField);
		centerPanel.add(sumButton);
		frame.add(leftPanel, BorderLayout.LINE_START);
		frame.add(centerPanel);
		// use a separate method to add listeners to
		// the fields and button
		setUpListeners();
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	/**
	 * Attach listeners to the active elements of the
	 * GUI.
	 */
	private void setUpListeners()
	{
		ActionListener temp;
		for (int i = 0; i < fields.length; i++)
		{
			temp = new FieldListener(i);
			fields[i].addActionListener(temp);
		}
		temp = new SumListener();
		sumButton.addActionListener(temp);
	}
	/**
	 * If the text in the array element fields[i] represents
	 * a number, the number is extracted from the text using
	 * a Scanner object and copied to the corresponding
	 * element of model.storage.
	 * If the text is not a number, no change is made to
	 * model.storage and an error dialog is shown.
	 * @param i - an index of the array fields
	 */
	private void copyFieldToArray(int i)
	{
		String str = fields[i].getText();
		if (str.length() > 0)
		{
			Scanner scan = new Scanner(str);
			if (scan.hasNextDouble())
			{
				model.storage[i] = scan.nextDouble();
			}
			else
			{
				JOptionPane.showMessageDialog(frame, "Field " + i + " contains bad data");
			}
		}
	}
	/**
	 * Listener for each of the 10 text fields.
	 * When a number is typed into one of the text fields,
	 * the value is copied into the corresponding
	 * element of model.storage when the enter-key is pressed
	 * @author Leslie Lander
	 *
	 */
	private class FieldListener implements ActionListener
	{
		int index;
		FieldListener(int i)
		{
			index = i;
		}
		public void actionPerformed(ActionEvent arg0)
		{
			copyFieldToArray(index);
		}
	}
	/**
	 * Listener for the button labeled "SUM"
	 * When clicked the values in the text fields
	 * are written to the data array model.storage
	 * and the sum is calculated
	 * @author Leslie Lander
	 *
	 */
	private class SumListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			for (int i = 0; i < fields.length; i++)
			{
				copyFieldToArray(i);
			}
			double sum = model.arraySum();
			sumField.setText("" + sum);
		}
	}

	/**
	 * Displays the values in the array model.storage
	 * in the text fields of this view
	 *
	 */
	public void displayArray()
	{
		// Show the values in each element of the array
		// model.storage in text form in the corresponding
		// field of the view

		for(int i == 0; i < model.storage.length; i++)
		{
                     fields[i] = (String)model.storage[i];
                }
	}

	private JFrame frame = new JFrame("Viewer");
	private JPanel leftPanel = new JPanel();
	private JPanel centerPanel = new JPanel();
	private JTextField sumField = new JTextField(25);
	private JButton sumButton = new JButton("SUM");
	private ArrayData model;
	// normally we would not use a public field but
	// this will simplify the code in the driver
	// for this special exercise
	public JTextField[] fields = new JTextField[10];
}