【发布时间】:2016-02-09 02:32:59
【问题描述】:
我一直在搜索有关 JavaFx ListView 的一些主题,但仍然很难找到一个简单而简单的解决方案来将 JavaFx ImageView 放入 JavaFx ListView。我正在尝试使用 JavaFx 框架创建一个聊天应用程序。在照片上它应该看起来像那样。这不是重复的,因为其他线程对于像我这样的初学者来说是模糊的。 ListView with imageview inside
我尝试过这样的事情。我完全不知道这个提前谢谢!
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
ImageView imageView = new ImageView();
Image image = new Image(Main.class.getResourceAsStream("bell.jpg"));
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList(
"chocolate", "salmon", "gold", "coral", "darkorchid",
"darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
"blueviolet", "brown");
Image pic;
@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(listView -> new ListCell<String>() {
public void updateItem(String friend, boolean empty) {
super.updateItem(friend, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
imageView.setImage(image);
setText(friend);
setGraphic(imageView);
}
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}`
【问题讨论】:
-
这样就可以了:listCell.setGraphic(row);其中“行”是 HBox 或 GridPane...