我一直在尋找一些關於JavaFx ListView的線程,但仍然很難找到一個簡單而簡單的解決方案來在JavaFx ListView中放置一個JavaFx ImageView。我正在嘗試使用JavaFx框架創建聊天應用程序。在照片應該看起來像這樣。這不是重複的,因爲另一個線程對於像我這樣的初學者來說是模糊的。 ListView with imageview insideJavaFx:如何把ImageView放入ListView
我試過這樣的事情。我完全不知道這個提前感謝!
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(行);其中「行」是一個HBox或GridPane ... –
可能的重複[圖像在JavaFX ListView](http://stackoverflow.com/questions/25570803/image-in-javafx-listview) – ItachiUchiha