【问题标题】:Add text from Arraylist to TextArea javafx将文本从 Arraylist 添加到 TextArea javafx
【发布时间】:2019-11-21 13:22:28
【问题描述】:

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

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

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

【问题讨论】:

  • 请提供一些代码,以便有人可以为您提供解决方案
  • 你需要做的和this非常相似。监听选择模型并相应地更新文本字段。
  • layout.getChildren().addAll(cats, catsdesricption); 是如何编译的。

标签: listview javafx arraylist


【解决方案1】:

仔细查看您的代码后,我发现了很多问题。回答你原来的问题。您需要SelectionModel's 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" 
);

您似乎希望ListView 保存Animal 对象,但您正在使用String 对象。

修复

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's CellFActory 来告诉ListView 要显示什么文本。在这种情况下,将显示名称。

    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's 文本。

完整代码:

主要

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

【讨论】:

  • 谢谢!在TextArea打印出来的时候,如果想给不同的猫的名字、类型和描述添加不同类型的字体和字体大小怎么办?
  • 研究问题。如果找不到解决方案,请提出新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多