The complete specification of assignment #7 can be found as part of the stream at iTunes.
Assemble the GUI interactors
As seen in the initial start-up screen of the application (shown below), there are a number of interactors (JLabels, JTextFields, and JButtons) in both the NORTH and WEST border regions of the application.
Similar to the NameSurfer assignment, your first milestone is simply to add the interactors to the application 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 fields. Since you’ve already had experience doing that in the previous assignment, this milestone hopefully won’t present many new challenges.
A few specific issues to note in the implementation of these interactors are the following:
- All text fields are TEXT_FIELD_SIZE characters wide. TEXT_FIELD_SIZE is just a
constant set in FacePamphletConstants.- The Name text field in the NORTH region does not have any actionCommand associated with it. In other words, pressing the Enter key in that text field should have no effect, so you don’t need to worry about that case.
- The three text fields in the WEST region do have actionCommands associated with them. The actionCommand associated with each respective text field should be the same as its corresponding button. For example, pressing the Enter key in the text fieldnexttotheChange Statusbuttonshouldhavethesameeffectaspressingthe Change Status button.
- If a text field is empty when its corresponding button is pressed, then nothing should happen. For example, if the Name text field in the NORTH region has nothing in it when the Add (or Delete, or Lookup) button is clicked (i.e., the text field’s value is the empty string (“”)), then we should simply not do anything as a result of the button click. This idea applies to all text fields in the application, and helps prevent situations such as trying to add a profile with an empty name, or trying to change the status of a profile to the empty string.
One issue to note is that in laying out the interactors in the WEST border region, you’ll notice that there are spaces between the various text field/button pairs (for example, there is space between the Change Status button and the text field associated with Change Picture). These spaces should be produced by adding a JLabel with the label text EMPTY_LABEL_TEXT (this is just a constant defined in FacePamphletConstants) at the appropriate points when adding interactors to the WEST border region. So, your interactor layout code will likely include two lines at various points that look something like this:
add(new JLabel(EMPTY_LABEL_TEXT), WEST);
Add the instance variables needed for the actions:
private JTextField nameField; private JButton addButton; private JButton deleteButton; private JButton lookupButton; private JTextField statusField; private JButton statusButton; private JTextField pictureField; private JButton pictureButton; private JTextField friendField; private JButton friendButton;
Initialize them and put them on screen together with the (blind) labels:
public void init() { add(new JLabel("Name:"), NORTH); nameField = new JTextField(TEXT_FIELD_SIZE); add(nameField, NORTH); addButton = new JButton("Add"); add(addButton, NORTH); deleteButton = new JButton("Delete"); add(deleteButton, NORTH); lookupButton = new JButton("Lookup"); add(lookupButton, NORTH); statusField = new JTextField(TEXT_FIELD_SIZE); statusField.addActionListener(this); add(statusField, WEST); statusButton = new JButton("Change Status"); add(statusButton, WEST); add(new JLabel(EMPTY_LABEL_TEXT), WEST); pictureField = new JTextField(TEXT_FIELD_SIZE); pictureField.addActionListener(this); add(pictureField, WEST); pictureButton = new JButton("Change Picture"); add(pictureButton, WEST); add(new JLabel(EMPTY_LABEL_TEXT), WEST); friendField = new JTextField(TEXT_FIELD_SIZE); friendField.addActionListener(this); add(friendField, WEST); friendButton = new JButton("Add Friend"); add(friendButton, WEST); addActionListeners(); }
Check which action has been triggered and – if there is something to do and the the corresponding text field is not empty – print an appropriate text:
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == addButton) { if (!emptyTextField(nameField)) { println("Add: " + nameField.getText()); } } else if (source == deleteButton) { if (!emptyTextField(nameField)) { println("Delete: " + nameField.getText()); } } else if (source == lookupButton) { if (!emptyTextField(nameField)) { println("Lookup: " + nameField.getText()); } } else if ((source == statusField) || (source == statusButton)) { if (!emptyTextField(statusField)) { println("Change Status: " + statusField.getText()); } } else if ((source == pictureField) || (source == pictureButton)) { if (!emptyTextField(pictureField)) { println("Change Picture: " + pictureField.getText()); } } else if ((source == friendField) || (source == friendButton)) { if (!emptyTextField(friendField)) { println("Add Friend: " + friendField.getText()); } } } private boolean emptyTextField(JTextField field) { return field.getText().equals(""); }
The code for this assignment is available on github.