将文本从Arraylist添加到TextArea javafx [英] Add text from Arraylist to TextArea javafx

查看:64
本文介绍了将文本从Arraylist添加到TextArea javafx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从存储在ArrayList中的对象到TextArea中获取各种属性?我有一个ListView,根据您按的ListView中的哪几行,不同的文本应出现在TextArea中.我就是无法正常工作.

I wonder how to do to get various attributes from my objects stored in an ArrayList to my TextArea? I have an ListView and depending on which of the lines in the ListView you press, different text should appear in the TextArea. I just can't get it to work.

到目前为止,这是我的一些代码.动物是另一类.

Here is a bit of my code this far. Animals is another class.

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

推荐答案

仔细查看您的代码后,我注意到了很多问题.回答您的原始问题.您需要在 SelectionModel的 ItemProperty 上使用侦听器来更新 TextArea .

After taking a closer look at your code, I noticed a ton of problems. To answer your original question. You need a listener on the SelectionModel's ItemProperty to updated the 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" 
);

似乎您想让 ListView 容纳 Animal 对象,但是您正在使用 String 对象.

It appears you want the ListView to hold Animal objects, but you are using String objects.

修复

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

第二个问题:现在, ListView 正在处理 Animal 对象,我们需要使用 ListView的 CellFActory 来告诉ListView 显示什么文本.在这种情况下,将显示名称.

Second Problem: Now that the ListView is handling Animal objects, we need to use the ListView's CellFActory to tell the ListView what text to display. In this case, the name will be displayed.

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

现在完成了这两项操作,可以添加原始问题代码.上面的代码根据在 ListView 中选择了哪个项目来更改 TextArea的文本.

Now that these two things are done, the original question code can be added. The code above that changes the TextArea's text depending on which item is selected in the ListView.

完整代码:

主要

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

我的动物课:(您没有发布您的动物课)

My Animal Class: (You did not post yours)

/**
 *
 * @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();
    }    
}

这篇关于将文本从Arraylist添加到TextArea javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆