SlideShare a Scribd company logo
1 of 9
Download to read offline
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;
}
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;
}
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
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 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 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 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
{
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?

More Related Content

Similar to 2. Section 2. Implementing functionality in the PersonEntry. (1.5 ma.pdf

Creating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfCreating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XLI.pdf
Creating a Facebook Clone - Part XLI.pdfCreating a Facebook Clone - Part XLI.pdf
Creating a Facebook Clone - Part XLI.pdfShaiAlmog1
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfarihantgiftgallery
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfShaiAlmog1
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfakshpatil4
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfarjuncp10
 
Creating a Facebook Clone - Part X.pdf
Creating a Facebook Clone - Part X.pdfCreating a Facebook Clone - Part X.pdf
Creating a Facebook Clone - Part X.pdfShaiAlmog1
 
Creating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfCreating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part VII.pdf
Creating a Facebook Clone - Part VII.pdfCreating a Facebook Clone - Part VII.pdf
Creating a Facebook Clone - Part VII.pdfShaiAlmog1
 
Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfShaiAlmog1
 
Creating a Facebook Clone - Part VI - Transcript.pdf
Creating a Facebook Clone - Part VI - Transcript.pdfCreating a Facebook Clone - Part VI - Transcript.pdf
Creating a Facebook Clone - Part VI - Transcript.pdfShaiAlmog1
 
Initial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdfInitial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XLVI - Transcript.pdf
Creating a Facebook Clone - Part XLVI - Transcript.pdfCreating a Facebook Clone - Part XLVI - Transcript.pdf
Creating a Facebook Clone - Part XLVI - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfCreating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfShaiAlmog1
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfNicholasflqStewartl
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2stuq
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議Jason Kuan
 

Similar to 2. Section 2. Implementing functionality in the PersonEntry. (1.5 ma.pdf (20)

Creating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfCreating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdf
 
Presentation
PresentationPresentation
Presentation
 
Creating a Facebook Clone - Part XLI.pdf
Creating a Facebook Clone - Part XLI.pdfCreating a Facebook Clone - Part XLI.pdf
Creating a Facebook Clone - Part XLI.pdf
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdf
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdf
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdf
 
Creating a Facebook Clone - Part X.pdf
Creating a Facebook Clone - Part X.pdfCreating a Facebook Clone - Part X.pdf
Creating a Facebook Clone - Part X.pdf
 
Creating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfCreating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdf
 
Creating a Facebook Clone - Part VII.pdf
Creating a Facebook Clone - Part VII.pdfCreating a Facebook Clone - Part VII.pdf
Creating a Facebook Clone - Part VII.pdf
 
MaintainStaffTable
MaintainStaffTableMaintainStaffTable
MaintainStaffTable
 
Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdf
 
Creating a Facebook Clone - Part VI - Transcript.pdf
Creating a Facebook Clone - Part VI - Transcript.pdfCreating a Facebook Clone - Part VI - Transcript.pdf
Creating a Facebook Clone - Part VI - Transcript.pdf
 
Initial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdfInitial UI Mockup - Part 3 - Transcript.pdf
Initial UI Mockup - Part 3 - Transcript.pdf
 
Creating a Facebook Clone - Part XLVI - Transcript.pdf
Creating a Facebook Clone - Part XLVI - Transcript.pdfCreating a Facebook Clone - Part XLVI - Transcript.pdf
Creating a Facebook Clone - Part XLVI - Transcript.pdf
 
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdfCreating a Facebook Clone - Part XXXVI - Transcript.pdf
Creating a Facebook Clone - Part XXXVI - Transcript.pdf
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議
 

More from allwayscollection

19. In the past few weeks, we have seen the Federal Reserve and Tr.pdf
19.   In the past few weeks, we have seen the Federal Reserve and Tr.pdf19.   In the past few weeks, we have seen the Federal Reserve and Tr.pdf
19. In the past few weeks, we have seen the Federal Reserve and Tr.pdfallwayscollection
 
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdfallwayscollection
 
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdfallwayscollection
 
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdfallwayscollection
 
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdfallwayscollection
 
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdfallwayscollection
 
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdfallwayscollection
 
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdfallwayscollection
 
142 Cranlal Nerwe Jumetion.pdf
142 Cranlal Nerwe Jumetion.pdf142 Cranlal Nerwe Jumetion.pdf
142 Cranlal Nerwe Jumetion.pdfallwayscollection
 
14. Essay Using the following information f�Using the followi.pdf
14. Essay Using the following information f�Using the followi.pdf14. Essay Using the following information f�Using the followi.pdf
14. Essay Using the following information f�Using the followi.pdfallwayscollection
 
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdfallwayscollection
 
13. Alan, Bruce and Connie want to combine their property in a new c.pdf
13. Alan, Bruce and Connie want to combine their property in a new c.pdf13. Alan, Bruce and Connie want to combine their property in a new c.pdf
13. Alan, Bruce and Connie want to combine their property in a new c.pdfallwayscollection
 
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdfallwayscollection
 
13. It is year 1225 and Easter Islanders are crafting their now well.pdf
13. It is year 1225 and Easter Islanders are crafting their now well.pdf13. It is year 1225 and Easter Islanders are crafting their now well.pdf
13. It is year 1225 and Easter Islanders are crafting their now well.pdfallwayscollection
 
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdfallwayscollection
 
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdfallwayscollection
 
2.4 CollaboratorsThe company should also understand the relationsh.pdf
2.4 CollaboratorsThe company should also understand the relationsh.pdf2.4 CollaboratorsThe company should also understand the relationsh.pdf
2.4 CollaboratorsThe company should also understand the relationsh.pdfallwayscollection
 
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdfallwayscollection
 

More from allwayscollection (20)

19. In the past few weeks, we have seen the Federal Reserve and Tr.pdf
19.   In the past few weeks, we have seen the Federal Reserve and Tr.pdf19.   In the past few weeks, we have seen the Federal Reserve and Tr.pdf
19. In the past few weeks, we have seen the Federal Reserve and Tr.pdf
 
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf
19) Una oficina de seis personas est� plagada de alto ausentismo. Se.pdf
 
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf
17.16 Uno de los inconvenientes de la televisi�n por sat�lite es....pdf
 
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf
16.Sophia est� muy molesta. Le ha contado a su maestra sobre mensaje.pdf
 
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf
16. The Plainfield Company has a long-term debt ratio (i.e., the rat.pdf
 
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf
15.Durante el per�odo de julio a diciembre, el mismo Hospital mencio.pdf
 
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf
15. valor 10.00 puntos Informaci�n requerida El enfoque c.pdf
 
15.9142.618.571.4.pdf
15.9142.618.571.4.pdf15.9142.618.571.4.pdf
15.9142.618.571.4.pdf
 
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf
15. A principios de 2021, Waterway Industries hab�a retenido gananci.pdf
 
142 Cranlal Nerwe Jumetion.pdf
142 Cranlal Nerwe Jumetion.pdf142 Cranlal Nerwe Jumetion.pdf
142 Cranlal Nerwe Jumetion.pdf
 
14. Essay Using the following information f�Using the followi.pdf
14. Essay Using the following information f�Using the followi.pdf14. Essay Using the following information f�Using the followi.pdf
14. Essay Using the following information f�Using the followi.pdf
 
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf
14. Mary es una maestra de secundaria jubilada de 72 a�os. Ella in.pdf
 
13. Alan, Bruce and Connie want to combine their property in a new c.pdf
13. Alan, Bruce and Connie want to combine their property in a new c.pdf13. Alan, Bruce and Connie want to combine their property in a new c.pdf
13. Alan, Bruce and Connie want to combine their property in a new c.pdf
 
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf
13. La revisi�n judicial de las decisiones de las agencias gira en t.pdf
 
13. It is year 1225 and Easter Islanders are crafting their now well.pdf
13. It is year 1225 and Easter Islanders are crafting their now well.pdf13. It is year 1225 and Easter Islanders are crafting their now well.pdf
13. It is year 1225 and Easter Islanders are crafting their now well.pdf
 
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf
20) The antibiotic penicillin is isolated from ________.A) B. subt.pdf
 
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf
2.1 Estudio de caso un l�der emergente Tim T. retrata su vida com.pdf
 
2.4 CollaboratorsThe company should also understand the relationsh.pdf
2.4 CollaboratorsThe company should also understand the relationsh.pdf2.4 CollaboratorsThe company should also understand the relationsh.pdf
2.4 CollaboratorsThe company should also understand the relationsh.pdf
 
2.5�108 concentration.pdf
2.5�108 concentration.pdf2.5�108 concentration.pdf
2.5�108 concentration.pdf
 
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf
2. �Cu�l de los siguientes no es un rasgo de alta inteligencia cultu.pdf
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

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;
  • 5. 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 plist; private PersonListing thisForm; private JScrollPane scrollPane; private JTable table; private DefaultTableModel model; public PersonListing() { super(new GridLayout(2,1));
  • 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
  • 9. { 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?