I NEED HELP WITH THIS QUESTION PROGRAMMING LANGUAGE : JAVA I \'M MAKING A PHONEB
ID: 3844452 • 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-AExplanation / Answer
For Java GUI I am assuming you are using Java Swing/AWT
Also I am assuming the variable names of objects for Text fields and buttons as :-
Name Text Field - nameField
Surname Text Field - surnameField
Phone Text Field - phoneField
Add Button - addButton
Remove Button - removeButton
Z - A Button - zaButton
A - Z Button - azButton
Display Text Area - displayArea
Also as you are passing an Arraylist of phone numbers to AddPerson method, I am
assuming that user will enter multiple phone numbers seperated by comma (,)
Below is the complete code for getting inputs from fields and adding listeners
to buttons. I have heavily commmented the code for deeper understanding.
// Get name from nameField
String name = nameField.getText();
//Get Surname from surnameField
String surname = surnameField.getText();
//Get phone numbers as string from phoneField
String phoneNumbers = phoneField.getText();
/* Now convert the phoneNumbers string to Array list of numbers by splitting the
* string whenever a comma (,) is encountered taking into account any white spaces, which
* will be trimmed in final Arraylist.
*/
List<String> phoneList = new ArrayList<String>(Arrays.asList(phoneNumbers.split("\s*,\s*")));
// Now adding listener to add button using anonymous inner class
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Here code will be executed after pressing the Add Button
// Using you AddPerson method
AddPerson(name + " " + surname, phoneList);
}
});
// Similarly adding Action Listener for Remove Button
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Here code will be executed after pressing the Remove Button
// Using you RemovePerson method
RemovePerson(name + " " +surname);
}
});
// Now for A-Z and Z-A Buttons.
azButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Here code will be executed after pressing the A - Z Button
// Using you GetAscendingOrder method
displayArea.setText(GetAscendingOrder());
}
});
zaButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Here code will be executed after pressing the Z - A Button
// Using you GetDescendingOrder method
displayArea.setText(GetDescendingOrder());
}
});
I Hope it helps!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.