cs106a – Assignment #6 – Task #1

The complete specification of assignment #6 can be found as part of the stream at iTunes.

Assemble the GUI interactors

If you look at the bottom of Figure 1, you will see that the region along the SOUTH edge of the window contains several interactors: a JLabel, a JTextField, and three JButtons. Since putting up interactors is something you haven’t done in previous assginments, you probably want to work on this step before it becomes complicated with all the other parts of the assignment. Thus, your first milestone is simply to add the interactors to the window and create an implementation for the actionPerformed method that allows you to check whether you can detect button clicks and read what’s in the text field.
The simplest strategy to check whether your program is working is to change the definition of the NameSurfer class so that it extends ConsoleProgram instead of Program, at least for the moment. You can always change it back later. Once you have made that change, you can then use the console to record what’s happening in terms of the interactors to make sure that you’ve got them right. For example, the figure below shows a possible transcript of the commands used to generate the output from Figure 1, in which the user has just completed the following actions:

  1. Entered the name Sam in the text field and clicked the Graph button.
  2. Entered the name Samantha in the text field and then typed the ENTER key.
  3. Clicked the Clear button.

The hard part about reaching this milestone is understanding how interactors work. Once you do, writing the code is quite straightforward – it’s only 10 to 15 lines of code.

cs106a – assignment #6 – task #1

As described above change the parent of the class to ConsoleProgram:

public class NameSurfer extends ConsoleProgram implements NameSurferConstants {

Because the text field and the buttons need to be access from several methods, create them as instance variables, and set them um in the init() method. The action listener for the text field needs to be called directly on the text-field object, for the buttons the global listener is called after the buttons have been added:

	private JTextField nameField;
	private JButton graphButton;
	private JButton clearButton;
	
	public void init() {
		nameField = new JTextField(N_CHARS_NAMEFIELD);
		nameField.addActionListener(this);
		graphButton = new JButton("Graph");
		clearButton = new JButton("Clear");
	   
		add(new JLabel("Name:"), POSITION_OF_INTERACTORS);
		add(nameField, POSITION_OF_INTERACTORS);
		add(graphButton, POSITION_OF_INTERACTORS);
		add(clearButton, POSITION_OF_INTERACTORS);
	    
		addActionListeners();
	}

Depending on what action has triggered an event print a suitable message:

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if ((source == nameField) || (source == graphButton)) {
			println("Graph: \"" + nameField.getText() + "\"");
		} else if (source == clearButton) {
			println("Clear");
		}
	}

To be able to change the position of the interactors quickly or change the size of the text field, use constants:

	private static final String POSITION_OF_INTERACTORS = NORTH;
	private static final int N_CHARS_NAMEFIELD = 20;

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *