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

    public PersonTableModel(List<Person> 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(List<Person>list)
   {
      
       //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
       List<Person>arrayList=new ArrayList<Person>();
       //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);
      
   }
}

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

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

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I have currently a functional Java progam with a gui. Its a simple table of contacts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import...

    Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener {    public static final int width = 500;    public static final int height = 300;    public static final int no_of_lines = 10;    public static final int chars_per_line = 20;    private JTextArea lan1;    private JTextArea lan2;    public static void main(String[] args){        translatorApp gui = new translatorApp();...

  • This is the java GUI program. I want to add an icon to a button, but...

    This is the java GUI program. I want to add an icon to a button, but it shows nothing. Can you fix it for me please? import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Icon extends JFrame{ public static void main(String args[]){ Icon frame = new Icon("title"); frame.setVisible(true); } Icon(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png"); JButton button1 = new JButton(icon1); p.add(button1); Container contentPane...

  • Debug this java and post the corrected code. /* Creates a simple JPanel with a single...

    Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame {     public QuitIt() {         startIt(); //Create everything and start the listener     }     public final void startIt() {        //Create the JPanel        JPanel myPanel =...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT