Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I NEED HELP WITH THIS QUESTION PROGRAMMING LANGUAGE : JAVA I \'M MAKING A PHONEB

ID: 3844439 • Letter: I

Question

I NEED HELP WITH THIS QUESTION

PROGRAMMING LANGUAGE : JAVA

I 'M MAKING A PHONEBOOK WITH GRAPHICAL USER INTERFACE (GUI), HERE'S MY GUI:

BUT I DON'T KNOW HOW TO ADD EVENT TO GUI BUTTONS.

I HAVE CODES FOR THEM BUT LIKE I SAID I DON'T KNOW HOW TO ADD EVENTS TO THEM. HERE'S MY CODES FOR GUI BUTTONS:

"ADD" BUTTON SHOULD DO ADDING PERSONS IN ORDER TO THEIR NAMES

"REMOVE" BUTTON SHOULD DO REMOVING PERSONS

"A - Z" BUTTON SHOULD PRINT PERSONS IN ASCENDING ALPHABETICAL ORDER

"Z-A" BUTTON SHOULD DO PRINT PERSONS IN DESCENDING ALPHABETICAL ORDER

PLEASE CAN YOU HELP ME?

Name Surname Phone Add Remove Z-A

Explanation / Answer

Since I can see that you have already done the major work that goes into making this Phone directory. Now the only thing as was mentioned was linking the GUI items with the
actual functions that's more easy than you think it is to be.

First since I can't see the entire code, so I am just labelling the GUI items based on convenience and you can change it at your side.


but this is how the process needs to be followed.

1) Add the new action Listener object to your button so that when once clicked it could trigger the actionPerformed() method in this custom made ButtonListener class.  

addButton.addActionListener(new ButtonListener()); //addButton is name given to add button at your end.

2) Now this ButtonListener is a new sub class that is created within the your main GUI Class. Main thing to note that this class is going to
fetch the data values entered in text fields, so its important to have them mentioned them as global variables first.

public class ButtonListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
           String first=name.getText(); //Taking the 3 values from the 3 text fields in the GUI
           String last=last.getText();
           String name=first.concat(last); // A single String is send as name in Add function that was
                                             //created at your end, so adding the first and last names.
           String phones=phone.getText(); //Taking the phone number as String separated by one or more space. U //can use your delimiter
           ArrayList<String> ix=new ArrayList<String>(); //Create a new ArrayList<String> for getting the numbers //as String and Storing each as unique number
                                                           //uniquely in the new ArrayList<String>
           String[] unique=phones.split("\s+");             // entry in the ArrayList. Defining the split criteria here I'm //assuming that phones nos are sepreated
           for(String items:unique){                       // by blank spaces as storing them as String[]
              
               ix.add(items);
           }
          
           /*Now since the required entries are created so now we can call the AddPerson function of the PhoneBook class.
   If AddPerson() is defined in a seperate class then use that object reference or else in the same then can use this keyword.*/

this.AddPerson(name,ix);
       }
      
} // This concluded the Add Button Action Listener

3) The same can be repeated for the Remove button as well. First adding action Listener object to your button so that when once clicked it could trigger the actionPerformed() method in this custom made
DeleteButtonListener class.

removeButton.addActionListener(new DeleteButtonListener()); //addButton is name given to add button at your end.

Same as above creating the DeleteButtonListener sub class within the main GUI Class.


public class DeleteButtonListener implements ActionListener{

@Override
       public void actionPerformed(ActionEvent e) {
   //Taking the 2 values from the 2 text fields in the GUI, since remove is Parametrized to have one String name.

           String first=name.getText();
           String last=last.getText();

// A single String is send as name in Remove function that was created at your end so using concat below
           String name=first.concat(last);


                 
           // We have the required entries, so now we can call the Remove funtion of the PhoneBook class.
           if(this.RemovePerson(name)){  

//Since your remove function is returning value back so u could use that value to that TextArea to inform.
           textArea.setText("Entry Removed");
           else{
           textArea.setText("Entry not found");
          
           }
          
          
       }
      
} // This concluded the Remove Button Action Listener

4) A-Z button. I'm using "ascend" to describe that button.
ascend.addActionListener(new AscendListener());


Same as above creating the AscendListener sub class within the main GUI Class.

public class AscendListener implements ActionListener{

@Override
       public void actionPerformed(ActionEvent e) {
          /*Since there are no parameters to work on to call GetAscendingOrder(), we can call it straighaway. The method return a String back, so lets store it in a variable named str.*/
              
             String str=this.GetAscendingOrder();              
/* I'm guessing that the string is going to outputted to the TextArea defined below, if so then u can assign the str value to it.*/
          
           textArea.setText(str); //textArea name has to changes at your end.
              
       }
      
} // This concluded the ascend Button Action Listener

5) Z-A button.

descend.addActionListener(new DescendListener());

public class DescendListener implements ActionListener{

@Override
       public void actionPerformed(ActionEvent e) {
           String str=this.GetDescendingOrder();              
          
           textArea.setText(str); //textArea name has to changes at your end.
              
       }
      
} // This concluded the descend Button Action Listener



In the end I just want to add that this is a rough sketch as to how to proceed with linking Listeners to your GUI items. This code is not tested and it just for representational purposes, I have tried to generalize the details as far as possible so that it can be customized based on your needs.

Inlcuding the Program Skeleton and showing where the modification have to be carried.

public class GraphicsPhoneBook extends JFrame{
  
   JTextField name;
   JTextField phone;
   DDPhoneBook d;
   JTextArea result;
   JScrollPane jscroll;
  
   public GraphicsPhoneBook(){
      
       //Main GUI components initialization goes here for above globally declared componenets and then linking their following actionListeners
       /*Code Add Start
       addButton.addActionListener(new ButtonListener());
       removeButton.addActionListener(new DeleteButtonListener());
       ascend.addActionListener(new AscendListener());
       descend.addActionListener(new DescendListener());
        */ Code Add End
   }
   
   public static void main(String[] args) {
       // Main method
       }
  

/*Code Add Start
   public class ButtonListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent arg0) {
          
       }  
      }  
  
   public class DeleteButtonListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
          
           }
          
       }

       public class AscendListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
         
           }
      
       }

       public class DescendListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
       }
      
       } */ Code Add End

} // End of Class