Home / Expert Answers / Computer Science / 1-section1-populating-the-list-of-persons-on-the-person-listing-form-and-put-functionality-in-pa396

(Solved): 1. Section1.- Populating the list of persons on the person listing form, and put functionality in ...



After the initial deployment of Vaccinator Prime(see Lab 5 description), it has become evident that a large number of users mLAB EXERCISES (5 MARKS FOR DEMONSTRATED FUNCTIONALITY) Java code for four classes are provided. The classes are:
- BasePersonFigure 2. Result of executing starting code for PersonEntry.
RADEABLE ACTIVITIES
1. Section1.- Populating the list of personsiii. In the appropriate listener, invoke setVisible on the instance of PersonEntry with an argument of false.
c. Implement fu

1. Section1.- Populating the list of persons on the person listing form, and put functionality in the “Add Person” button (1 mark)

a. In PersonListing: The starting code places only one item in the list of persons. Discussions with the original developer have yielded that the method “showTable” in PersonListing is responsible for populating the table. The addToTable method which accepts one person and adds information regarding that person to the table should NOT be modified. Complete showTable so that an entry is placed in the table for each row in the file person.dat.

b. In PersonEntry: Modify PersonEntry so that it is able to store an instance of PersonListing.

c. In PersonEntry: Modify the Constructor of PersonEntry so that it accepts an instance of PersonListing, and then sets the local instance variable to the value accepted by the constructor.

d. In PersonListing: Add a listener to the “Add Person” button so that when it is clicked, an instance of PersonEntry is displayed. When invoking the instance of PersonEntry, a reference to the current instance of PersonListing should be passed as an argument to the constructor. //Hint… a listener is already included in the starting code that adds functionality to the Close button.

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.

3. Section 3 – Complete functionality on listing screen (1.5 marks)

a. The starting code includes the definition of a JButton named cmdSortAge, but it is not shown on the PersonListing screen. Modify the PersonListingScreen to allow it to be shown

b. Add a listener which implements the functionality to sort the list in ascending order of age. Hints:

i. You should already know how to sort data

ii. Presenting information in a specific order in the table may involve removing all the data and then re-adding it iii. Data in the table is removed with the command model.setRowCount(0);- model is an object name that was declared with the starting code. Ie. model is an object of the class DefaultTableModel.

c. Add a button and an associated listener to sort the list in order of first name .

4. Change the colors of the form to implement a custom colour scheme. (1 mark) Figure 4 shows an example of a custom colour scheme. You should come up with your own and then set the colours on the Person Listing screen appropriately.

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;

}

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<Person>

{

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;

}

public static String getPHeader()

{

String returnval = "ID\tName\tAge\tWillPublish";

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

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;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Scanner;

import java.util.ArrayList;

import java.io.File;

import java.io.IOException;

import javax.swing.JButton;

import javax.swing.table.*;

import javax.swing.table.DefaultTableModel;

import java.util.Comparator;

import java.util.Collections;

import java.awt.Color;

public class PersonListing extends JPanel {

private JButton cmdAddPerson;

private JButton cmdClose;

private JButton cmdSortAge;

private JPanel pnlCommand;

private JPanel pnlDisplay;

private ArrayList<Person> plist;

private PersonListing thisForm;

private JScrollPane scrollPane;

private JTable table;

private DefaultTableModel model;

public PersonListing() {

super(new GridLayout(2,1));

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);

}

private void showTable(ArrayList<Person> 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();

}

});

}

public void addPerson(Person p)

{

plist.add(p);

addToTable(p);

}

private ArrayList<Person> loadPersons(String pfile)

{

Scanner pscan = null;

ArrayList<Person> plist = new ArrayList<Person>();

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

{

public void actionPerformed(ActionEvent e)

{

System.exit(0); }

}

Please help its due tomorrow could u write the code where it should be in the starter code?

After the initial deployment of Vaccinator Prime(see Lab 5 description), it has become evident that a large number of users may need to interact with the person registration process on the front lines. As a result it has been decided that a GUI is needed for that part of the system. Your immediate tasks are 1. to complete a prototype that demonstrates basic functionality of displaying a list of persons, adding a person and sorting the list as required, and 2. then describe the effort required to implement a fully functional CRUD (Create/Read/Update/Delete) application. Your entry point to the project comes at a time when a junior developer has completed a basic mockup of the appearance of the system, which has been approved by top management. Specific objectives of the lab are: - Integrating a GUI with an application - Adding components to a GUI - Writing listeners for GUI components NON-FUNCTIONAL ASSESSMENT CRITERIA LAB EXERCISES (5 MARKS FOR DEMONSTRATED FUNCTIONALITY) Java code for four classes are provided. The classes are: - BasePerson:Abstract person class - Exact replica of class used as a starter code for Lab 5 - Person: Defines a person - Exact replica of class used as a starter code for Lab 5 - PersonListing: Starter code for screen that should list persons - PersonEntry : Starter code for a screen that should allow entry into the list of persons. Please download these classes and load them into a project for this lab. Spend a few moments to get acquainted with the code. PersonListing.java exposes a public static main method that presents the screen shown in Figure 1. In addition, PersonListing.java also includes several attributes and methods that are used to implement basic functionality, some of which are presented in Table 1. Figure 1 Person Listing Screen Figure 2. Result of executing starting code for PersonEntry. RADEABLE ACTIVITIES 1. Section1.- Populating the list of persons on the person listing form, and put functionality in the "Add Person" button (1 mark) a. In PersonListing: The starting code places only one item in the list of persons. Discussions with the original developer have yielded that the method "showTable" in PersonListing is responsible for populating the table. The addToTable method which accepts one person and adds information regarding that person to the table should NOT be modified. Complete showTable so that an entry is placed in the table for each row in the file person.dat. b. In PersonEntry: Modify PersonEntry so that it is able to store an instance of PersonListing. c. In PersonEntry: Modify the Constructor of PersonEntry so that it accepts an instance of PersonListing, and then sets the local instance variable to the value accepted by the constructor. d. In PersonListing: Add a listener to the "Add Person" button so that when it is clicked, an instance of PersonEntry is displayed. When invoking the instance of PersonEntry, a reference to the current instance of PersonListing should be passed as an argument to the constructor. //Hint... a listener is already included in the starting code that adds functionality to the Close button. 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 Figure 3.Expected Appearance of Person Entry Screen 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 get Text0 method of a jTextField returns the value of the text it currently displays. 3. Section 3 - Complete functionality on listing screen (1.5 marks) a. The starting code includes the definition of a JButton named cmdSortAge, but it is not shown on the PersonListing screen. Modify the PersonListingScreen to allow it to be shown b. Add a listener which implements the functionality to sort the list in ascending order of age. Hints: i. You should already know how to sort data ii. Presenting information in a specific order in the table may involve removing all the data and then re-adding it iii. Data in the table is removed with the command modelsetRowCount(0); model is an object name that was declared with the starting code. le. model is an object of the class DefaultTableModel c. Add a button and an associated listener to sort the list in order of first name. 4. Change the colors of the form to implement a custom colour scheme. (1 mark) Figure 4 shows an example of a custom colour scheme. You should come up with your own and then set the colours on the Person Listing screen appropriately. Figure 4. An example of a custom color scheme after two more persons have been entered, sorted in order of age. 5. Compress the .java files into a .zip and submit to OurVLE.


We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe