The document provides instructions to modify the PersonEntry class to:
1) Update the interface to match a provided figure
2) Make the close button hide the form when clicked
3) Validate data entry and save to the person list when the save button is clicked
It gives hints on how to:
- Add a label and checkbox to the interface
- Store a reference to access and hide the form
- Validate data is entered and is the right type before saving
2. Section 2. Implementing functionality in the PersonEntry. (1.5 ma.pdf
1. 2. Section 2. Implementing functionality in the PersonEntry. (1.5 marks)
a. Modify the PersonEntry object so that the interface looks like that presented in Figure 3
Hints:
i. You may need to add a JLabel and a JCheckBox
ii. The layout from the previous developer for the panel displays data on two rows.
b. Add functionality to the Close button of the PersonEntry form so that it is no longer visible
when it is clicked.
Hint: one way to do it is to
i. Store a reference to a PersonEntry as an instance variable
ii. Set the instance of PersonEntry to this in the constructor
iii. In the appropriate listener, invoke setVisible on the instance of PersonEntry with an
argument of false.
c. Implement functionality in the Save button of PersonEntry so that when it is clicked, the data
is first validated to ensure both first and last names have been entered, and that the age is
an integer. If data is valid, the addPerson method of PersonListing is called, and the person
entry form disappears. Note that the addPerson method effectively adds a person to
the arraylist holding person list data, and updates the data in the list.
Hints/Questions
i. Have we already met a method to split a string into parts?
ii. What functionality can tell if a value can be converted to an Integer?
iii. Remember the getText() method of a jTextField returns the value of the text it currently
displays.
starter code
public abstract class BasePerson {
protected String name;
private boolean publish;
private int age;
private int id;
public BasePerson(String name,int age, boolean pub)
{
this.name =name;
this.age=age;
publish = pub;
2. }
public int getAge()
{
return age;
}
public abstract String getName();
protected void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public boolean getPublish()
{
return publish;
}
}
public class Person extends BasePerson implements Comparable
{
private static int nextId=0;
public Person(String name, int age, boolean willPublish)
{
super(name, age, willPublish);
super.setId(nextId);
nextId++;
}
public String getName()
{
return name;
3. }
public static String getPHeader()
{
String returnval = "IDtNametAgetWillPublish";
returnval+="n---------------------------------";
return returnval;
}
public String toString()
{
return(getId()+"t"+getName()+"t"+getAge()+"t"+getPublish());
}
public static void resetId()
{
nextId=0;
}
public int compareTo(Person other)
{
return other.getId() - this.getId();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class PersonEntry extends JFrame
{
private JTextField txtName; //name
private JTextField txtAge; //age
4. private JButton cmdSave;
private JButton cmdClose;
private JButton cmdClearAll;
private JPanel pnlCommand;
private JPanel pnlDisplay;
public PersonEntry()
{
setTitle("Entering new person");
pnlCommand = new JPanel();
pnlDisplay = new JPanel();
pnlDisplay.add(new JLabel("Name:"));
txtName = new JTextField(20);
pnlDisplay.add(txtName);
pnlDisplay.add(new JLabel("Age:"));
txtAge = new JTextField(3);
pnlDisplay.add(txtAge);
pnlDisplay.setLayout(new GridLayout(2,4));
cmdSave = new JButton("Save");
cmdClose = new JButton("Close");
pnlCommand.add(cmdSave);
pnlCommand.add(cmdClose);
add(pnlDisplay, BorderLayout.CENTER);
add(pnlCommand, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
import javax.swing.JFrame;
6. thisForm = this;
pnlCommand = new JPanel();
pnlDisplay = new JPanel();
plist= loadPersons("person.dat");
String[] columnNames= {"First Name",
"Last Name",
"Age",
"Will Publish"};
model=new DefaultTableModel(columnNames,0);
table = new JTable(model);
showTable(plist);
table.setPreferredScrollableViewportSize(new Dimension(500, plist.size()*15 +50));
table.setFillsViewportHeight(true);
scrollPane = new JScrollPane(table);
add(scrollPane);
cmdAddPerson = new JButton("Add Person");
cmdSortAge = new JButton("Sort by Age");
cmdClose = new JButton("Close");
cmdClose.addActionListener(new CloseButtonListener());
pnlCommand.add(cmdAddPerson);
pnlCommand.add(cmdClose);
add(pnlCommand);
}
7. private void showTable(ArrayList plist)
{
if (plist.size()>0)
addToTable(plist.get(0));
}
private void addToTable(Person p)
{
String[] name= p.getName().split(" ");
String[] item={name[0],name[1],""+ p.getAge(),""+p.getPublish()};
model.addRow(item);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("List of persons who are requesting a vaccine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
PersonListing newContentPane = new PersonListing();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
8. });
}
public void addPerson(Person p)
{
plist.add(p);
addToTable(p);
}
private ArrayList loadPersons(String pfile)
{
Scanner pscan = null;
ArrayList plist = new ArrayList();
try
{
pscan = new Scanner(new File(pfile));
while(pscan.hasNext())
{
String [] nextLine = pscan.nextLine().split(" ");
String name = nextLine[0]+ " " + nextLine[1];
int age = Integer.parseInt(nextLine[2]);
boolean publish = false;
if (nextLine[3].equals("0"))
publish = false;
else
publish =true;
Person p = new Person(name, age, publish);
plist.add(p);
}
pscan.close();
}
catch(IOException e)
{}
return plist;
}
private class CloseButtonListener implements ActionListener