I am creating an application in JavaFX with an FXML file. The application will have a listview that takes in a person object class for firstName, lastName, and notes for personalHobbies. All with getters and setter. And a to string to with all the relevant items. Each class part is typed in a textField in the app by the user in the person list. And when a person is selected, all of the parts in the toString of each person class show like this: John Smith Hobbies include video games, playing, and writing.
The app also includes a button to add and delete a person from the list too. The problem is that all of the to string information shows up on the selection in the list, not just the first name by itself. I have been using an observable arraylist And everythibg but that works. This is my first post, and I am new to JavaFX so please forgive me, but I need help, thanks in advance.
Controller class with Person class
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class ListViewWithTextAreaController implements Initializable{
@FXML
private TextArea textArea;
@FXML
private ListView<Person> listView;
@FXML
private TextField firstName;
@FXML
private TextField lastName;
@FXML
private TextArea personalHobbies;
//Key Component: ObservableList with variety of string item types for list.
final ObservableList<Person> listPerson = FXCollections.observableArrayList();
@FXML
void addButtonClicked(ActionEvent event) {
//adds new item from the user to the list.
Person newPerson = new Person(firstName.getText());
newPerson.setLastName(lastName.getText());
newPerson.setPersonalHobbies(personalHobbies.getText());
listPerson.add(newPerson);
String about = newPerson.toString();
//shows the currently added Person to the TextField.
textArea.setText(about);
clearTextRefocus();
}
@FXML
void deleteButtonClicked(ActionEvent event){
//Deletes the currently selected Person from the list.
Person selectionToRemove = listView.getSelectionModel().getSelectedItem();
listPerson.remove(selectionToRemove);
textArea.clear();
clearTextRefocus();
}
@Override
public void initialize(URL url, ResourceBundle rb){
//Set the listed observableList items to the listView as selections.
listView.setItems(listPerson);
//ChangeListener for TextField to update for changes in focus on List items.
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
@Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
if(listView.isFocused()){
textArea.setText(newValue.toString());
//textArea.getText();
}
}
});
//Gets the selection of the first index model type in the listView, then
//Wrap the requestFocus inside a Platform.runLater() to set the focus
//on the first element of the string index of zero "Add/Delete items Here".
listView.getSelectionModel().select(0);
Platform.runLater(new Runnable(){
@Override
public void run(){
listView.requestFocus();
}
});
}
//Person class.
private static class Person {
private String firstName;
private String lastName;
private String personalHobbies;
public Person(String firstName) {
this.firstName = firstName;
this.lastName = lastName = "";
this.personalHobbies = "";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPersonalHobbies() {
return personalHobbies;
}
public void setPersonalHobbies(String personalHobbies) {
this.personalHobbies = personalHobbies;
}
@Override
public String toString() {
return String.format("firstName " + firstName + "\nlastName " + lastName
+ "\n\tpersonal Hobbies " + personalHobbies);
}
}
public void clearTextRefocus(){
//Auto clear the user Typing textFields.
firstName.clear();
lastName.clear();
personalHobbies.clear();
listView.requestFocus(); //Place focus back on the list (stops focus glitch).
}
}