gpt4 book ai didi

带有图标和文件名的 JavaFX 文件 ListView

转载 作者:行者123 更新时间:2023-12-04 21:24:06 25 4
gpt4 key购买 nike

在 JavaFX 对话框中,我想显示带有图标和文件名的文件列表。

很容易找到如何获取文件扩展名的图标:

File file = File.createTempFile("icon", ".doc");  
FileSystemView view = FileSystemView.getFileSystemView();
java.swing.Icon icon = view.getSystemIcon(file);
file.delete();

但是,我怎么能画那个 Swing Icon在 JavaFX 中 ListView ?
private static class AttachmentListCell extends ListCell<String> {
@Override
public void updateItem(String fileName, boolean empty) {
if (item != null) {

// Get file Icon for fileName as shown above.
java.swing.Icon icon =

// Transform Icon to something that can be
// added to the box, maybe an ImageView.
javafx.scene.image.ImageView image = ???

// Label for file name
Label label = new Label(item);

HBox box = new HBox();
box.getChildren().addAll(image, label);
setGraphic(box);
}
}
}

最佳答案

这是带有文件图标的 ListView 的示例:

enter image description here

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
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;
import javafx.util.Callback;

import javax.swing.filechooser.FileSystemView;

public class ListViewCellFactory2 extends Application {

ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList(
"a.msg", "a1.msg", "b.txt", "c.pdf",
"d.html", "e.png", "f.zip",
"g.docx", "h.xlsx", "i.pptx");

@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(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> list) {
return new AttachmentListCell();
}
});

stage.show();
}

private static class AttachmentListCell extends ListCell<String> {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
Image fxImage = getFileIcon(item);
ImageView imageView = new ImageView(fxImage);
setGraphic(imageView);
setText(item);
}
}
}

public static void main(String[] args) {
launch(args);
}


static HashMap<String, Image> mapOfFileExtToSmallIcon = new HashMap<String, Image>();

private static String getFileExt(String fname) {
String ext = ".";
int p = fname.lastIndexOf('.');
if (p >= 0) {
ext = fname.substring(p);
}
return ext.toLowerCase();
}

private static javax.swing.Icon getJSwingIconFromFileSystem(File file) {

// Windows {
FileSystemView view = FileSystemView.getFileSystemView();
javax.swing.Icon icon = view.getSystemIcon(file);
// }

// OS X {
//final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
//javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file);
// }

return icon;
}

private static Image getFileIcon(String fname) {
final String ext = getFileExt(fname);

Image fileIcon = mapOfFileExtToSmallIcon.get(ext);
if (fileIcon == null) {

javax.swing.Icon jswingIcon = null;

File file = new File(fname);
if (file.exists()) {
jswingIcon = getJSwingIconFromFileSystem(file);
}
else {
File tempFile = null;
try {
tempFile = File.createTempFile("icon", ext);
jswingIcon = getJSwingIconFromFileSystem(tempFile);
}
catch (IOException ignored) {
// Cannot create temporary file.
}
finally {
if (tempFile != null) tempFile.delete();
}
}

if (jswingIcon != null) {
fileIcon = jswingIconToImage(jswingIcon);
mapOfFileExtToSmallIcon.put(ext, fileIcon);
}
}

return fileIcon;
}

private static Image jswingIconToImage(javax.swing.Icon jswingIcon) {
BufferedImage bufferedImage = new BufferedImage(jswingIcon.getIconWidth(), jswingIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
jswingIcon.paintIcon(null, bufferedImage.getGraphics(), 0, 0);
return SwingFXUtils.toFXImage(bufferedImage, null);
}

}

编辑:在 HDPI 监视器上,文件图标被剪裁。

enter image description here
  • 必须做什么才能使图标完全显示在行中?
  • 如何从 Java 中检索比例因子?
  • 关于带有图标和文件名的 JavaFX 文件 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034432/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com