JavaFX对象listView将对象名称显示为列表中的tostring
我正在用一个FXML文件在JavaFX中创建一个应用程序。该应用程序将有一个listview,它为firstName、lastName引入person对象类,并为Personal嗜好引入notes。所有的人都有能手和二传手。和一个包含所有相关项的to字符串。每个类部件都由用户在人员列表中的文本字段中键入。当选择一个人时,每个person类的toString中的所有部分如下所示: 约翰 史密斯 爱好包括电子游戏、玩耍和写作 该应用程序还包括一个按钮,可以从列表中添加和删除一个人。 问题是所有的to字符串信息都显示在列表中的选择项上,而不仅仅是名字本身。 我一直在使用一个可观察的arraylist 除此之外的一切都是有效的。 这是我的第一篇文章,我是JavaFX新手,所以请原谅我,但我需要帮助,提前谢谢 带有Person类的控制器类JavaFX对象listView将对象名称显示为列表中的tostring,listview,object,javafx,textarea,tostring,Listview,Object,Javafx,Textarea,Tostring,我正在用一个FXML文件在JavaFX中创建一个应用程序。该应用程序将有一个listview,它为firstName、lastName引入person对象类,并为Personal嗜好引入notes。所有的人都有能手和二传手。和一个包含所有相关项的to字符串。每个类部件都由用户在人员列表中的文本字段中键入。当选择一个人时,每个person类的toString中的所有部分如下所示: 约翰 史密斯 爱好包括电子游戏、玩耍和写作 该应用程序还包括一个按钮,可以从列表中添加和删除一个人。 问题是所有的to
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).
}
import java.net.URL;
导入java.util.ResourceBundle;
导入javafx.application.Platform;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.ListView;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextField;
公共类ListViewWithTextAreaController实现可初始化{
@FXML
私人文本区文本区;
@FXML
私有列表视图列表视图;
@FXML
私有文本字段名;
@FXML
私有文本字段lastName;
@FXML
私人爱好;
//关键组件:ObservableList,具有列表的各种字符串项类型。
最终ObservableList listPerson=FXCollections.observableArrayList();
@FXML
作废addButtonClicked(ActionEvent事件){
//将用户的新项目添加到列表中。
Person newPerson=newPerson(firstName.getText());
newPerson.setLastName(lastName.getText());
newPerson.setPersonal嗜好(Personal嗜好.getText());
添加(newPerson);
字符串about=newPerson.toString();
//在文本字段中显示当前添加的人员。
textArea.setText(关于);
clearTextRefocus();
}
@FXML
作废deleteButtonClicked(ActionEvent事件){
//从列表中删除当前选定的人员。
Person selectionToRemove=listView.getSelectionModel().getSelectedItem();
listPerson.remove(选择删除);
textArea.clear();
clearTextRefocus();
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
//将列出的observableList项设置为listView选项。
setItems(listPerson);
//ChangeListener for TextField用于更新列表项焦点的更改。
listView.getSelectionModel().SelectEditeProperty().addListener(新的ChangeListener()){
@凌驾
更改公众假期(ObservalEvalue我也能够找出如何让它工作,但这是一种不正确的方法,方法是将to字符串更改为getMethod,创建一个字符串作为setText值传递给initialize方法的change listener中的textArea,并更改Person类本身的构造函数。在这种情况下需要一个CellFactory正如James_D和kleopatra所建议的那样,用一个变更监听器进行案例分析。我放置了正确的代码和错误的代码,以向像我这样的其他人展示如何正确地完成这一点,以及不应该做什么
以下是使用cellFactory和change listener的正确方法:
import java.net.URL;
导入java.util.ResourceBundle;
导入javafx.application.Platform;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextField;
公共类ListViewWithTextAreaController实现可初始化{
//关键组件:ObservableList,具有列表的各种字符串项类型。
最终ObservableList listPerson=FXCollections.observableArrayList();
@FXML
私人文本区文本区;
@FXML
私有列表视图列表视图;
@FXML
私有文本字段名;
@FXML
私有文本字段lastName;
@FXML
私人爱好;
@FXML
作废addButtonClicked(ActionEvent事件){
//将用户的新项目添加到列表中。
Person newPerson=新人(firstName.getText(),lastName.getText(),personalcabiods.getText());
添加(newPerson);
//在文本字段中显示当前添加的人员。
textArea.setText(newPerson.toString());
clearTextRefocus();
}
@FXML
作废deleteButtonClicked(ActionEvent事件){
//从列表中删除当前选定的人员。
Person selectionToRemove=listView.getSelectionModel().getSelectedItem();
listPerson.remove(选择删除);
textArea.clear();
clearTextRefocus();
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
//将列出的observableList项设置为listView选项。
添加(新人物(“山姆”,“山”,“洞穴探险和探索洞穴”);
添加(新人物(“简”、“飞机”、“读书和缝纫”);
添加(新的人(“伯妮丝”,“特妮丝”,“东西和东西”);
setItems(listPerson);
//细胞工厂实施。
setCellFactory(参数->新建ListCell(){
@凌驾
受保护的void updateItem(Person p,布尔值为空){
super.updateItem(p,空);
if(空| | p==null | | p.getFirstName()==null){
setText(“”);
}
否则{
setText(p.getFirstName());
//已实现更改侦听器。
listView.getSelectionModel().se
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
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.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class ListViewWithTextAreaController implements Initializable{
//Key Component: ObservableList with variety of string item types for list.
final ObservableList<Person> listPerson = FXCollections.observableArrayList();
@FXML
private TextArea textArea;
@FXML
private ListView<Person> listView;
@FXML
private TextField firstName;
@FXML
private TextField lastName;
@FXML
private TextArea personalHobbies;
@FXML
void addButtonClicked(ActionEvent event) {
//adds new item from the user to the list.
Person newPerson = new Person(firstName.getText(), lastName.getText(),personalHobbies.getText());
listPerson.add(newPerson);
//shows the currently added Person to the TextField.
textArea.setText(newPerson.toString());
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.
listPerson.add(new Person("Sam", "Hill", "Spelunking and exploring caves."));
listPerson.add(new Person("Jane", "Plane", "Reading Books and sewing."));
listPerson.add(new Person("Bernice", "Ternice", " Things and stuff."));
listView.setItems(listPerson);
//cell factory implemented.
listView.setCellFactory(param -> new ListCell<Person>() {
@Override
protected void updateItem(Person p, boolean empty){
super.updateItem(p, empty);
if(empty || p == null || p.getFirstName() == null){
setText("");
}
else{
setText(p.getFirstName());
//Change listener implemented.
listView.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Person> observable, Person oldValue, Person newValue) -> {
if(listView.isFocused()){
textArea.setText(newValue.toString());
}
});
}
}
});
Platform.runLater(new Runnable(){
@Override
public void run(){
listView.requestFocus();
}
});
}
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).
}
//Person class.
private static class Person {
private String firstName;
private String lastName;
private String personalHobbies;
public Person(String firstName, String lastName, String hobbies) {
this.firstName = firstName;
this.lastName = lastName;
this.personalHobbies = hobbies;
}
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(getFirstName() + "\n" + getLastName() + "\n\t" + getPersonalHobbies());
}
}
package listviewwithtextarea;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
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(), lastName.getText(),personalHobbies.getText());
listPerson.add(newPerson);
//shows the currently added Person to the TextField.
textArea.setText(newPerson.toString());
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.
listPerson.add(new Person("Sam", "Hill", "Spelunking and exploring caves."));
listPerson.add(new Person("Jane", "Plane", "Reading Books and sewing."));
listPerson.add(new Person("Bernice", "Ternice", " Things and stuff."));
listView.setItems(listPerson);
//ChangeListener for TextField to update for changes in focus on List items.
listView.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Person> observable, Person oldValue, Person newValue) -> {
if(listView.isFocused()){
String info = String.format(newValue.getFirstName() + "\n" +
newValue.getLastName() + "\n\t" + newValue.getPersonalHobbies());
textArea.setText(info);
}
});
//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();
}
});
}
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).
}
//Person class.
private static class Person {
private String firstName;
private String lastName;
private String personalHobbies;
public Person(String firstName, String lastName, String hobbies) {
this.firstName = firstName;
this.lastName = lastName;
this.personalHobbies = hobbies;
}
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;
}`enter code here`
public String getPersonalHobbies() {
return personalHobbies;
}
public void setPersonalHobbies(String personalHobbies) {
this.personalHobbies = personalHobbies;
}
@Override
public String toString() {
return getFirstName();
}
}