Listview 将文本从Arraylist添加到TextArea javafx

Listview 将文本从Arraylist添加到TextArea javafx,listview,javafx,arraylist,Listview,Javafx,Arraylist,我想知道如何将存储在ArrayList中的对象的各种属性获取到TextArea? 我有一个ListView,根据您按下ListView中的哪一行,文本区域中应显示不同的文本。我就是不能让它工作 这里是我目前的一些代码。动物是另一类 ListView<String> cats = new ListView<>(); cats.setPrefSize(90, 200); cats.getItems().addAll( "Firs

我想知道如何将存储在ArrayList中的对象的各种属性获取到TextArea? 我有一个ListView,根据您按下ListView中的哪一行,文本区域中应显示不同的文本。我就是不能让它工作

这里是我目前的一些代码。动物是另一类

    ListView<String> cats = new ListView<>();
    cats.setPrefSize(90, 200);
    cats.getItems().addAll(
            "First cat",
            "Second cat" 
    );

    final ArrayList<Animals> catsdesricption = new ArrayList<Animals>();
    Animals FirstCat = new Animals("First cat", "cats", "is small and fluffy");

    catsdesricption.add(FirstCat);
    TextArea description = new TextArea();
    description.setMaxSize(300, 200);
    description.setWrapText(true);

    VBox vbox = new VBox();
    Label heading = new Label("Cats");
    heading.setFont(new Font("Times new Roman", 20));

    HBox layout = new HBox();

    layout.getChildren().addAll(cats, catsdesricption);
    vbox.getChildren().addAll(heading, layout);

    Scene scene = new Scene(vbox, 420, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
ListView猫=新建ListView();
猫的体型(90200);
cats.getItems().addAll(
“第一只猫”,
“第二只猫”
);
最终ArrayList CATSDESRICTION=新ArrayList();
动物第一只猫=新动物(“第一只猫”、“猫”、“小而蓬松”);
CATSDESRICTION.add(第一个CAT);
TextArea description=新建TextArea();
说明.setMaxSize(300200);
description.setWrapText(true);
VBox VBox=新的VBox();
标签标题=新标签(“猫”);
标题.setFont(新字体(“Times new Roman”,20));
HBox布局=新的HBox();
layout.getChildren().addAll(cats、catsdesricption);
vbox.getChildren().addAll(标题、布局);
场景=新场景(vbox,420,250);
初级阶段。场景(场景);
primaryStage.show();

仔细查看您的代码后,我发现了大量问题。回答你原来的问题。您需要在
SelectionModel的
ItemProperty
上设置一个侦听器来更新
TextArea

    cats.getSelectionModel().selectedItemProperty().addListener((obs, oldAnimal, newAnimal) -> {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Name: ").append(newAnimal.getName()).append(System.lineSeparator());
        stringBuilder.append("Type: ").append(newAnimal.getType()).append(System.lineSeparator());
        stringBuilder.append("About: ").append(newAnimal.getAbout()).append(System.lineSeparator());        

        description.setText(stringBuilder.toString());
    });
第一个问题:

ListView<String> cats = new ListView<>();
cats.setPrefSize(90, 200);
cats.getItems().addAll(
        "First cat",
        "Second cat" 
);
第二个问题: 既然
列表视图
正在处理
动物
对象,我们需要使用
列表视图的
CellFActory
告诉
列表视图
显示什么文本。在这种情况下,将显示名称

    cats.setCellFactory((ListView<Animal> param) -> {
        ListCell<Animal> cell = new ListCell<Animal>() {                
            @Override
            protected void updateItem(Animal item, boolean empty) {
                super.updateItem(item, empty);
                if (item != null) {
                    setText(item.getName());
                } else {
                    setText("");
                }
            }
        };

        return cell;
    }); 
我的动物课:(你没有发布你的)


请提供一些代码,以便有人能为您提供解决方案。您需要做的事情与。收听选择模型并相应地更新文本字段。
layout.getChildren().addAll(cats,catsdesricption)如何编译。谢谢!如果我想在文本区域打印时为不同猫的名称、类型和描述添加不同类型的字体和字体大小,该怎么办?研究这个问题。如果你找不到解决办法,问一个新问题。
    cats.setCellFactory((ListView<Animal> param) -> {
        ListCell<Animal> cell = new ListCell<Animal>() {                
            @Override
            protected void updateItem(Animal item, boolean empty) {
                super.updateItem(item, empty);
                if (item != null) {
                    setText(item.getName());
                } else {
                    setText("");
                }
            }
        };

        return cell;
    }); 
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.control.TextArea;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;

    /**
     * JavaFX App
     */
    public class App extends Application {

        @Override
        public void start(Stage primaryStage) {
            ListView<Animal> cats = new ListView<>();
            cats.getItems().add(new Animal("First cat", "cats", "is small and fluffy"));
            cats.getItems().add(new Animal("Second cat", "cats", "is big and fluffy"));

            cats.setCellFactory((ListView<Animal> param) -> {
                ListCell<Animal> cell = new ListCell<Animal>() {                
                    @Override
                    protected void updateItem(Animal item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item.getName());
                        } else {
                            setText("");
                        }
                    }
                };

                return cell;
            }); 
            cats.setPrefSize(90, 200);           

            TextArea description = new TextArea();
            description.setMaxSize(300, 200);
            description.setWrapText(true);

            cats.getSelectionModel().selectedItemProperty().addListener((obs, oldAnimal, newAnimal) -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("Name: ").append(newAnimal.getName()).append(System.lineSeparator());
                stringBuilder.append("Type: ").append(newAnimal.getType()).append(System.lineSeparator());
                stringBuilder.append("About: ").append(newAnimal.getAbout()).append(System.lineSeparator());        

                description.setText(stringBuilder.toString());
            });

            VBox vbox = new VBox();
            Label heading = new Label("Cats");
            heading.setFont(new Font("Times new Roman", 20));

            HBox layout = new HBox(cats, description);

            vbox.getChildren().addAll(heading, layout);

            Scene scene = new Scene(vbox, 420, 250);
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        public static void main(String[] args) {
            launch();
        }    
    }
/**
 *
 * @author blj0011
 */
class Animal {
    private String name;
    private String type;
    private String about;

    public Animal(String name, String type, String about) {
        this.name = name;
        this.type = type;
        this.about = about;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Animals{name=").append(name);
        sb.append(", type=").append(type);
        sb.append(", about=").append(about);
        sb.append('}');
        return sb.toString();
    }    
}