我有两个来自twitter API的数据的哈希图。我想在控制台中显示的GUI中显示这些数据。
我该怎么做? TableView是一个选项,但有更好的选择吗?
HashMap<Integer, String> tweetName = new HashMap<>();
HashMap<Integer, String> tweetText = new HashMap<>();
for (int i = 0; i < tweets.size() - 97; i++) {
userName = tweets.get(i).getUser().getName();
text = tweets.get(i).getText();
tweetName.put(i, userName);
tweetText.put(i, text);
}
System.out.println(tweetName.get(0));
System.out.println(tweetText.get(0));
我希望以这样的格式显示推文和用户名:
[名称]
[我刚发推文]
[NAME2]
[我发推文]
答案 0 :(得分:0)
作为起点可能是这样的:
public class Twitter extends Application {
@Override
public void start(Stage primaryStage) {
ListView<Message> listView = new ListView<>();
listView.setCellFactory(lv -> new TwitterCell());
listView.setItems(getMessages());
Scene scene = new Scene(listView);
primaryStage.setScene(scene);
primaryStage.show();
}
private ObservableList<Message> getMessages() {
return FXCollections.observableArrayList(new Message("User1", "I just tweeted something sjködflk jskldjf slkd sdeklfjs kfjsl"),
new Message("User1", "I am tweeting this"));
}
public static void main(String[] args) {
launch(args);
}
public class Message {
private String userName;
private String content;
public Message(String userName, String content) {
this.userName = userName;
this.content = content;
}
public String getUserName() {
return userName;
}
public void setName(String name) {
userName = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
public class TwitterCell extends ListCell<Message> {
private Label lblName;
private Label lblContent;
private VBox box;
public TwitterCell() {
setPrefWidth(100);
lblName= new Label();
lblContent = new Label();
lblContent.setWrapText(true);
box = new VBox(lblName, lblContent);
}
@Override
protected void updateItem(Message message, boolean empty) {
super.updateItem(message, empty);
if (message == null || empty) {
setGraphic(null);
} else {
lblName.setText(message.getUserName());
lblContent.setText(message.getContent());
setGraphic(box);
}
}
}
}