我有一个带有imageView
项的TilePane,每个imageview显示从我的电脑中的目录获取的图像。单击tilePane
中的任何图像时,会在控制台上打印一条消息,其中包含图像所在文件夹的目录路径地址,例如:
You clicked: ImageResources/wp.png
我想进一步扩展这一点,以便在单击特定图像时打开图像所在的文件夹。
下面的我的实现仅打印到控制下面的消息,但没有打开目录/文件夹。消息是:
File Not Found
如何让它工作以便打开文件夹目录?提前谢谢大家。
此外,目录可以打开并选中所单击的图像,那么这将是一个额外的,但现在不是优先考虑。
到目前为止,我的实施如下:
public class TilePaneExample extends Application {
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(30);
String[] imageResources = new String[]{
//loading images
"ImageResources/facebook.png",
"ImageResources/faviicon.png",
"ImageResources/jquery-logo.png",
"ImageResources/linkedin_32.png",
"ImageResources/loading1.png",
"ImageResources/twitter.png",
"ImageResources/twitter_32.png",
"ImageResources/wp.png",};
// Pane
TilePane tilePane = new TilePane();
tilePane.setHgap(5);
tilePane.setVgap(5);
for (final String imageResource : imageResources) {
Image image = new Image(getClass().getResourceAsStream(imageResource));
ImageView imageView = new ImageView(image);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
File f = new File(imageResource);
String absolutePath = f.getAbsolutePath();
String folderPath = absolutePath.
substring(0, absolutePath.lastIndexOf(File.separator));
try {
Desktop.getDesktop().open(new File(folderPath));
} catch (IllegalArgumentException iae) {
System.out.println("File Not Found");
} catch (IOException ex) {
Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
tilePane.getChildren().add(imageView);
}
root.getChildren().addAll(tilePane);
primaryStage.setTitle("TilePane Example");
Scene scene = new Scene(root, 300, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
例如,单击第二个图像“ImageResources / faviicon.png”,将其文件夹路径打印为D:\ standAloneDev \ java \ workingDir \ Jive \ TilePaneExample \ ImageResources