JavaFX对象listView将对象名称显示为列表中的tostring

时间:2018-04-24 19:51:40

标签: listview object javafx textarea tostring

我正在使用FXML文件在JavaFX中创建一个应用程序。该应用程序将有一个listview,它接受firstName,lastName和personalHobbies的notes的person对象类。所有人都有吸气剂和二传手。和一个串起来与所有相关的项目。用户在人员列表中在应用程序的textField中键入每个类部件。当一个人被选中时,每个人类的toString中的所有部分都显示如下:     约翰     工匠     爱好包括视频游戏,游戏和写作。

该应用程序还包括一个按钮,用于添加和删除列表中的人员。 问题是所有的字符串信息都显示在列表中的选择上,而不仅仅是名字本身。  我一直在使用一个可观察的arraylist  而everythibg但是有效。 这是我的第一篇文章,我是JavaFX的新手,所以请原谅我,但我需要帮助,提前谢谢。

具有Person类的控制器类

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).
}

}

All of the toString as the selection name

1 个答案:

答案 0 :(得分:0)

我还能够弄清楚如何让它工作但这是错误的方法,将to string更改为getMethod,并创建一个字符串作为setText值传递给更改侦听器中的textArea。初始化方法。并更改Person类本身的构造函数。在这种情况下,需要一个CellFactory,并根据James_D和kleopatra的建议使用更改侦听器。我正在放置正确的代码和错误的代码,以显示像我这样的其他人如何正确地完成这个,以及什么不该做。

以下是使用cellFactory和更改侦听器的正确方法:

cellFactory correct way

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());     

    }
}

}

这是错误的代码和图片:

incorrect listView Image

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();     

    }
}

}