Question: I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now

I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much.

Here is the working code so far:

//PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel { private static final int COLUMN_FIRSTNAME = 0; private static final int COLUMN_MIDDLENAME = 1; private static final int COLUMN_LASTNAME = 2; private static final int COLUMN_JOB = 3; private String[] columnNames = {"First Name", "Middle Name", "Last Name", "JOB" }; private List listPersons;

public PersonTableModel(List listPersons){ this.listPersons = listPersons; } @Override public int getColumnCount() { return columnNames.length; }

@Override public int getRowCount() { return listPersons.size(); }

@Override public Object getValueAt(int rowIndex, int columnIndex) { Person person = listPersons.get(rowIndex); Object returnValue = null; switch(columnIndex){ case COLUMN_FIRSTNAME: returnValue = person.getFirstName(); break; case COLUMN_MIDDLENAME: returnValue = person.getMiddleName(); break; case COLUMN_LASTNAME: returnValue = person.getLastName(); break; case COLUMN_JOB: returnValue = person.getJob(); break; default: throw new IllegalArgumentException("Invalid column index"); } return returnValue; } @Override public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } }

------------------------------------------------------------------------------------------------------------------------------------

//Person.java public class Person { public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } private String firstName; private String middleName; private String lastName; private String job;

public Person(String firstName, String middleName, String lastName, String job){ this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.job = job; } }

-----------------------------------------------------------------------------------------------------------------------------

//TableGUI.java import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; public class TableGUI extends JFrame { //Create PersonTableModel variable PersonTableModel prm; //Create JTable variable JTable table; //Create two panel variables JPanel tablePanel; JPanel buttonPanel; //Declare three button variables JButton addButton; JButton editButton; JButton exitButton; public TableGUI(Listlist) { //set layour as borderlayout setLayout(new BorderLayout()); //Craete an instance of PersonTableModel prm=new PersonTableModel(list); //Create an intance of JTable and set PersonTableModel as its object table=new JTable(prm); tablePanel=new JPanel(); tablePanel.add(table); //Add tablePanel add(tablePanel, BorderLayout.NORTH); buttonPanel=new JPanel(); buttonPanel.setLayout(new FlowLayout()); addButton=new JButton("Add"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { System.out.println("Not implemented"); } }); buttonPanel.add(addButton); editButton=new JButton("Edit"); buttonPanel.add(editButton); editButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Not implemented"); } }); exitButton=new JButton("Exit"); buttonPanel.add(exitButton); exitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Not implemented"); } }); //Add buttonPanel add(buttonPanel, BorderLayout.CENTER); pack(); } }

--------------------------------------------------------------------------------------------------------------------------------------------

/**The driver program that displays jTable with * three buttons to add, edit and exit . The buttons * are not implemented. * */ //PersonDriver.java import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; public class PersonDriver extends JFrame { public static void main(String[] args) { //Create a ArrayList object ListarrayList=new ArrayList(); //Add rows to the arrayList arrayList.add(new Person("FirstName", "MiddleName", "LastName", "Job")); arrayList.add(new Person("f1", "m1", "l2", "j3")); arrayList.add(new Person("f5", "m4", "l3", "j1")); arrayList.add(new Person("f3", "m3", "l5", "j2")); arrayList.add(new Person("f1", "m2", "l4", "j5")); arrayList.add(new Person("f4", "m5", "l1", "j4")); //Create an JFrame object JFrame tableFrame=new TableGUI(arrayList); //Set title tableFrame.setTitle("JTableExample"); //Set visible true tableFrame.setVisible(true); } }

------------------------------------------------------------------------------------------------------------------------------------------------

I have currently a functional Java progam with a gui. Its a

JTableExample FirstName MiddleName LastName Johb m1 m4 m3 m2 m5 j3 j5 Add Edit Exit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!